From bd25b473fb42cff6c097c24f3cc8f6bab8749243 Mon Sep 17 00:00:00 2001 From: Joaquin Ossa Date: Thu, 9 Jan 2025 15:56:58 +0100 Subject: [PATCH 1/2] Adding PMS info for Account Margin report --- ...hly_aggregated_metrics_history_by_deal.sql | 2 + ...metrics_history_by_deal_by_time_window.sql | 204 ++++++++++++------ models/intermediate/cross/schema.yml | 26 +++ .../kpis/int_kpis__dimension_deals.sql | 46 +++- models/intermediate/kpis/schema.yml | 11 + ...metrics_history_by_deal_by_time_window.sql | 2 + models/reporting/general/schema.yml | 13 ++ 7 files changed, 225 insertions(+), 79 deletions(-) diff --git a/models/intermediate/cross/int_monthly_aggregated_metrics_history_by_deal.sql b/models/intermediate/cross/int_monthly_aggregated_metrics_history_by_deal.sql index eb982be..11110ce 100644 --- a/models/intermediate/cross/int_monthly_aggregated_metrics_history_by_deal.sql +++ b/models/intermediate/cross/int_monthly_aggregated_metrics_history_by_deal.sql @@ -85,6 +85,8 @@ select -- DEAL STATIC ATTRIBUTES -- ikdd.id_deal, ikdd.main_deal_name, + ikdd.active_pms, + ikdd.pms, ikdd.main_billing_country_iso_3_per_deal, -- DEAL LIFECYCLE -- diff --git a/models/intermediate/cross/int_monthly_aggregated_metrics_history_by_deal_by_time_window.sql b/models/intermediate/cross/int_monthly_aggregated_metrics_history_by_deal_by_time_window.sql index bff173c..86a8f8e 100644 --- a/models/intermediate/cross/int_monthly_aggregated_metrics_history_by_deal_by_time_window.sql +++ b/models/intermediate/cross/int_monthly_aggregated_metrics_history_by_deal_by_time_window.sql @@ -3,14 +3,14 @@ with int_monthly_aggregated_metrics_history_by_deal as ( select * from {{ ref("int_monthly_aggregated_metrics_history_by_deal") }} ), - int_hubspot__deal as ( - select * from {{ ref("int_hubspot__deal") }} - ), + int_hubspot__deal as (select * from {{ ref("int_hubspot__deal") }}), metric_aggregation_all_history as ( select date, id_deal, main_deal_name, + active_pms, + pms, main_billing_country_iso_3_per_deal, deal_lifecycle_state, 'All History' as time_window, @@ -41,12 +41,24 @@ with }}, {{ calculate_aggregation_between_preceeding_x_and_y( - "total_guest_payments_in_gbp", "sum", "id_deal", "date", "", 1, True + "total_guest_payments_in_gbp", + "sum", + "id_deal", + "date", + "", + 1, + True, ) }}, {{ calculate_aggregation_between_preceeding_x_and_y( - "xero_operator_net_fees_in_gbp", "sum", "id_deal", "date", "", 1, True + "xero_operator_net_fees_in_gbp", + "sum", + "id_deal", + "date", + "", + 1, + True, ) }}, {{ @@ -60,9 +72,9 @@ with "sum", "id_deal", "date", - "", - 1, - True + "", + 1, + True, ) }}, {{ @@ -71,20 +83,20 @@ with "sum", "id_deal", "date", - "", - 1, - True + "", + 1, + True, ) }}, {{ calculate_aggregation_between_preceeding_x_and_y( - "xero_host_resolution_payment_count", - "sum", - "id_deal", - "date", - "", - 1, - True + "xero_host_resolution_payment_count", + "sum", + "id_deal", + "date", + "", + 1, + True, ) }}, {{ @@ -98,9 +110,9 @@ with "sum", "id_deal", "date", - "", - 1, - True + "", + 1, + True, ) }} from int_monthly_aggregated_metrics_history_by_deal @@ -110,6 +122,8 @@ with date, id_deal, main_deal_name, + active_pms, + pms, main_billing_country_iso_3_per_deal, deal_lifecycle_state, 'Previous 12 months' as time_window, @@ -159,7 +173,8 @@ with "sum", "id_deal", "date", - 12, 1 + 12, + 1, ) }}, {{ @@ -168,12 +183,18 @@ with "sum", "id_deal", "date", - 12, 1 + 12, + 1, ) }}, {{ calculate_aggregation_between_preceeding_x_and_y( - "xero_host_resolution_payment_count", "sum", "id_deal", "date", 12, 1 + "xero_host_resolution_payment_count", + "sum", + "id_deal", + "date", + 12, + 1, ) }}, {{ @@ -187,7 +208,8 @@ with "sum", "id_deal", "date", - 12, 1 + 12, + 1, ) }} @@ -198,6 +220,8 @@ with date, id_deal, main_deal_name, + active_pms, + pms, main_billing_country_iso_3_per_deal, deal_lifecycle_state, 'Previous 6 months' as time_window, @@ -247,7 +271,8 @@ with "sum", "id_deal", "date", - 6, 1 + 6, + 1, ) }}, {{ @@ -256,12 +281,18 @@ with "sum", "id_deal", "date", - 6, 1 + 6, + 1, ) }}, {{ calculate_aggregation_between_preceeding_x_and_y( - "xero_host_resolution_payment_count", "sum", "id_deal", "date", 6, 1 + "xero_host_resolution_payment_count", + "sum", + "id_deal", + "date", + 6, + 1, ) }}, {{ @@ -275,7 +306,8 @@ with "sum", "id_deal", "date", - 6, 1 + 6, + 1, ) }} @@ -286,6 +318,8 @@ with date, id_deal, main_deal_name, + active_pms, + pms, main_billing_country_iso_3_per_deal, deal_lifecycle_state, 'Previous 3 months' as time_window, @@ -335,7 +369,8 @@ with "sum", "id_deal", "date", - 3, 1 + 3, + 1, ) }}, {{ @@ -344,12 +379,18 @@ with "sum", "id_deal", "date", - 3, 1 + 3, + 1, ) }}, {{ calculate_aggregation_between_preceeding_x_and_y( - "xero_host_resolution_payment_count", "sum", "id_deal", "date", 3, 1 + "xero_host_resolution_payment_count", + "sum", + "id_deal", + "date", + 3, + 1, ) }}, {{ @@ -363,7 +404,8 @@ with "sum", "id_deal", "date", - 3, 1 + 3, + 1, ) }} @@ -374,6 +416,8 @@ with date, id_deal, main_deal_name, + active_pms, + pms, main_billing_country_iso_3_per_deal, deal_lifecycle_state, 'Previous month' as time_window, @@ -423,7 +467,8 @@ with "sum", "id_deal", "date", - 1, 1 + 1, + 1, ) }}, {{ @@ -432,12 +477,18 @@ with "sum", "id_deal", "date", - 1, 1 + 1, + 1, ) }}, {{ calculate_aggregation_between_preceeding_x_and_y( - "xero_host_resolution_payment_count", "sum", "id_deal", "date", 1, 1 + "xero_host_resolution_payment_count", + "sum", + "id_deal", + "date", + 1, + 1, ) }}, {{ @@ -451,7 +502,8 @@ with "sum", "id_deal", "date", - 1, 1 + 1, + 1, ) }} @@ -473,39 +525,49 @@ with select * from metric_aggregation_previous_1_month ) - select - -- Primary key - mabd.date, - mabd.id_deal, - mabd.time_window, - - -- Window attributes - date_trunc('month', mabd.min_date)::date as metric_from_date, - mabd.max_date as metric_to_date, +select + -- Primary key + mabd.date, + mabd.id_deal, + mabd.time_window, - -- Deal attributes - mabd.main_deal_name, - mabd.main_billing_country_iso_3_per_deal, - mabd.deal_lifecycle_state, - d.deal_hubspot_stage, - d.account_manager, - d.live_date_utc, - d.cancellation_date_utc, + -- Window attributes + date_trunc('month', mabd.min_date)::date as metric_from_date, + mabd.max_date as metric_to_date, - -- Windowed metrics - coalesce(mabd.sum_created_bookings,0) as created_bookings, - coalesce(mabd.avg_listings_booked_in_month,0) as listings_booked_in_month, - coalesce(mabd.sum_total_revenue_in_gbp,0) as total_revenue_in_gbp, - coalesce(mabd.sum_revenue_retained_in_gbp,0) as revenue_retained_in_gbp, - coalesce(mabd.sum_xero_waiver_paid_back_to_host_in_gbp,0) as waiver_paid_back_to_host_in_gbp, - coalesce(mabd.sum_xero_operator_net_fees_in_gbp,0) - + coalesce(mabd.sum_xero_apis_net_fees_in_gbp,0) as invoiced_revenue_in_gbp, - coalesce(mabd.sum_total_guest_payments_in_gbp,0) as guest_payments_in_gbp, - coalesce(mabd.sum_total_guest_payments_in_gbp,0) - + coalesce(mabd.sum_xero_waiver_paid_back_to_host_in_gbp,0) as guest_revenue_retained_in_gbp, - coalesce(mabd.sum_xero_host_resolution_payment_count,0) as host_resolution_payment_count, - coalesce(mabd.sum_xero_host_resolution_amount_paid_in_gbp,0) as host_resolution_amount_paid_in_gbp, - coalesce(mabd.sum_revenue_retained_post_resolutions_in_gbp,0) - as revenue_retained_post_resolutions_in_gbp - from metric_aggregation_by_deal mabd - left join int_hubspot__deal d on mabd.id_deal = d.id_deal + -- Deal attributes + mabd.main_deal_name, + mabd.active_pms, + mabd.pms, + mabd.main_billing_country_iso_3_per_deal, + mabd.deal_lifecycle_state, + d.deal_hubspot_stage, + d.account_manager, + d.live_date_utc, + d.cancellation_date_utc, + + -- Windowed metrics + coalesce(mabd.sum_created_bookings, 0) as created_bookings, + coalesce(mabd.avg_listings_booked_in_month, 0) as listings_booked_in_month, + coalesce(mabd.sum_total_revenue_in_gbp, 0) as total_revenue_in_gbp, + coalesce(mabd.sum_revenue_retained_in_gbp, 0) as revenue_retained_in_gbp, + coalesce( + mabd.sum_xero_waiver_paid_back_to_host_in_gbp, 0 + ) as waiver_paid_back_to_host_in_gbp, + coalesce(mabd.sum_xero_operator_net_fees_in_gbp, 0) + + coalesce(mabd.sum_xero_apis_net_fees_in_gbp, 0) as invoiced_revenue_in_gbp, + coalesce(mabd.sum_total_guest_payments_in_gbp, 0) as guest_payments_in_gbp, + coalesce(mabd.sum_total_guest_payments_in_gbp, 0) + coalesce( + mabd.sum_xero_waiver_paid_back_to_host_in_gbp, 0 + ) as guest_revenue_retained_in_gbp, + coalesce( + mabd.sum_xero_host_resolution_payment_count, 0 + ) as host_resolution_payment_count, + coalesce( + mabd.sum_xero_host_resolution_amount_paid_in_gbp, 0 + ) as host_resolution_amount_paid_in_gbp, + coalesce( + mabd.sum_revenue_retained_post_resolutions_in_gbp, 0 + ) as revenue_retained_post_resolutions_in_gbp +from metric_aggregation_by_deal mabd +left join int_hubspot__deal d on mabd.id_deal = d.id_deal diff --git a/models/intermediate/cross/schema.yml b/models/intermediate/cross/schema.yml index 460b47a..0081184 100644 --- a/models/intermediate/cross/schema.yml +++ b/models/intermediate/cross/schema.yml @@ -359,6 +359,19 @@ models: tests: - not_null + - name: active_pms + data_type: boolean + description: | + Does the deal have an active associated PMS. + tests: + - not_null + + - name: pms + data_type: string + description: | + Name of the active PMS associated with the deal. It can have more than + one PMS associated with it. It can be null doesn't have any PMS associated. + - name: main_billing_country_iso_3_per_deal data_type: string description: | @@ -1467,6 +1480,19 @@ models: tests: - not_null + - name: active_pms + data_type: boolean + description: | + Does the deal have an active associated PMS. + tests: + - not_null + + - name: pms + data_type: string + description: | + Name of the active PMS associated with the deal. It can have more than + one PMS associated with it. It can be null doesn't have any PMS associated. + - name: main_billing_country_iso_3_per_deal data_type: string description: | diff --git a/models/intermediate/kpis/int_kpis__dimension_deals.sql b/models/intermediate/kpis/int_kpis__dimension_deals.sql index be96141..d85142b 100644 --- a/models/intermediate/kpis/int_kpis__dimension_deals.sql +++ b/models/intermediate/kpis/int_kpis__dimension_deals.sql @@ -12,14 +12,44 @@ with first_created_date_utc as deal_start_date, main_billing_country_iso_3_per_deal from {{ ref("int_core__deal") }} + ), + integrations_per_user as ( + select + icuh.id_deal, + sci.id_superhog_user as id_user, + scit.display_name as active_pms + from {{ ref("stg_core__integration") }} sci + left join + {{ ref("stg_core__integration_type") }} scit + on sci.id_integration_type = scit.id_integration_type + left join + {{ ref("int_core__user_host") }} icuh + on sci.id_superhog_user = icuh.id_user_host + where sci.is_active = true and icuh.is_missing_id_deal = false + ), + integrations_per_deal as ( + select id_deal, string_agg(distinct active_pms, ', ') as distinct_active_pms + from integrations_per_user + group by id_deal + ), + combined_deals as ( + select + coalesce(hd.id_deal, cd.id_deal) as id_deal, + coalesce(hd.main_deal_name, cd.main_deal_name) as main_deal_name, + cd.main_billing_country_iso_3_per_deal, + min( + coalesce(hd.deal_start_date, cd.deal_start_date) + ) as effective_deal_start_date_utc + from hubspot_deals hd + full outer join core_deals cd on hd.id_deal = cd.id_deal + group by 1, 2, 3 ) select - coalesce(hd.id_deal, cd.id_deal) as id_deal, - coalesce(hd.main_deal_name, cd.main_deal_name) as main_deal_name, + cd.id_deal, + cd.main_deal_name, + case when ipd.distinct_active_pms is null then false else true end as active_pms, + ipd.distinct_active_pms as pms, cd.main_billing_country_iso_3_per_deal, - min( - coalesce(hd.deal_start_date, cd.deal_start_date) - ) as effective_deal_start_date_utc -from hubspot_deals hd -full outer join core_deals cd on hd.id_deal = cd.id_deal -group by 1, 2, 3 + cd.effective_deal_start_date_utc +from combined_deals cd +left join integrations_per_deal ipd on cd.id_deal = ipd.id_deal diff --git a/models/intermediate/kpis/schema.yml b/models/intermediate/kpis/schema.yml index 514b103..9f0cc3a 100644 --- a/models/intermediate/kpis/schema.yml +++ b/models/intermediate/kpis/schema.yml @@ -450,6 +450,17 @@ models: in both systems, Hubspot data will take precedence in terms of deal name. tests: - not_null + - name: active_pms + data_type: boolean + description: | + Does the deal have an active associated PMS. + tests: + - not_null + - name: pms + data_type: string + description: | + Name of the active PMS associated with the deal. It can have more than + one PMS associated with it. It can be null doesn't have any PMS associated. - name: main_billing_country_iso_3_per_deal data_type: string description: | diff --git a/models/reporting/general/monthly_aggregated_metrics_history_by_deal_by_time_window.sql b/models/reporting/general/monthly_aggregated_metrics_history_by_deal_by_time_window.sql index 961f1f9..67be709 100644 --- a/models/reporting/general/monthly_aggregated_metrics_history_by_deal_by_time_window.sql +++ b/models/reporting/general/monthly_aggregated_metrics_history_by_deal_by_time_window.sql @@ -11,6 +11,8 @@ select metric_from_date as metric_from_date, metric_to_date as metric_to_date, main_deal_name as main_deal_name, + active_pms as active_pms, + pms as pms, main_billing_country_iso_3_per_deal as main_billing_country_iso_3_per_deal, deal_lifecycle_state as deal_lifecycle_state, deal_hubspot_stage as deal_hubspot_stage, diff --git a/models/reporting/general/schema.yml b/models/reporting/general/schema.yml index ce9d3b3..7ca4a5e 100644 --- a/models/reporting/general/schema.yml +++ b/models/reporting/general/schema.yml @@ -1435,6 +1435,19 @@ models: tests: - not_null + - name: active_pms + data_type: boolean + description: | + Does the deal have an active associated PMS. + tests: + - not_null + + - name: pms + data_type: string + description: | + Name of the active PMS associated with the deal. It can have more than + one PMS associated with it. It can be null doesn't have any PMS associated. + - name: main_billing_country_iso_3_per_deal data_type: string description: | From 0c311d02e841ebb8badaf94b3183a2146081e22f Mon Sep 17 00:00:00 2001 From: Joaquin Ossa Date: Thu, 9 Jan 2025 16:31:22 +0100 Subject: [PATCH 2/2] Update name of fields --- ...hly_aggregated_metrics_history_by_deal.sql | 4 ++-- ...metrics_history_by_deal_by_time_window.sql | 24 +++++++++---------- models/intermediate/cross/schema.yml | 12 +++++----- .../kpis/int_kpis__dimension_deals.sql | 6 +++-- models/intermediate/kpis/schema.yml | 6 ++--- ...metrics_history_by_deal_by_time_window.sql | 4 ++-- models/reporting/general/schema.yml | 6 ++--- 7 files changed, 32 insertions(+), 30 deletions(-) diff --git a/models/intermediate/cross/int_monthly_aggregated_metrics_history_by_deal.sql b/models/intermediate/cross/int_monthly_aggregated_metrics_history_by_deal.sql index 11110ce..b668766 100644 --- a/models/intermediate/cross/int_monthly_aggregated_metrics_history_by_deal.sql +++ b/models/intermediate/cross/int_monthly_aggregated_metrics_history_by_deal.sql @@ -85,8 +85,8 @@ select -- DEAL STATIC ATTRIBUTES -- ikdd.id_deal, ikdd.main_deal_name, - ikdd.active_pms, - ikdd.pms, + ikdd.has_active_pms, + ikdd.active_pms_list, ikdd.main_billing_country_iso_3_per_deal, -- DEAL LIFECYCLE -- diff --git a/models/intermediate/cross/int_monthly_aggregated_metrics_history_by_deal_by_time_window.sql b/models/intermediate/cross/int_monthly_aggregated_metrics_history_by_deal_by_time_window.sql index 86a8f8e..f8811f7 100644 --- a/models/intermediate/cross/int_monthly_aggregated_metrics_history_by_deal_by_time_window.sql +++ b/models/intermediate/cross/int_monthly_aggregated_metrics_history_by_deal_by_time_window.sql @@ -9,8 +9,8 @@ with date, id_deal, main_deal_name, - active_pms, - pms, + has_active_pms, + active_pms_list, main_billing_country_iso_3_per_deal, deal_lifecycle_state, 'All History' as time_window, @@ -122,8 +122,8 @@ with date, id_deal, main_deal_name, - active_pms, - pms, + has_active_pms, + active_pms_list, main_billing_country_iso_3_per_deal, deal_lifecycle_state, 'Previous 12 months' as time_window, @@ -220,8 +220,8 @@ with date, id_deal, main_deal_name, - active_pms, - pms, + has_active_pms, + active_pms_list, main_billing_country_iso_3_per_deal, deal_lifecycle_state, 'Previous 6 months' as time_window, @@ -318,8 +318,8 @@ with date, id_deal, main_deal_name, - active_pms, - pms, + has_active_pms, + active_pms_list, main_billing_country_iso_3_per_deal, deal_lifecycle_state, 'Previous 3 months' as time_window, @@ -416,8 +416,8 @@ with date, id_deal, main_deal_name, - active_pms, - pms, + has_active_pms, + active_pms_list, main_billing_country_iso_3_per_deal, deal_lifecycle_state, 'Previous month' as time_window, @@ -537,8 +537,8 @@ select -- Deal attributes mabd.main_deal_name, - mabd.active_pms, - mabd.pms, + mabd.has_active_pms, + mabd.active_pms_list, mabd.main_billing_country_iso_3_per_deal, mabd.deal_lifecycle_state, d.deal_hubspot_stage, diff --git a/models/intermediate/cross/schema.yml b/models/intermediate/cross/schema.yml index 0081184..c5d87e2 100644 --- a/models/intermediate/cross/schema.yml +++ b/models/intermediate/cross/schema.yml @@ -359,18 +359,18 @@ models: tests: - not_null - - name: active_pms + - name: has_active_pms data_type: boolean description: | Does the deal have an active associated PMS. tests: - not_null - - name: pms + - name: active_pms_list data_type: string description: | Name of the active PMS associated with the deal. It can have more than - one PMS associated with it. It can be null doesn't have any PMS associated. + one PMS associated with it. It can be null if it doesn't have any PMS associated. - name: main_billing_country_iso_3_per_deal data_type: string @@ -1480,18 +1480,18 @@ models: tests: - not_null - - name: active_pms + - name: has_active_pms data_type: boolean description: | Does the deal have an active associated PMS. tests: - not_null - - name: pms + - name: active_pms_list data_type: string description: | Name of the active PMS associated with the deal. It can have more than - one PMS associated with it. It can be null doesn't have any PMS associated. + one PMS associated with it. It can be null if it doesn't have any PMS associated. - name: main_billing_country_iso_3_per_deal data_type: string diff --git a/models/intermediate/kpis/int_kpis__dimension_deals.sql b/models/intermediate/kpis/int_kpis__dimension_deals.sql index d85142b..f5586ef 100644 --- a/models/intermediate/kpis/int_kpis__dimension_deals.sql +++ b/models/intermediate/kpis/int_kpis__dimension_deals.sql @@ -47,8 +47,10 @@ with select cd.id_deal, cd.main_deal_name, - case when ipd.distinct_active_pms is null then false else true end as active_pms, - ipd.distinct_active_pms as pms, + case + when ipd.distinct_active_pms is null then false else true + end as has_active_pms, + ipd.distinct_active_pms as active_pms_list, cd.main_billing_country_iso_3_per_deal, cd.effective_deal_start_date_utc from combined_deals cd diff --git a/models/intermediate/kpis/schema.yml b/models/intermediate/kpis/schema.yml index 9f0cc3a..8301b76 100644 --- a/models/intermediate/kpis/schema.yml +++ b/models/intermediate/kpis/schema.yml @@ -450,17 +450,17 @@ models: in both systems, Hubspot data will take precedence in terms of deal name. tests: - not_null - - name: active_pms + - name: has_active_pms data_type: boolean description: | Does the deal have an active associated PMS. tests: - not_null - - name: pms + - name: active_pms_list data_type: string description: | Name of the active PMS associated with the deal. It can have more than - one PMS associated with it. It can be null doesn't have any PMS associated. + one PMS associated with it. It can be null if it doesn't have any PMS associated. - name: main_billing_country_iso_3_per_deal data_type: string description: | diff --git a/models/reporting/general/monthly_aggregated_metrics_history_by_deal_by_time_window.sql b/models/reporting/general/monthly_aggregated_metrics_history_by_deal_by_time_window.sql index 67be709..54d67a5 100644 --- a/models/reporting/general/monthly_aggregated_metrics_history_by_deal_by_time_window.sql +++ b/models/reporting/general/monthly_aggregated_metrics_history_by_deal_by_time_window.sql @@ -11,8 +11,8 @@ select metric_from_date as metric_from_date, metric_to_date as metric_to_date, main_deal_name as main_deal_name, - active_pms as active_pms, - pms as pms, + has_active_pms as has_active_pms, + active_pms_list as active_pms_list, main_billing_country_iso_3_per_deal as main_billing_country_iso_3_per_deal, deal_lifecycle_state as deal_lifecycle_state, deal_hubspot_stage as deal_hubspot_stage, diff --git a/models/reporting/general/schema.yml b/models/reporting/general/schema.yml index 7ca4a5e..24b4fde 100644 --- a/models/reporting/general/schema.yml +++ b/models/reporting/general/schema.yml @@ -1435,18 +1435,18 @@ models: tests: - not_null - - name: active_pms + - name: has_active_pms data_type: boolean description: | Does the deal have an active associated PMS. tests: - not_null - - name: pms + - name: active_pms_list data_type: string description: | Name of the active PMS associated with the deal. It can have more than - one PMS associated with it. It can be null doesn't have any PMS associated. + one PMS associated with it. It can be null if it doesn't have any PMS associated. - name: main_billing_country_iso_3_per_deal data_type: string