From 261c6d3c49e834d04c00fa320abcc501a16c5a65 Mon Sep 17 00:00:00 2001 From: Joaquin Ossa Date: Wed, 15 Jan 2025 12:03:25 +0100 Subject: [PATCH] Updated logic of model --- .../cross/int_monthly_onboarding_mrr_metrics.sql | 15 ++++++++------- models/intermediate/cross/schema.yml | 16 +++++++++++++--- 2 files changed, 21 insertions(+), 10 deletions(-) diff --git a/models/intermediate/cross/int_monthly_onboarding_mrr_metrics.sql b/models/intermediate/cross/int_monthly_onboarding_mrr_metrics.sql index edb93c2..71c3cf8 100644 --- a/models/intermediate/cross/int_monthly_onboarding_mrr_metrics.sql +++ b/models/intermediate/cross/int_monthly_onboarding_mrr_metrics.sql @@ -7,7 +7,7 @@ with ), monthly_revenue_per_number_of_properties as ( select - date_trunc('month', m.date)::date as metric_month, + m.date as metric_month, coalesce( d.main_billing_country_iso_3_per_deal, 'global' ) as main_billing_country_iso_3, @@ -22,19 +22,20 @@ with on m.id_deal = d.id_deal and date_trunc('month', m.date) >= d.effective_deal_start_month and date_trunc('month', m.date) - <= coalesce(d.effective_deal_cancellation_month, '2099-01-01') + <= coalesce(d.hubspot_deal_cancellation_month, '2099-01-01') and date_trunc('month', m.date)::date <> date_trunc('month', now())::date where d.hubspot_listing_segmentation <> 'UNSET' group by rollup ( - date_trunc('month', m.date)::date, + m.date, d.hubspot_listing_segmentation, d.main_billing_country_iso_3_per_deal ) - having date_trunc('month', m.date)::date is not null + -- Exclude total date aggregation + having m.date is not null ) select - (r.metric_month - interval '1 day')::date as date, + r.metric_month as date, r.main_billing_country_iso_3, r.hubspot_listing_segmentation, sum(coalesce(m.total_revenue_in_gbp, 0)) as total_revenue_in_gbp, @@ -44,8 +45,8 @@ select from monthly_revenue_per_number_of_properties m inner join monthly_revenue_per_number_of_properties r - on r.metric_month > m.metric_month - and r.metric_month <= m.metric_month + interval '12 months' + on r.metric_month >= m.metric_month + and r.metric_month < m.metric_month + interval '12 months' and r.hubspot_listing_segmentation = m.hubspot_listing_segmentation and r.main_billing_country_iso_3 = m.main_billing_country_iso_3 group by r.metric_month, r.hubspot_listing_segmentation, r.main_billing_country_iso_3 diff --git a/models/intermediate/cross/schema.yml b/models/intermediate/cross/schema.yml index df08619..a19a116 100644 --- a/models/intermediate/cross/schema.yml +++ b/models/intermediate/cross/schema.yml @@ -1714,12 +1714,22 @@ models: each new deal. It is calculated by taking the total revenue generated by all active accounts over the last 12 months and dividing it by the number of active months for each account." + + data_tests: + - dbt_utils.unique_combination_of_columns: + combination_of_columns: + - date + - main_billing_country_iso_3 + - hubspot_listing_segmentation + columns: - name: date data_type: date description: | - "Last date of the month for which the data is reported. - It considers the data from the previous 12 months up to the date." + "Date representing the last day of the month. The metrics are calculated using data + from the 12 months leading up to and including this date. Along with + `main_billing_country_iso_3` and `hubspot_listing_segmentation`, this field serves + as part of the primary key for the model." data_tests: - not_null - is_last_day_of_month @@ -1727,7 +1737,7 @@ models: - name: main_billing_country_iso_3 data_type: text description: | - "ISO 3166-1 alpha-3 main country code in which the Deals are billed" + "Main billing country code from ISO 3166" data_tests: - not_null