Mitchell Alpert
07/03/2024, 7:28 PM| product name | sales date |
=============================
| widget 1 | 2024-01-01 |
| widget 1 | 2024-02-01 |
| widget 2 | 2023-03-02 |
| widget 1 | 2024-07-01 |
I need to calculate a metric that can be viewed by month, quarter or year that shows the percentage of products ( by product name) that were sold in that period and were also sold in the previous period. I'm having a hard time figuring out how to formulate the sub-selects I will need to find the count of products that are in both this period and the previous period. Any help would be appreciated, thanks!Michael Ullock
07/03/2024, 8:39 PMMitchell Alpert
07/03/2024, 8:56 PMSELECT (COUNT(product_name) WHERE (product_name IN (SELECT product_name WHERE sales_date.day = THIS(DAY)) AND product_name in (SELECT product_name WHERE sales_date.day = PREVIOUS(DAY))
The IN doesn't seem to accept a SELECT statement and I'm not sure how else I would form this metricMichael Ullock
07/03/2024, 10:24 PMSELECT (SUM([Metric A])) / (SUM([Metric B]))
Your metrics would look something like this:
Select COUNT ({label/product_name}) WHERE {label/sales_date.day} = THIS(DAY)
Select COUNT ({label/product_name}) WHERE {label/sales_date.day} = PREVIOUS (DAY)
And you’d then combine those metrics in a 3rd like this:
SELECT Metric Year = This / Metric Year = Previous
We have this legacy article that helps explain how to calculate and compare percentages which you might find useful - The images look different and the build of the is slightly different in look when using GD Cloud. However, the MAQL logic is the same:
https://help.gooddata.com/classic/en/dashboards-and-insights/reports/working-with-tables/calculate-percentage-shares/Mitchell Alpert
07/03/2024, 10:43 PMMitchell Alpert
07/08/2024, 8:04 PMMitchell Alpert
07/10/2024, 6:31 PMMoises Morales
07/10/2024, 9:43 PMMitchell Alpert
07/10/2024, 9:53 PMMichal Hauzírek
07/11/2024, 10:25 PMSELECT COUNT({label/product_name},{dataset/sales})
WHERE
(SELECT COUNT({dataset/sales}) BY {label/product_name} FOR PREVIOUSPERIOD({label/sales_date.day})) > 0
AND
(SELECT COUNT({dataset/sales}) BY {label/product_name}) > 0
The IN logic you were looking for is actually achieved by the sub-metrics dimensionalities.
(when the sub-metric in the condition has forced dimensionality with the BY, it hints the system that we want to apply the filtering on that granularity - on product_name in this case)
The metric looks a bit complicated but the logic is not that difficult. When rewritten to more human-readable form it would be:
SELECT COUNT(product) WHERE sales_by_product_normal>0 AND sales_by_product_previous_period>0
and the sales_by_product_...
logic is done by counting the sales records.
And the FOR PREVIOUSPERIOD shifts data in the submetric by one date period. What period it is, is dynamic and depends on the date granularity used in the visualization.
Note that this works without IFNULL, because the condition is for existence of the sales records.
If you on the other hand wanted to look for products sold in this period but not sold in he the previous period, you would need to put IFNULL around the sub-metrics…
I hope this helps.Mitchell Alpert
07/11/2024, 10:34 PM