Skip to content

Metric Filters Inherit Parent Query Granularity#464

Open
wtremml18 wants to merge 6 commits intodbt-labs:mainfrom
wtremml18:main
Open

Metric Filters Inherit Parent Query Granularity#464
wtremml18 wants to merge 6 commits intodbt-labs:mainfrom
wtremml18:main

Conversation

@wtremml18
Copy link

Resolves #463

Summary

This PR extends metric filter parsing to allow metric_time in Metric(..., 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:

  • “ARR from accounts sending push in that month”
  • “Email volume for SMS‑active accounts (per week)”

This change makes those filters time‑aware by allowing metric_time to be expressed in the filter group_by.

What’s Changing

  • Parsing: Metric('metric_name', group_by=[...]) now accepts metric_time.
  • Normalization: group_by items become GroupByItemReference entries (entity links + optional time dimension).
  • Validation: preserves the rule that metric filters have exactly one non‑metric_time group_by item (with optional 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']) }} > 0

Invalid:

# Multiple non‑metric_time group_by items
{{ Metric('bookings', group_by=['listing','user','metric_time']) }} > 2

Edge Cases / Expected Outcomes

  • If metric_time is present, the downstream engine can align joins at the query grain.
  • If the parent query has no time grain, the filter’s metric_time is ignored (backward compatible).
  • If the filter metric only supports a coarser grain (e.g., month), a daily query fails at query resolution (not parse).

Testing

From your local metricflow repo

  • hatch run dev-env:pip install -e ../dbt-semantic-interfaces
  • hatch run dev-env:pytest tests/parsing/test_where_filter_parsing.py
  • hatch run dev-env:pytest tests/implementations/where_filter/test_parse_calls.py
  • hatch run dev-env:pytest tests/validations/test_where_filters_are_parseable.py

Changelog

  • Not added yet (run changie new before opening PR).

Notes / Coordination

  • MetricFlow relies on this parsing change to implement time‑aligned metric filters.
  • API exposure (e.g., queryable time grains) should reflect the intersection of filter and base metric time grains to avoid surfacing unsupported grains.
    I am not sure where the APIs need to be updated!!

Checklist

  • [ x] I have read the contributing guide and understand what's expected of me
  • [ x] I have signed the CLA
  • [ x] This PR includes tests, or tests are not required/relevant for this PR
  • I have run changie new to create a changelog entry
    I did not add to the changelog for either PR!

wtremml18 and others added 2 commits January 16, 2026 11:03
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] Support metric_time in Metric Filters with Parent Query Granularity Inheritance

1 participant