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

fix(dbt): Support filtered COUNT DISTINCT in metrics #274

Merged
merged 1 commit into from
Mar 21, 2024

Conversation

Vitor-Avila
Copy link
Contributor

When creating a COUNT (DISTINCT x) metric with a filter -- for example:

semantic_models:
  - name: datadiff_test
    ...
    measures:
      - name: distinct_count_test
        expr: id
        description: "The count of unique rows."
        agg: count_distinct
    dimensions:
      - name: quantity
        type: categorical
    ...

metrics:
  - name: test_distinct_metric
    description: "The number of unique rows."
    type: simple
    label: Number of CX Sent Secure Messages
    type_params:
      measure: distinct_count_test
    filter: |                                     
      {{  Dimension('id__quantity') }} > 10

The metric conversion would result in an incompatible syntax:

COUNT(CASE WHEN quantity > 10 THEN DISTINCT id END)

This PR updates the logic to replace the DISTINCT as needed:

COUNT(DISTINCT CASE WHEN quantity > 10 THEN id END)

Copy link
Member

@betodealmeida betodealmeida left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lovely!

@Vitor-Avila Vitor-Avila merged commit 33c1a95 into main Mar 21, 2024
5 checks passed
@Vitor-Avila Vitor-Avila deleted the count-distinct-filtered branch March 21, 2024 23:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants