Merged PR 4894: Refactor growth score by deal to stop depending on cross models

# Description

Starting 2nd stage of the refactor so Account Managers stops depending on int_monthly_aggregated_metrics_history_by_deal.

This has been tested with the audit and the differences only happen with the rank of cases that are equal (ex: there's 2 deals with 0 revenue, then who is the 1st and the 2nd is random).

There are other possibilities of refactoring here (including hubspot deals info in int_kpis__dimension_deals, for instance) but I feel like this is a separated line of work if we want to.

# 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: #28947
This commit is contained in:
Oriol Roqué Paniagua 2025-04-03 07:06:49 +00:00
commit a14de4fe3a

View file

@ -1,28 +1,78 @@
{{ config(materialized="table", unique_key=["date", "id_deal"]) }}
with
int_monthly_aggregated_metrics_history_by_deal as (
select * from {{ ref("int_monthly_aggregated_metrics_history_by_deal") }}
),
int_kpis__dimension_deals as (select * from {{ ref("int_kpis__dimension_deals") }}),
int_hubspot__deal as (select * from {{ ref("int_hubspot__deal") }}),
int_kpis__lifecycle_daily_deal as (
select * from {{ ref("int_kpis__lifecycle_daily_deal") }}
),
int_kpis__agg_dates_main_kpis as (
select *
from {{ ref("int_kpis__agg_dates_main_kpis") }}
where
dimension in ('by_deal')
and dimension_value <> 'UNSET'
and is_end_of_month = true
),
created_bookings as (
select *
from {{ ref("int_kpis__agg_monthly_created_bookings") }}
where dimension in ('by_deal') and dimension_value <> 'UNSET'
),
listings as (
select *
from {{ ref("int_kpis__agg_daily_listings") }}
where
dimension in ('by_deal')
and dimension_value <> 'UNSET'
and is_end_of_month = true
),
total_and_retained_revenue as (
select *
from {{ ref("int_kpis__agg_monthly_total_and_retained_revenue") }}
where dimension in ('by_deal') and dimension_value <> 'UNSET'
),
deal_history_from_previous_months as (
select
year,
month,
date,
date_trunc('month', date)::date as first_day_month,
id_deal,
client_type,
main_deal_name,
has_active_pms,
active_pms_list,
main_billing_country_iso_3_per_deal,
deal_lifecycle_state,
coalesce(total_revenue_in_gbp, 0) as revenue_in_gbp,
coalesce(created_bookings, 0) as created_bookings,
coalesce(listings_booked_in_month, 0) as listings_booked_in_month
from int_monthly_aggregated_metrics_history_by_deal am
d.year,
d.month,
d.date,
date_trunc('month', d.date)::date as first_day_month,
-- DEAL STATIC ATTRIBUTES --
ikdd.id_deal,
ikdd.client_type,
ikdd.main_deal_name,
ikdd.has_active_pms,
ikdd.active_pms_list,
ikdd.main_billing_country_iso_3_per_deal,
-- DEAL LIFECYCLE --
deal_lifecycle.deal_lifecycle_state,
-- METRICS --
coalesce(
total_and_retained_revenue.total_revenue_in_gbp, 0
) as revenue_in_gbp,
coalesce(created_bookings.created_bookings, 0) as created_bookings,
coalesce(listings.listings_booked_in_month, 0) as listings_booked_in_month
from int_kpis__agg_dates_main_kpis d
left join int_kpis__dimension_deals ikdd on d.dimension_value = ikdd.id_deal
left join
int_kpis__lifecycle_daily_deal deal_lifecycle
on d.date = deal_lifecycle.date
and d.dimension_value = deal_lifecycle.id_deal
left join
created_bookings
on d.date = created_bookings.end_date
and d.dimension_value = created_bookings.dimension_value
left join
listings
on d.date = listings.date
and d.dimension_value = listings.dimension_value
left join
total_and_retained_revenue
on d.date = total_and_retained_revenue.end_date
and d.dimension_value = total_and_retained_revenue.dimension_value
-- Do not show data of ongoing month
where am.date < date_trunc('month', current_date)::date
where d.date < date_trunc('month', current_date)::date
),
revenue_12_months_window_aggregation_per_deal as (
select