dbt-labs / dbt-labs/metricflow
[Bug] Cumulative metrics don't work with time dimensions other than metric time.
- Dominant language
- Python
- Stars
- 1.8k
- Forks
- 202
- Avg merge
- 1d 8h
- Merged PRs (30d)
- 14
Description
### Is this a new bug in metricflow?
- [X] I believe this is a new bug in metricflow
- [X] I have searched the existing issues, and I could not find an existing issue for this bug
### Current Behavior
Running a query referencing the `agg_time_dimension` by name makes cumulative metrics ignore the times pine model. For example `mf query --metrics cumulative_revenue --group-by order_item__ordered_at,order_id__customer --explain` returns the following SQL:
> SELECT
> subq_3.order_item__ordered_at__day AS order_item__ordered_at__day
> , orders_src_3.customer_id AS order_id__customer
> , SUM(subq_3.revenue) AS cumulative_revenue
> FROM (
> SELECT
> cast(ordered_at as DATETIME) AS order_item__ordered_at__day
> , order_id
> , product_price AS revenue
> FROM ANALYTICS.js_dbt_sl_demo.order_items order_item_src_2
> ) subq_3
> LEFT OUTER JOIN
> ANALYTICS.js_dbt_sl_demo.orders orders_src_3
> ON
> subq_3.order_id = orders_src_3.order_id
> GROUP BY
> subq_3.order_item__ordered_at__
>
> day
> , orders_src_3.customer_id
Note that there is no time spine join in the SQL. `order_item__ordered_at` is the agg time dimension for this measure so I would expect to be able to call it. If I replace `order_item__ordered_at` with `metric_time` the cumulative metric works as expected.
>
> SELECT
> subq_5.metric_time__day AS metric_time__day
> , orders_src_3.customer_id AS order_id__customer
> , SUM(subq_5.revenue) AS cumulative_revenue
> FROM (
> SELECT
> subq_4.date_day AS metric_time__day
> , subq_2.order_id AS order_id
> , subq_2.revenue AS revenue
> FROM js_dbt_sl_demo.metricflow_time_spine subq_4
> INNER JOIN (
> SELECT
> cast(ordered_at as DATETIME) AS metric_time__day
> , order_id
> , product_price AS revenue
> FROM ANALYTICS.js_dbt_sl_demo.order_items order_item_src_2
> ) subq_2
> ON
> (
> subq_2.metric_time__day <= subq_4.date_day
> ) AND (
> subq_2.metric_time__day > DATEADD(day, -7, subq_4.date_day)
> )
> ) subq_5
> LEFT OUTER JOIN
> ANALYTICS.js_dbt_sl_demo.orders orders_src_3
> ON
> subq_5.order_id = orders_src_3.order_id
> GROUP BY
> subq_5.metric_time__day
> , orders_src_3.customer_id
> ORDER BY metric_time__day DESC
### Expected Behavior
I expect to be able to call `agg_time_dimension` by name or `metric_time` and have the query run. I also expect to be able to reference another time dimension that is not the `agg_time_dimension` and have the cumulative metric work properly. For example, if I had `ordered_at` which is the `agg_time_dimension` for cumulative revenue, and `ordered_at_local` which shows the local time I would expect to be able to reference either of these time dimensions and see the cumulative revenue at that date.
### Steps To Reproduce
In the jaffle-sl-template project run `mf query --metrics cumulative_revenue --group-by order_item__ordered_at,order_id__customer --explain`. The generated SQL will not join the timespine model.
### Relevant log output
_No response_
### Environment
```markdown
- OS:
- Python:
- dbt:
- metricflow:
```
### Which database are you using?
_No response_
### Additional Context
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.