Hi All, What would be the solution to execute a we...
# gooddata-cloud
a
Hi All, What would be the solution to execute a weighted average in MAQL ? does something like this would work ?
Copy code
SELECT SUM(Numerator) / SUM(Denominator)
Where Numerator and Denominator are sub queries ?
j
Hi, you can use following MAQL if fact dataset contains facts value and weight.
Copy code
select sum({fact/value} * {fact/weight}) / sum({fact/weight})
In case you need to calculate value as metric you may need to specify grain at which it is calculated. For example following MAQL
Copy code
select sum(select {metric/value} * {metric/weight} by label/date.day) / sum(select {metric/weight} by label/date.day)
will calculate value and weight for each day as interim step and then the weighted average will be computed.
You can also decompose it to multiple metrics. So Numerator:
Copy code
select {fact/value} * {fact/weight}
Denominator
Copy code
select {fact/weight}
can be used in
Copy code
SELECT SUM(metric/Numerator) / SUM(metric/Denominator)
or Numerator:
Copy code
select {metric/value} * {metric/weight} by label/date.day
Denominator
Copy code
select {metric/weight} by label/date.day
can be used in
Copy code
SELECT SUM(metric/Numerator) / SUM(metric/Denominator)
a
I created a query like this with the grain matching our data
Copy code
SELECT (
  SUM(SELECT{fact/private_metric_mqlCount.value} * {fact/private_metric_mqlToSQL.value} by {label/Metric_Date.month}) /
  SUM (SELECT{fact/private_metric_mqlCount.value} by {label/Metric_Date.month})
)
But it fails
Copy code
{
  "title": "Bad Request",
  "status": 400,
  "detail": "A result cache error has occurred during the calculation of the result",
  "resultId": "a8de28ed2bc07fc1439e26fcd3be822d2417fe9b",
  "reason": "Antichain='[attribute/private_metric_mqlCount.Metric_ID/06f46a2becd6468ca7f54e34db0ee38c, attribute/private_metric_mqlToSQL.Metric_ID/06f46a2becd6468ca7f54e34db0ee38c]' has no witness",
  "traceId": "8b3abb647b0826e00bd493f3149c8435"
}
Ok it's working using our precomputed metrics
But once the new metric added, I can't add the date dimension anymore ?
Is this related to the grain you talked about ?
j
You should not write
Copy code
select fact/private_metric_mqlCount.value by {label/Metric_Date.month}
Fact table may contain multiple facts for each month. You need to specify aggregation how to reduce number of facts to single value per month. E.g.
Copy code
select sum(fact/private_metric_mqlCount.value) by {label/Metric_Date.month}
or you can decompose it and make metric1:
Copy code
select sum(fact/private_metric_mqlCount.value)
and reference it from the other metric
Copy code
select {metric/metric1}  by {label/Metric_Date.month}
I am also not sure if the fact is connected to date in data model
a
Our fact is connected to the date dimension in the data model. I used metrics instead facts to create the query as you mentioned in your second solution. I did not add the "by" in the query I am trying with the by right now. That said, My others dimensions are also not working. Each metric is linked to a set of dimensions, here a funnel and a channel
j
I used a date as example of attribute at which granularity the individual values for weigted average should be aggregated. If you need to calculate it from facts without aggregation then weight and fact need to be probably on the same grain=in the same row of the same dataset, otherwise the query engine may be not sure how to join the fact tables.
a
ok I understand
Copy code
SELECT (
  SUM(SELECT{metric/mql_count_forecast} * {metric/mql_to_sql_forecast} by {label/Metric_Date.month}) /
  SUM (SELECT {metric/mql_count_forecast} by {label/Metric_Date.month})
)
So Imagine all my metrics share the same dimensions, funnel and channel, I could add some by grouping for each dimension I want to group ? Can I cumulate grain filters ?
j
You can specify multiple dimensions in BY rule if you want to make finer breakdown of values and weight from which the weighted average is computed. E.g.
Copy code
SELECT (
  SUM(SELECT{metric/mql_count_forecast} * {metric/mql_to_sql_forecast} by {label/Metric_Date.month},{label/funnel},{label/channel}) /
  SUM (SELECT {metric/mql_count_forecast} by {label/Metric_Date.month},{label/funnel},{label/channel})
)
The finer grain will make individual values smaller and thus also result of weighted average. Common dimensions can be used also for slicing of the result so you can calculate weighted average per funnel or per channel or per pair (funnel,channel).
For weighted average computed from data at different granularity levels you can create multiple metrics.
a
Ok thanks a lot for this help. I have a value that is now displaying. What i can't understand is why I can't use the date as dimension filter while building a report with this new metric One last thing please, I would like to display a row, with each value aggregated by quarter. Once the weighted average metric is dropped into the list I do not have access to the date dimension anymore. All facts that are used in the sub metrics are linked to the date dataset.
Ok seems to be working now. I had an active filter hidding the date. My bad.
Thanks a lot Jakub, your help is really appreciated.
j
I am not sure if there may be some limitation of grain you can use in filters if computation is done on month grain. But it seems you solved the problem with hidden filter so let’s hope it will be not issue. You are welcome.
🙌 1
a
I also removed the monthly grain since my data granularity is a month.
👍 1