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,
            xero_booking_net_fees_in_gbp as current_month_mtd_booking_net_fees_in_gbp,

            -- 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_year_xero_booking_net_fees_in_gbp
            as previous_year_mtd_booking_net_fees_in_gbp,

            -- 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,
            lag(xero_booking_net_fees_in_gbp) over (
                partition by dimension, dimension_value order by date
            ) as previous_month_eom_booking_net_fees_in_gbp

        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,
            sum(current_month_mtd_booking_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_booking_net_fees_in_gbp,
            -- 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,
            sum(previous_year_mtd_booking_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_booking_net_fees_in_gbp,
            -- 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,
    coalesce(current_month_mtd_booking_net_fees_in_gbp, 0) / nullif(
        current_month_mtd_billable_bookings, 0
    ) as current_month_mtd_booking_net_fees_per_billable_booking,

    -- 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,
    coalesce(previous_year_mtd_booking_net_fees_in_gbp, 0) / nullif(
        previous_year_mtd_billable_bookings, 0
    ) as previous_year_mtd_booking_net_fees_per_billable_booking,

    -- 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,
    coalesce(previous_month_eom_booking_net_fees_in_gbp, 0) / nullif(
        previous_month_eom_billable_bookings, 0
    ) as previous_month_eom_booking_net_fees_per_billable_booking,

    -- 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,
    coalesce(current_ytd_booking_net_fees_in_gbp, 0) / nullif(
        current_ytd_billable_bookings, 0
    ) as current_ytd_booking_net_fees_per_billable_booking,

    -- 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,
    coalesce(previous_ytd_booking_net_fees_in_gbp, 0) / nullif(
        previous_ytd_billable_bookings, 0
    ) as previous_ytd_booking_net_fees_per_billable_booking

from ytd_mtd_main_metrics_overview
