data-dwh-dbt-project/models/intermediate/cross/int_mtd_aggregated_metrics_by_deal.sql
Oriol Roqué Paniagua a67e3f46ba Merged PR 4948: Reporting model for mtd aggregated metrics by deal
# Description

Creates a new model for reporting. It includes any relevant metric at Deal level in a similar format as we do for `mtd_aggregated_metrics`. Additionally, there's few Deal attributes - from Hubspot, segmentations, lifecycles, etc.

In order to dynamically choose which metrics are relevant on a Deal level, I modified the configuration in `int_mtd_aggregated_metrics` so the extraction is under control.

# 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. **Worth discussing the possibilities to include indexes in the future**

# Other

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

Related work items: #28998
2025-04-08 10:04:11 +00:00

77 lines
2.4 KiB
SQL

with
int_mtd_aggregated_metrics as (
select *
from {{ ref("int_mtd_aggregated_metrics") }} m
where dimension = 'by_deal' and include_in_account_reporting = true
),
int_kpis__dimension_deals as (select * from {{ ref("int_kpis__dimension_deals") }}),
int_hubspot__deal as (select * from {{ ref("int_hubspot__deal") }}),
daily_deal_lifecycle as (select * from {{ ref("int_kpis__lifecycle_daily_deal") }}),
int_kpis__dimension_daily_accommodation as (
select * from {{ ref("int_kpis__dimension_daily_accommodation") }}
)
select
-- PRIMARY KEY --
m.date,
m.dimension_value as id_deal,
m.metric,
-- TIME ATTRIBUTES --
m.year,
m.month,
m.day,
m.is_end_of_month,
m.is_current_month,
m.is_end_of_month_or_yesterday,
m.first_day_month,
-- MAIN DEAL ATTRIBUTES --
m.dimension_value || '-' || coalesce(ikdd.main_deal_name, '') as deal,
coalesce(
dda.active_accommodations_per_deal_segmentation, 'UNSET'
) as active_accommodations_per_deal_segmentation,
ikdd.main_billing_country_iso_3_per_deal,
-- HUBSPOT ATTRIBUTES --
hd.account_manager,
-- DEAL BUSINESS SCOPE
case
when ikdd.client_type = 'API'
then 'API'
when ikdd.client_type = 'PLATFORM'
then
case
when
ikdd.id_deal is not null
and m.date >= ikdd.min_user_in_new_dash_since_date_utc
then 'New Dash'
else 'Old Dash'
end
else 'UNSET'
end as business_scope,
-- DEAL LIFECYCLE --
daily_deal_lifecycle.deal_lifecycle_state,
-- METRIC VALUES AND DISPLAY --
m.order_by,
m.number_format,
-- Force 0 if null to avoid nulls in the report
coalesce(m.value, 0) as value,
coalesce(m.previous_year_value, 0) as previous_year_value,
m.relative_increment,
m.relative_increment_with_sign_format,
m.display_exclusion
from int_mtd_aggregated_metrics m
left join int_kpis__dimension_deals ikdd on m.dimension_value = ikdd.id_deal
left join int_hubspot__deal hd on m.dimension_value = hd.id_deal
left join
daily_deal_lifecycle
on m.date = daily_deal_lifecycle.date
and m.dimension_value = daily_deal_lifecycle.id_deal
left join
int_kpis__dimension_daily_accommodation as dda
on m.date = dda.date
and m.dimension_value = dda.id_deal