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 2739189..77a98ae 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 @@ -166,7 +166,8 @@ select as host_resolution_amount_paid_per_created_booking, {{ return_capped_value( - "cast(host_resolutions.xero_host_resolution_payment_count as decimal)/ created_bookings.created_bookings", + "cast(host_resolutions.xero_host_resolution_payment_count as decimal) + / created_bookings.created_bookings", -1, 1, ) 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 c2d72ea..f33b703 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 @@ -1,3 +1,7 @@ +-- HubSpot stage for live deals +{% set live_stage = "Live" %} +{% set churned_state = "05-Churning" %} + {{ config(materialized="table", unique_key=["date", "id_deal"]) }} with int_monthly_aggregated_metrics_history_by_deal as ( @@ -557,7 +561,32 @@ select d.account_manager, d.live_date_utc, d.cancellation_date_utc, + case + when mabd.deal_lifecycle_state = '{{churned_state}}' + then coalesce(d.cancellation_date_utc, mabd.date) - d.live_date_utc + else null + end as days_between_live_and_churn, + d.last_contacted_date_utc, + case + when mabd.deal_lifecycle_state = '{{churned_state}}' + then + case + when + coalesce(d.cancellation_date_utc, mabd.date) + - d.last_contacted_date_utc + < 0 + then null + else + coalesce(d.cancellation_date_utc, mabd.date) + - d.last_contacted_date_utc + end + else null + end as days_between_last_contacted_and_churn, + d.amount_times_contacted, d.cancellation_category, + case + when d.deal_hubspot_stage = '{{ live_stage }}' then true else false + end as is_churning_from_inactivity, -- Windowed metrics coalesce(mabd.sum_created_bookings, 0) as created_bookings, diff --git a/models/intermediate/cross/schema.yml b/models/intermediate/cross/schema.yml index 2ce96a4..49dc7ca 100644 --- a/models/intermediate/cross/schema.yml +++ b/models/intermediate/cross/schema.yml @@ -1636,6 +1636,44 @@ models: Hubspot. It can be null if the deal has never churned. + - name: days_between_live_and_churn + data_type: integer + description: | + Number of days between the live date and the + churn date. + data_tests: + - dbt_expectations.expect_column_values_to_be_between: + min_value: 0 + strictly: false + + - name: last_contacted_date_utc + data_type: date + description: | + Date when the deal was last contacted according to + Hubspot. + + - name: days_between_last_contacted_and_churn + data_type: integer + description: | + Number of days between the last contacted date + and the churn date. + data_tests: + - dbt_expectations.expect_column_values_to_be_between: + min_value: 0 + strictly: false + + - name: amount_times_contacted + data_type: integer + description: | + Number of times the deal was contacted according + to Hubspot. + + - name: is_churning_from_inactivity + data_type: boolean + description: | + Flag to identify if the deal is churning due to + inactivity because of lack of bookings in the past 12 months. + - name: created_bookings data_type: integer description: | diff --git a/models/intermediate/hubspot/int_hubspot__deal.sql b/models/intermediate/hubspot/int_hubspot__deal.sql index e074ade..ad1913d 100644 --- a/models/intermediate/hubspot/int_hubspot__deal.sql +++ b/models/intermediate/hubspot/int_hubspot__deal.sql @@ -21,6 +21,8 @@ select d.cancellation_category, d.cancellation_details, d.amount_of_properties, + d.last_contacted_date_utc, + d.amount_times_contacted, d.created_at_utc, d.created_date_utc, d.updated_at_utc, diff --git a/models/intermediate/hubspot/schema.yml b/models/intermediate/hubspot/schema.yml index 44460fd..8344d1e 100644 --- a/models/intermediate/hubspot/schema.yml +++ b/models/intermediate/hubspot/schema.yml @@ -72,6 +72,14 @@ models: Amount of properties the owner told us they manage. This is not necessarily the amount of properties that will be finally booked in our business. + - name: last_contacted_date_utc + data_type: date + description: Date in which the account was last contacted + + - name: amount_times_contacted + data_type: integer + description: Amount of times the account has been contacted + - name: created_at_utc data_type: timestamp with time zone description: Timestamp of when the record was created in Hubspot