2 weeks ago
Hi
I think I’ve found a reproducible bug / or am misunderstanding some syntax / capabilities of Metric Views when joining a calendar scaffold to an SCD2 table.
The same SQL query works perfectly, but the Metric View always returns a constant 1 per month when using COUNT(DISTINCT …) on a joined column. I've added some simple code / metric view that reproduces the behaviour I'm seeing.
Minimal Repro Code:
-- Base calendar
CREATE OR REPLACE TABLE dim_calendar_month AS
SELECT explode(sequence(DATE'2025-06-01', DATE'2025-08-01', INTERVAL 1 MONTH)) AS month_start,
last_day(month_start) AS month_end;
-- Simple SCD2 table
CREATE OR REPLACE TABLE dim_contact AS VALUES
('A','Open','client',DATE'2024-01-01',NULL),
('B','Open','client',DATE'2025-06-01',DATE'2025-07-01'),
('B','Closed','client',DATE'2025-07-01',NULL),
('D','Open','client',DATE'2025-07-15',NULL)
AS dim_contact(contact_id,status,contact_type,from_date,to_date);
-- Works fine (expected 2,2,2)
WITH calendar_cte AS (SELECT month_start, month_end FROM dim_calendar_month)
SELECT calendar_cte.month_end, COUNT(DISTINCT c.contact_id) AS open_contacts
FROM calendar_cte
LEFT JOIN dim_contact c
ON c.from_date <= calendar_cte .month_end
AND (c.to_date > calendar_cte .month_end OR c.to_date IS NULL)
AND lower(c.status)='open'
GROUP BY calendar_cte .month_end
ORDER BY calendar_cte .month_end;
Metric View:
SQL:
-- Returns 1, 1, 1
Unity Catalog enabled
Runtime: 16.0
DBSQL metric view syntax: version 1.1
2 weeks ago
Hey @playnicekids , I dig some digging and have come up with some helpful hints/tips to get you past your issue:
-- Metric view definition (YAML in SQL)
ALTER VIEW my_catalog.mv_bug_demo.dim_calendar_month_metric_view AS
version: 1.1
source: |
SELECT
m.month_end AS month,
c.contact_id
FROM my_catalog.mv_bug_demo.dim_calendar_month m
LEFT JOIN my_catalog.mv_bug_demo.dim_contact c
ON c.from_date <= m.month_end
AND (c.to_date > m.month_end OR c.to_date IS NULL)
AND lower(c.status) = 'open'
AND c.contact_type = 'client'
dimensions:
- name: month
expr: month
display_name: Month
- name: contact_id
expr: contact_id
display_name: Contact ID
measures:
- name: eligible_contacts
expr: COUNT(DISTINCT contact_id)
display_name: Eligible Contacts
sql
SELECT
month,
MEASURE(eligible_contacts) AS eligible_contacts
FROM my_catalog.mv_bug_demo.dim_calendar_month_metric_view
GROUP BY month
ORDER BY month;
CREATE OR REPLACE VIEW my_catalog.mv_bug_demo.v_month_contact_open AS
SELECT
m.month_end AS month,
c.contact_id
FROM my_catalog.mv_bug_demo.dim_calendar_month m
LEFT JOIN my_catalog.mv_bug_demo.dim_contact c
ON c.from_date <= m.month_end
AND (c.to_date > m.month_end OR c.to_date IS NULL)
AND lower(c.status) = 'open'
AND c.contact_type = 'client';
yaml
version: 1.1
source: my_catalog.mv_bug_demo.v_month_contact_open
dimensions:
- name: month
expr: month
display_name: Month
- name: contact_id
expr: contact_id
display_name: Contact ID
measures:
- name: eligible_contacts
expr: COUNT(DISTINCT contact_id)
display_name: Eligible Contacts
sql
SELECT month, MEASURE(eligible_contacts)
FROM my_catalog.mv_bug_demo.dim_calendar_month_metric_view
GROUP BY month
ORDER BY month;
Hope this helps, Louis.
2 weeks ago
Hey @playnicekids , I dig some digging and have come up with some helpful hints/tips to get you past your issue:
-- Metric view definition (YAML in SQL)
ALTER VIEW my_catalog.mv_bug_demo.dim_calendar_month_metric_view AS
version: 1.1
source: |
SELECT
m.month_end AS month,
c.contact_id
FROM my_catalog.mv_bug_demo.dim_calendar_month m
LEFT JOIN my_catalog.mv_bug_demo.dim_contact c
ON c.from_date <= m.month_end
AND (c.to_date > m.month_end OR c.to_date IS NULL)
AND lower(c.status) = 'open'
AND c.contact_type = 'client'
dimensions:
- name: month
expr: month
display_name: Month
- name: contact_id
expr: contact_id
display_name: Contact ID
measures:
- name: eligible_contacts
expr: COUNT(DISTINCT contact_id)
display_name: Eligible Contacts
sql
SELECT
month,
MEASURE(eligible_contacts) AS eligible_contacts
FROM my_catalog.mv_bug_demo.dim_calendar_month_metric_view
GROUP BY month
ORDER BY month;
CREATE OR REPLACE VIEW my_catalog.mv_bug_demo.v_month_contact_open AS
SELECT
m.month_end AS month,
c.contact_id
FROM my_catalog.mv_bug_demo.dim_calendar_month m
LEFT JOIN my_catalog.mv_bug_demo.dim_contact c
ON c.from_date <= m.month_end
AND (c.to_date > m.month_end OR c.to_date IS NULL)
AND lower(c.status) = 'open'
AND c.contact_type = 'client';
yaml
version: 1.1
source: my_catalog.mv_bug_demo.v_month_contact_open
dimensions:
- name: month
expr: month
display_name: Month
- name: contact_id
expr: contact_id
display_name: Contact ID
measures:
- name: eligible_contacts
expr: COUNT(DISTINCT contact_id)
display_name: Eligible Contacts
sql
SELECT month, MEASURE(eligible_contacts)
FROM my_catalog.mv_bug_demo.dim_calendar_month_metric_view
GROUP BY month
ORDER BY month;
Hope this helps, Louis.
2 weeks ago
Incredible, thanks very much Louis!
Passionate about hosting events and connecting people? Help us grow a vibrant local community—sign up today to get started!
Sign Up Now