Simon Lejeune
10/18/2023, 7:16 PMSELECT SUM({fact/products.price})
BY {dataset/surveys_pivot}
WHERE {label/customer_bought} IN (
"yes")
But then again it sums me all the product prices, not only the ones that have been bought.
This is trivial to do with an SQL statement (join the pivot with the products table, then sum) but seems a bit complicated here. It looks like GoodData doesn't understand that there is a relationship between my products and pivot table in that case (but it is working fine with the view by/stacked by feature) or that the "join" doesn't behave as I think it should behave (maybe it is summing all prices because it includes empty values when trying to join?)
Any help? I'm at a loss.
Thanks in advanceMichal Hauzírek
10/18/2023, 8:58 PMproduct_bought
contains NULL/empty value, you should also have a record in your Products
table with NULL/empty value in productId
- representing those surveys where no product was bought. Without this, you might be getting inconsistent results since GoodData is performing inner joins internally.
Second - the filters in GoodData are applied in the direction of the arrow in the LDM (that is in your case from Product to Survey) but not the other way. And also GoodData tries not to allow multiple-counting by exploding the tables unless explicitly instructed to in a metric.
So if you do SELECT SUM({fact/products.price})
it will perform the SUM only within the products table (not exploded to surveys) and it will not get filtered by Surveys.
So here are some options how to achieve what you want:
• Probably the simplest way how to do it would be something like SELECT SUM( ({fact/products.price} * COUNT({survey_pivot.surveyId}) ) WHERE {label/customer_bought} IN ("yes")
◦ Here it counts the number of surveys for each product (and this part is for sure filtered) and multiplies it by the product price and then it adds this for all the products.
• Another option could be this: SELECT SUM( (SELECT {fact/products.price} BY {survey_pivot.surveyId}) ) WHERE {label/customer_bought} IN ("yes")
◦ here in the inner metric (with BY) you perform the “explicit lifting” of the price fact to the surveys table - technically this is the way to tell GoodData to do the “expanding” join that multiplies one price value to multiple rows in surveys. Where it becomes filterable by everything in the surveys table. And then you just SUM these values.
I hope this helps.Simon Lejeune
10/19/2023, 11:10 AMSELECT SUM( (SELECT {fact/products.price} BY {dataset/survey_pivot}))
Notice the part inside the BY that points to the whole survey dataset and not the just surveyId column. With that change, it is behaving as expected.
I explored the idea of adding a "null" product with a 0 price like you suggested but, as far as i am with my tests, its presence or not do not impact the computation of the metric. Still i will keep that in mind if an issue arise.
I'm very happy, thanks a lot for your prompt response and help!
Have a nice day!