Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Allow metrics/measures to be referenced in filters and where clause #740

Closed
3 tasks done
Jstein77 opened this issue Aug 28, 2023 · 2 comments · Fixed by #1102
Closed
3 tasks done

[Feature] Allow metrics/measures to be referenced in filters and where clause #740

Jstein77 opened this issue Aug 28, 2023 · 2 comments · Fixed by #1102
Assignees
Labels
backlog enhancement New feature or request linear

Comments

@Jstein77
Copy link
Contributor

Is this your first time submitting a feature request?

  • I have read the expectations for open source contributors
  • I have searched the existing issues, and I could not find an existing issue for this feature
  • I am requesting a straightforward extension of existing metricflow functionality, rather than a Big Idea better suited to a discussion

Describe the feature

Currently, filters and the where clause can only reference dimensions. This is overly restrictive, and there are valid cases where I might want to filter by a metric value. For example, I might want to calculate total revenue but only include users who made less than 3 orders in the last 10 days. The SQL to calculate this metric is simple:

select 
 sum(revenue) as revenue
from orders
where order_count_l10d < 3

However, this is challenging to do in MetricFlow and requires you to precompute the order_count_l10d on your dbt model, before referencing it as a dimension.

Describe alternatives you've considered

The only available workaround is to duplicate the aggregation logic in your dbt model and make a separate semantic model as a dimension source. Very much not ideal.

Who will this benefit?

No response

Are you interested in contributing this feature?

No response

Anything else?

No response

@Jstein77 Jstein77 added enhancement New feature or request triage Tasks that need to be triaged linear labels Aug 28, 2023
@Jstein77 Jstein77 self-assigned this Aug 29, 2023
@Jstein77 Jstein77 added backlog and removed triage Tasks that need to be triaged labels Aug 30, 2023
@kmclaugh
Copy link

kmclaugh commented Oct 9, 2023

Really, you ought to be able to filter by measures. For instance, "What is the total revenue for orders with a shipping amount less than $10?"

You should be able to run this command:

mf query --metrics revenue_total --group-by metric_time --where "{{Measure('order_id__shipping_amount')}} > 10"

You can work around it by recreate every Measure as a dimension, but that's a little annoying

@tlento tlento changed the title [Feature] Allow metrics to be referenced in filters and where clause [Feature] Allow metrics/measures to be referenced in filters and where clause Nov 27, 2023
@giokincade
Copy link

I'd love to see something like this.

@kmclaugh In order to recreate a measure as a dimension, wouldn't you have to create a separate model that aggregates the source model? Which then requires you to make choices about what granularity or group-by keys to aggregate by? Maybe I'm misunderstanding but that feels really arduous and probably only feasible for a handful of keys/dimensions you may want to aggregate by.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backlog enhancement New feature or request linear
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants