Metric Filters Inherit Parent Query Granularity#464
Open
wtremml18 wants to merge 6 commits intodbt-labs:mainfrom
Open
Metric Filters Inherit Parent Query Granularity#464wtremml18 wants to merge 6 commits intodbt-labs:mainfrom
wtremml18 wants to merge 6 commits intodbt-labs:mainfrom
Conversation
Introduces GroupByItemReference to represent group_by items in Metric calls, supporting parsed entity links and time granularity. Updates relevant code and tests to use GroupByItemReference instead of LinkableElementReference, and enhances parsing logic to handle custom granularity names and entity links in group_by parameters.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Resolves #463
Summary
This PR extends metric filter parsing to allow
metric_timeinMetric(..., group_by=[...])filters and normalizes those group_by items into structured references. This enables MetricFlow to align filter evaluation to the parent query’s time grain (e.g., month‑level filtering for month‑level queries).The companion PR is in the metricflow repo: dbt-labs/metricflow#1965
Motivation / User Impact
Without this, metric filters are effectively all‑time, even when the parent query is time‑bucketed. That makes common questions incorrect, such as:
This change makes those filters time‑aware by allowing
metric_timeto be expressed in the filter group_by.What’s Changing
Metric('metric_name', group_by=[...])now acceptsmetric_time.GroupByItemReferenceentries (entity links + optional time dimension).metric_time). Invalid group_by lists are rejected with clear errors.Examples
Valid:
{{ Metric('bookings', group_by=['listing','metric_time']) }} > 2 {{ Metric('sum_push_received', group_by=['klaviyo_account_id','metric_time__month']) }} > 0Invalid:
# Multiple non‑metric_time group_by items {{ Metric('bookings', group_by=['listing','user','metric_time']) }} > 2Edge Cases / Expected Outcomes
metric_timeis present, the downstream engine can align joins at the query grain.metric_timeis ignored (backward compatible).Testing
From your local
metricflowrepohatch run dev-env:pip install -e ../dbt-semantic-interfaceshatch run dev-env:pytest tests/parsing/test_where_filter_parsing.pyhatch run dev-env:pytest tests/implementations/where_filter/test_parse_calls.pyhatch run dev-env:pytest tests/validations/test_where_filters_are_parseable.pyChangelog
changie newbefore opening PR).Notes / Coordination
I am not sure where the APIs need to be updated!!
Checklist
changie newto create a changelog entryI did not add to the changelog for either PR!