Hi team, I have a simple metric aggregation issue...
# gooddata-cloud
e
Hi team, I have a simple metric aggregation issue that I need help with. I have a metric called 'Product Gross Order Revenue Score' in GoodData with the definition below:
Copy code
SELECT 
 CASE
  WHEN SUM({fact/gross_revenue_percentile}) <= 0.2 THEN 60
  WHEN SUM({fact/gross_revenue_percentile}) <= 0.4 THEN 48
  WHEN SUM({fact/gross_revenue_percentile}) <= 0.6 THEN 36
  WHEN SUM({fact/gross_revenue_percentile}) <= 0.8 THEN 24
  WHEN SUM({fact/gross_revenue_percentile}) <= 1 THEN 12
  ELSE 8 
 END
It's supposed to return a score based on the product's gross_revenue_percentile value. I created a simple table to see the score of an individual product. A screenshot of the table is attached below. The problem is that even though I put a product id filter to see a single product, it returns all the products in the database. Why does it return all the products ignoring the product id filter? I tried to use BY clause in the metric definition but it didn't help. Thanks in advance!
m
Hi Emir, off the top of my head I can’t be sure what is causing this issue. Could you please send a direct link to your report so I can review this for you? 🙂
Hi Emir, as this issue was resolved via DM, I am adding the metric syntax which resolved this issue in case it could help others in our community that might run into the same issue:
Copy code
SELECT SUM ( SELECT CASE WHEN (Gross revenue percentile) <= 0.2 THEN 60 WHEN (Gross revenue percentile) <= 0.4 THEN 48 WHEN (Gross revenue percentile) <= 0.6 THEN 36 WHEN (Gross revenue percentile) <= 0.8 THEN 24 WHEN (Gross revenue percentile) <= 1 THEN 12 ELSE 8 END)
More information on CASE metric can be found here: https://www.gooddata.com/docs/cloud/create-metrics/maql/conditionals/case/
j
I suggest to tell in metric definition that the SUM shall be calculated for each product separately and what to do if the product is not filtered (e.g. display maximum). E.g.
Copy code
SELECT MAX(
SELECT
 CASE
  WHEN SUM({fact/gross_revenue_percentile}) <= 0.2 THEN 60
  WHEN SUM({fact/gross_revenue_percentile}) <= 0.4 THEN 48
  WHEN SUM({fact/gross_revenue_percentile}) <= 0.6 THEN 36
  WHEN SUM({fact/gross_revenue_percentile}) <= 0.8 THEN 24
  WHEN SUM({fact/gross_revenue_percentile}) <= 1 THEN 12
  ELSE 8 
 END
 BY {label/product_id}
)
You may also consider to use some more efficient approximate formula like
GREATEST(8, 60 - 12 * FLOOR(5 * SUM({fact/gross_revenue_percentile})))
e
Hey Jakub, thanks for the help, it's really useful!