data-dwh-dbt-project/models/intermediate/cross/int_ytd_mtd_main_metrics_overview.sql
Oriol Roqué Paniagua a6671ee4d0 Merged PR 4493: Adds Revenue Churn Rate in YTD/MTD Overview
# Description

Changes:
* Adds Revenue Churn Rate in YTD/MTD Overview. This has several implications, I finally understood how to properly compute a YTD.

The problem is that Revenue Churn Rate is a % of the Total "Churned" Revenue in a 12 m period vs. the Total Revenue in the same 12 m period. This is a bit tricky because it's not really additive, because of the Churn definition. Total Churned Revenue is the Revenue that the churned deals in a month generated on that past 12 months prior to churning.

So - in order to aggregate it properly, we need to do the sum of the Total Churned Revenue and retrieve the Total Revenue on these 12 months, and THEN compute the Churn rate.

This PR mainly retrieves the necessary input from the Churn models and then follows a similar computation as for the rest of YTD/MTD converted metrics.

I'll handle Onboarding MRR in a separated PR as this one is quite dense already.

# Checklist

- [X] The edited models and dependants run properly with production data.
- [X] The edited models are sufficiently documented.
- [X] The edited models contain PK tests, and I've ran and passed them.
- [X] I have checked for DRY opportunities with other models and docs.
- [X] I've picked the right materialization for the affected models.

# Other

- [ ] Check if a full-refresh is required after this PR is merged.

Related work items: #27609, #27805
2025-02-25 09:41:28 +00:00

358 lines
19 KiB
SQL

with
int_mtd_vs_previous_year_metrics as (
select * from {{ ref("int_mtd_vs_previous_year_metrics") }}
),
mtd_main_metrics_overview as (
select
-- Time-based attributes --
year as calendar_year,
case
when month >= 4
then year + 1 -- Financial Year labeled by the next year
else year -- Financial Year labeled by the current year
end as financial_year,
date,
previous_year_date,
-- Dimension/Value --
dimension,
dimension_value,
-- Current Month, Month To Date Metrics --
total_revenue_in_gbp as current_month_mtd_total_revenue_in_gbp,
revenue_retained_post_resolutions_in_gbp
as current_month_mtd_revenue_retained_post_resolutions_in_gbp,
total_guest_payments_in_gbp
as current_month_mtd_total_guest_payments_in_gbp,
xero_operator_net_fees_in_gbp
as current_month_mtd_xero_operator_net_fees_in_gbp,
xero_apis_net_fees_in_gbp as current_month_mtd_xero_apis_net_fees_in_gbp,
xero_host_resolution_amount_paid_in_gbp
as current_month_mtd_xero_host_resolution_amount_paid_in_gbp,
xero_waiver_paid_back_to_host_in_gbp
as current_month_mtd_xero_waiver_paid_back_to_host_in_gbp,
billable_bookings as current_month_mtd_billable_bookings,
new_deals as current_month_mtd_new_deals,
churning_deals as current_month_mtd_churning_deals,
live_deals as current_month_mtd_live_deals,
waiver_payments_in_gbp as current_month_mtd_waiver_payments_in_gbp,
total_revenue_churn_preceding_12_months
as current_month_mtd_total_revenue_churn_12m,
total_revenue_global_preceding_12_months
as current_month_mtd_total_revenue_global_12m,
-- Previous Year (12 months ago), Month To Date Metrics --
previous_year_total_revenue_in_gbp
as previous_year_mtd_total_revenue_in_gbp,
previous_year_revenue_retained_post_resolutions_in_gbp
as previous_year_mtd_revenue_retained_post_resolutions_in_gbp,
previous_year_total_guest_payments_in_gbp
as previous_year_mtd_total_guest_payments_in_gbp,
previous_year_xero_operator_net_fees_in_gbp
as previous_year_mtd_xero_operator_net_fees_in_gbp,
previous_year_xero_apis_net_fees_in_gbp
as previous_year_mtd_xero_apis_net_fees_in_gbp,
previous_year_xero_host_resolution_amount_paid_in_gbp
as previous_year_mtd_xero_host_resolution_amount_paid_in_gbp,
previous_year_xero_waiver_paid_back_to_host_in_gbp
as previous_year_mtd_xero_waiver_paid_back_to_host_in_gbp,
previous_year_billable_bookings as previous_year_mtd_billable_bookings,
previous_year_new_deals as previous_year_mtd_new_deals,
previous_year_churning_deals as previous_year_mtd_churning_deals,
previous_year_live_deals as previous_year_mtd_live_deals,
previous_year_waiver_payments_in_gbp
as previous_year_mtd_waiver_payments_in_gbp,
previous_year_total_revenue_churn_preceding_12_months
as previous_year_mtd_total_revenue_churn_12m,
previous_year_total_revenue_global_preceding_12_months
as previous_year_mtd_total_revenue_global_12m,
-- Previous Month, End Of Month Metrics --
lag(total_revenue_in_gbp) over (
partition by dimension, dimension_value order by date
) as previous_month_eom_total_revenue_in_gbp,
lag(revenue_retained_post_resolutions_in_gbp) over (
partition by dimension, dimension_value order by date
) as previous_month_eom_revenue_retained_post_resolutions_in_gbp,
lag(total_guest_payments_in_gbp) over (
partition by dimension, dimension_value order by date
) as previous_month_eom_total_guest_payments_in_gbp,
lag(xero_operator_net_fees_in_gbp) over (
partition by dimension, dimension_value order by date
) as previous_month_eom_xero_operator_net_fees_in_gbp,
lag(xero_apis_net_fees_in_gbp) over (
partition by dimension, dimension_value order by date
) as previous_month_eom_xero_apis_net_fees_in_gbp,
lag(xero_host_resolution_amount_paid_in_gbp) over (
partition by dimension, dimension_value order by date
) as previous_month_eom_xero_host_resolution_amount_paid_in_gbp,
lag(xero_waiver_paid_back_to_host_in_gbp) over (
partition by dimension, dimension_value order by date
) as previous_month_eom_xero_waiver_paid_back_to_host_in_gbp,
lag(billable_bookings) over (
partition by dimension, dimension_value order by date
) as previous_month_eom_billable_bookings,
lag(new_deals) over (
partition by dimension, dimension_value order by date
) as previous_month_eom_new_deals,
lag(churning_deals) over (
partition by dimension, dimension_value order by date
) as previous_month_eom_churning_deals,
lag(live_deals) over (
partition by dimension, dimension_value order by date
) as previous_month_eom_live_deals,
lag(waiver_payments_in_gbp) over (
partition by dimension, dimension_value order by date
) as previous_month_eom_waiver_payments_in_gbp,
lag(total_revenue_churn_preceding_12_months) over (
partition by dimension, dimension_value order by date
) as previous_month_eom_total_revenue_churn_12m,
lag(total_revenue_global_preceding_12_months) over (
partition by dimension, dimension_value order by date
) as previous_month_eom_total_revenue_global_12m
from int_mtd_vs_previous_year_metrics
where
is_end_of_month_or_yesterday = true
-- Keeping only global dimension to start with
and dimension = 'global'
),
ytd_mtd_main_metrics_overview as (
select
-- Keep all Time Attributes, Dimension/Value and Month/MTD metrics
*,
-- Current Financial Year, Year To Date Metrics
sum(current_month_mtd_total_revenue_in_gbp) over (
partition by financial_year, dimension, dimension_value
order by date
rows between unbounded preceding and current row
) as current_ytd_total_revenue_in_gbp,
sum(current_month_mtd_revenue_retained_post_resolutions_in_gbp) over (
partition by financial_year, dimension, dimension_value
order by date
rows between unbounded preceding and current row
) as current_ytd_revenue_retained_post_resolutions_in_gbp,
sum(current_month_mtd_total_guest_payments_in_gbp) over (
partition by financial_year, dimension, dimension_value
order by date
rows between unbounded preceding and current row
) as current_ytd_total_guest_payments_in_gbp,
sum(current_month_mtd_xero_operator_net_fees_in_gbp) over (
partition by financial_year, dimension, dimension_value
order by date
rows between unbounded preceding and current row
) as current_ytd_xero_operator_net_fees_in_gbp,
sum(current_month_mtd_xero_apis_net_fees_in_gbp) over (
partition by financial_year, dimension, dimension_value
order by date
rows between unbounded preceding and current row
) as current_ytd_xero_apis_net_fees_in_gbp,
sum(current_month_mtd_xero_host_resolution_amount_paid_in_gbp) over (
partition by financial_year, dimension, dimension_value
order by date
rows between unbounded preceding and current row
) as current_ytd_xero_host_resolution_amount_paid_in_gbp,
sum(current_month_mtd_xero_waiver_paid_back_to_host_in_gbp) over (
partition by financial_year, dimension, dimension_value
order by date
rows between unbounded preceding and current row
) as current_ytd_xero_waiver_paid_back_to_host_in_gbp,
sum(current_month_mtd_billable_bookings) over (
partition by financial_year, dimension, dimension_value
order by date
rows between unbounded preceding and current row
) as current_ytd_billable_bookings,
sum(current_month_mtd_new_deals) over (
partition by financial_year, dimension, dimension_value
order by date
rows between unbounded preceding and current row
) as current_ytd_new_deals,
sum(current_month_mtd_churning_deals) over (
partition by financial_year, dimension, dimension_value
order by date
rows between unbounded preceding and current row
) as current_ytd_churning_deals,
sum(current_month_mtd_waiver_payments_in_gbp) over (
partition by financial_year, dimension, dimension_value
order by date
rows between unbounded preceding and current row
) as current_ytd_waiver_payments_in_gbp,
sum(current_month_mtd_total_revenue_churn_12m) over (
partition by financial_year, dimension, dimension_value
order by date
rows between unbounded preceding and current row
) as current_ytd_total_revenue_churn_12m,
sum(current_month_mtd_total_revenue_global_12m) over (
partition by financial_year, dimension, dimension_value
order by date
rows between unbounded preceding and current row
) as current_ytd_total_revenue_global_12m,
-- Specific treatment for live_deals as it is a counter
current_month_mtd_live_deals as current_ytd_live_deals,
-- Previous Financial Year, Year To Date Metrics
sum(previous_year_mtd_total_revenue_in_gbp) over (
partition by financial_year, dimension, dimension_value
order by date
rows between unbounded preceding and current row
) as previous_ytd_total_revenue_in_gbp,
sum(previous_year_mtd_revenue_retained_post_resolutions_in_gbp) over (
partition by financial_year, dimension, dimension_value
order by date
rows between unbounded preceding and current row
) as previous_ytd_revenue_retained_post_resolutions_in_gbp,
sum(previous_year_mtd_total_guest_payments_in_gbp) over (
partition by financial_year, dimension, dimension_value
order by date
rows between unbounded preceding and current row
) as previous_ytd_total_guest_payments_in_gbp,
sum(previous_year_mtd_xero_operator_net_fees_in_gbp) over (
partition by financial_year, dimension, dimension_value
order by date
rows between unbounded preceding and current row
) as previous_ytd_xero_operator_net_fees_in_gbp,
sum(previous_year_mtd_xero_apis_net_fees_in_gbp) over (
partition by financial_year, dimension, dimension_value
order by date
rows between unbounded preceding and current row
) as previous_ytd_xero_apis_net_fees_in_gbp,
sum(previous_year_mtd_xero_host_resolution_amount_paid_in_gbp) over (
partition by financial_year, dimension, dimension_value
order by date
rows between unbounded preceding and current row
) as previous_ytd_xero_host_resolution_amount_paid_in_gbp,
sum(previous_year_mtd_xero_waiver_paid_back_to_host_in_gbp) over (
partition by financial_year, dimension, dimension_value
order by date
rows between unbounded preceding and current row
) as previous_ytd_xero_waiver_paid_back_to_host_in_gbp,
sum(previous_year_mtd_billable_bookings) over (
partition by financial_year, dimension, dimension_value
order by date
rows between unbounded preceding and current row
) as previous_ytd_billable_bookings,
sum(previous_year_mtd_new_deals) over (
partition by financial_year, dimension, dimension_value
order by date
rows between unbounded preceding and current row
) as previous_ytd_new_deals,
sum(previous_year_mtd_churning_deals) over (
partition by financial_year, dimension, dimension_value
order by date
rows between unbounded preceding and current row
) as previous_ytd_churning_deals,
sum(previous_year_mtd_waiver_payments_in_gbp) over (
partition by financial_year, dimension, dimension_value
order by date
rows between unbounded preceding and current row
) as previous_ytd_waiver_payments_in_gbp,
sum(previous_year_mtd_total_revenue_churn_12m) over (
partition by financial_year, dimension, dimension_value
order by date
rows between unbounded preceding and current row
) as previous_ytd_total_revenue_churn_12m,
sum(previous_year_mtd_total_revenue_global_12m) over (
partition by financial_year, dimension, dimension_value
order by date
rows between unbounded preceding and current row
) as previous_ytd_total_revenue_global_12m,
-- Specific treatment for live_deals as it is a counter
previous_year_mtd_live_deals as previous_ytd_live_deals
from mtd_main_metrics_overview
)
select
-- Keep all Time Attributes, Dimension/Value, Month/MTD metrics and YTD metrics
*,
-- Compute Derived Metrics --
-- Current Month, Month To Date Metrics --
abs(coalesce(current_month_mtd_xero_waiver_paid_back_to_host_in_gbp, 0)) / nullif(
current_month_mtd_waiver_payments_in_gbp, 0
) as current_month_mtd_waiver_payout_rate,
abs(
coalesce(current_month_mtd_xero_host_resolution_amount_paid_in_gbp, 0)
) / nullif(
current_month_mtd_total_revenue_in_gbp, 0
) as current_month_mtd_resolutions_payout_rate,
coalesce(current_month_mtd_xero_operator_net_fees_in_gbp, 0) / nullif(
current_month_mtd_billable_bookings, 0
) as current_month_mtd_operator_revenue_per_billable_booking,
coalesce(current_month_mtd_waiver_payments_in_gbp, 0) / nullif(
current_month_mtd_billable_bookings, 0
) as current_month_mtd_waiver_revenue_per_billable_booking,
coalesce(current_month_mtd_total_revenue_churn_12m, 0) / nullif(
current_month_mtd_total_revenue_global_12m, 0
) as current_month_mtd_total_revenue_churn_rate,
-- Previous Year (12 months ago), Month To Date Metrics --
abs(coalesce(previous_year_mtd_xero_waiver_paid_back_to_host_in_gbp, 0)) / nullif(
previous_year_mtd_waiver_payments_in_gbp, 0
) as previous_year_mtd_waiver_payout_rate,
abs(
coalesce(previous_year_mtd_xero_host_resolution_amount_paid_in_gbp, 0)
) / nullif(
previous_year_mtd_total_revenue_in_gbp, 0
) as previous_year_mtd_resolutions_payout_rate,
coalesce(previous_year_mtd_xero_operator_net_fees_in_gbp, 0) / nullif(
previous_year_mtd_billable_bookings, 0
) as previous_year_mtd_operator_revenue_per_billable_booking,
coalesce(previous_year_mtd_waiver_payments_in_gbp, 0) / nullif(
previous_year_mtd_billable_bookings, 0
) as previous_year_mtd_waiver_revenue_per_billable_booking,
coalesce(previous_year_mtd_total_revenue_churn_12m, 0) / nullif(
previous_year_mtd_total_revenue_global_12m, 0
) as previous_year_mtd_total_revenue_churn_rate,
-- Previous Month, End Of Month Metrics --
abs(coalesce(previous_month_eom_xero_waiver_paid_back_to_host_in_gbp, 0)) / nullif(
previous_month_eom_waiver_payments_in_gbp, 0
) as previous_month_eom_waiver_payout_rate,
abs(
coalesce(previous_month_eom_xero_host_resolution_amount_paid_in_gbp, 0)
) / nullif(
previous_month_eom_total_revenue_in_gbp, 0
) as previous_month_eom_resolutions_payout_rate,
coalesce(previous_month_eom_xero_operator_net_fees_in_gbp, 0) / nullif(
previous_month_eom_billable_bookings, 0
) as previous_month_eom_operator_revenue_per_billable_booking,
coalesce(previous_month_eom_waiver_payments_in_gbp, 0) / nullif(
previous_month_eom_billable_bookings, 0
) as previous_month_eom_waiver_revenue_per_billable_booking,
coalesce(previous_month_eom_total_revenue_churn_12m, 0) / nullif(
previous_month_eom_total_revenue_global_12m, 0
) as previous_month_eom_total_revenue_churn_rate,
-- Current Financial Year, Year To Date Metrics --
abs(coalesce(current_ytd_xero_waiver_paid_back_to_host_in_gbp, 0))
/ nullif(current_ytd_waiver_payments_in_gbp, 0) as current_ytd_waiver_payout_rate,
abs(coalesce(current_ytd_xero_host_resolution_amount_paid_in_gbp, 0)) / nullif(
current_ytd_total_revenue_in_gbp, 0
) as current_ytd_resolutions_payout_rate,
coalesce(current_ytd_xero_operator_net_fees_in_gbp, 0) / nullif(
current_ytd_billable_bookings, 0
) as current_ytd_operator_revenue_per_billable_booking,
coalesce(current_ytd_waiver_payments_in_gbp, 0) / nullif(
current_ytd_billable_bookings, 0
) as current_ytd_waiver_revenue_per_billable_booking,
coalesce(current_ytd_total_revenue_churn_12m, 0) / nullif(
current_ytd_total_revenue_global_12m, 0
) as current_ytd_total_revenue_churn_rate,
-- Previous Financial Year, Year To Date Metrics --
abs(coalesce(previous_ytd_xero_waiver_paid_back_to_host_in_gbp, 0))
/ nullif(previous_ytd_waiver_payments_in_gbp, 0) as previous_ytd_waiver_payout_rate,
abs(coalesce(previous_ytd_xero_host_resolution_amount_paid_in_gbp, 0)) / nullif(
previous_ytd_total_revenue_in_gbp, 0
) as previous_ytd_resolutions_payout_rate,
coalesce(previous_ytd_xero_operator_net_fees_in_gbp, 0) / nullif(
previous_ytd_billable_bookings, 0
) as previous_ytd_operator_revenue_per_billable_booking,
coalesce(previous_ytd_waiver_payments_in_gbp, 0) / nullif(
previous_ytd_billable_bookings, 0
) as previous_ytd_waiver_revenue_per_billable_booking,
coalesce(previous_ytd_total_revenue_churn_12m, 0) / nullif(
previous_ytd_total_revenue_global_12m, 0
) as previous_ytd_total_revenue_churn_rate
from ytd_mtd_main_metrics_overview