Reputation: 131
I have a list of 5 products in my BigQuery data set:
Product_Name | Sales |
---|---|
A | 1000 |
B | 2000 |
C | 3000 |
D | 4000 |
E | 5000 |
How can I get %OutOfTotalSales
?
Product_Name | Sales | %OutOfTotalSales |
---|---|---|
A | 1000 | 6.67% |
B | 2000 | 13.33% |
C | 3000 | 20.00% |
D | 4000 | 26.67% |
E | 5000 | 33.33% |
I assume that I need to create an extra field, TOTAL_GROSS
:
TOTAL_GROSS / PRODUCT_GROSS*100/TOTAL_GROSS
The question would then be: How can I get TOTAL_GROSS
inside this expression?
Sample:
Data Set (Google Sheets)
Google Data Studio report
Upvotes: 0
Views: 375
Reputation: 6471
It can be achieved using the comparison metric, Percent of total:
Product_Name
Sales
SUM
Sales
%OutOfTotalSales
SUM
Publicly editable Google Data Studio report (embedded Google Sheets data source) and a GIF to elaborate:
Upvotes: 1
Reputation: 1946
If I understand correctly then ideally you would do that calculation outside of Data Studio as Data Studio isn't really set up to do that automatically.
However if that isn't an option, you'll have to create a metric separately for each product which for product A would look something like:
sum(CASE WHEN Product_Name = 'A' then Sales Else 0 END)/sum(Sales)
To do the calculation in BigQuery you would do:
SELECT Product_Name, Sales, Sales/SUM(Sales) OVER () as %OutOfTotalSales
FROM you_table
Upvotes: 3