data-dwh-dbt-project/models/intermediate/kpis/int_kpis__dimension_deals.sql
Oriol Roqué Paniagua 4867e8f6d0 Merged PR 4349: Xero metrics by Business Scope
# Description

Changes:
* Creation of a deal-based model that contains when a "deal has appeared in new dash". This is tricky because a Deal can still have multiple users, thus it needs to be attributed to a date. I've chosen the first user appearance for the rest of the metrics.
* Adaptation of dimension deals in KPIs to include a client type, that indicates if the deal is from APIs or not (Platform, i.e., Dashboard).
* Xero metrics by Business Scope. This is the previous "dash source" that I need to change in the previously worked models. I decided to include APIs in the segmentation since in most cases we distinguish old dash from new dash by just "anything that is not in new dash". This is very wrong for invoicing metrics, in which we have APIs. So this actually properly computes a client segmentation by scope.

Note that I'll need to handle the monthly/mtd metrics/agg for these 2 metric models (Resolutions + Invoiced revenue) separately.

# 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: #27356
2025-02-11 15:13:42 +00:00

72 lines
2.7 KiB
SQL

{{ config(materialized="table", unique_key="id_deal") }}
with
hubspot_deals as (
select
id_deal,
deal_pipeline,
deal_name as main_deal_name,
live_date_utc as deal_start_date,
date_trunc('month', live_date_utc) as deal_start_month,
cancellation_date_utc as deal_cancellation_date,
date_trunc('month', cancellation_date_utc) as deal_cancellation_month,
case
when amount_of_properties between 1 and 5
then '01-05'
when amount_of_properties between 6 and 20
then '06-20'
when amount_of_properties between 21 and 60
then '21-60'
when amount_of_properties >= 61
then '61+'
else 'UNSET'
end as hubspot_listing_segmentation
from {{ ref("int_hubspot__deal") }}
where live_date_utc is not null
),
core_deals as (
select
id_deal,
main_deal_name,
has_active_pms,
active_pms_list,
first_created_date_utc as deal_start_date,
date_trunc('month', first_created_date_utc) as deal_start_month,
main_billing_country_iso_3_per_deal
from {{ ref("int_core__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,
coalesce(cd.has_active_pms, false) as has_active_pms,
cd.active_pms_list,
cd.main_billing_country_iso_3_per_deal,
case
when upper(hd.deal_pipeline) in ('API SALES') then 'API' else 'PLATFORM'
end as client_type,
min(
coalesce(hd.deal_start_date, cd.deal_start_date)
) as effective_deal_start_date_utc,
min(
coalesce(hd.deal_start_month, cd.deal_start_month)
) as effective_deal_start_month,
min(hd.deal_cancellation_date) as hubspot_deal_cancellation_date_utc,
min(hd.deal_cancellation_month) as hubspot_deal_cancellation_month,
min(hd.hubspot_listing_segmentation) as hubspot_listing_segmentation
from hubspot_deals hd
full outer join core_deals cd on hd.id_deal = cd.id_deal
group by 1, 2, 3, 4, 5, 6
)
select
cd.id_deal,
cd.main_deal_name,
cd.has_active_pms,
cd.active_pms_list,
cd.client_type,
cd.main_billing_country_iso_3_per_deal,
cd.effective_deal_start_date_utc,
cd.effective_deal_start_month,
cd.hubspot_deal_cancellation_date_utc,
cd.hubspot_deal_cancellation_month,
cd.hubspot_listing_segmentation
from combined_deals cd