data-dwh-dbt-project/models/intermediate/kpis/int_kpis__metric_mtd_invoiced_revenue.sql
Oriol Roqué Paniagua f5311fa954 Merged PR 3431: Adds Invoicing metrics
# Description

Same old story: includes all metrics coming from Xero.
Adds a daily model, monthly, mtd + monthly agg and mtd agg
A test to compare values new vs. old
AND fixes the issue I mentioned with the timestamp - an issue in the old KPIs.

# 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.
- [NA] 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: #23565
2024-11-05 16:57:23 +00:00

38 lines
1.3 KiB
SQL

{{
config(
materialized="view",
unique_key=[
"end_date",
"id_deal",
"active_accommodations_per_deal_segmentation",
],
)
}}
select
-- Unique Key --
d.first_day_month as start_date,
d.date as end_date,
ir.id_deal,
ir.active_accommodations_per_deal_segmentation,
-- Dimensions --
ir.main_billing_country_iso_3_per_deal,
-- Metrics --
sum(ir.xero_booking_net_fees_in_gbp) as xero_booking_net_fees_in_gbp,
sum(ir.xero_listing_net_fees_in_gbp) as xero_listing_net_fees_in_gbp,
sum(ir.xero_verification_net_fees_in_gbp) as xero_verification_net_fees_in_gbp,
sum(ir.xero_operator_net_fees_in_gbp) as xero_operator_net_fees_in_gbp,
sum(
ir.xero_waiver_paid_back_to_host_in_gbp
) as xero_waiver_paid_back_to_host_in_gbp,
sum(ir.xero_e_deposit_net_fees_in_gbp) as xero_e_deposit_net_fees_in_gbp,
sum(ir.xero_guesty_net_fees_in_gbp) as xero_guesty_net_fees_in_gbp,
sum(ir.xero_apis_net_fees_in_gbp) as xero_apis_net_fees_in_gbp
from {{ ref("int_kpis__dimension_dates") }} d
left join
{{ ref("int_kpis__metric_daily_invoiced_revenue") }} ir
on date_trunc('month', ir.date)::date = d.first_day_month
and extract(day from ir.date) <= d.day
where d.is_month_to_date = true and ir.id_deal is not null
group by 1, 2, 3, 4, 5