Merged PR 2232: KPI refactor - 1st step, bookings

First step on refactor of kpis:
- Remove relative incremental vs. previous year computation from the source model (`mtd_booking_metrics`, in this case)
- Aggregate the source mtd global metrics models into a single model: `int_mtd_vs_previous_year_metrics` (to enable multi-source weighted metric computation) and compute previous year value and relative increment. Now this logic is encapsulated into a macro `calculate_safe_relative_increment`, easing readability and providing a bit more robustness.
- End-to-end continuity to not break the existing dashboard display in `int_core__mtd_aggregated_metrics`

This is a substep of the global change. All info can be found in the documentation [here](https://www.notion.so/knowyourguest-superhog/Refactoring-Business-KPIs-5deb6aadddb34884ae90339402ac16e3)

Related work items: #18202
This commit is contained in:
Oriol Roqué Paniagua 2024-07-08 15:58:36 +00:00
parent 62ad19ea9e
commit 409ac47591
5 changed files with 101 additions and 46 deletions

View file

@ -1,6 +1,6 @@
{% set metrics = [
{
"source": "int_core__mtd_booking_metrics",
"source": "int_mtd_vs_previous_year_metrics",
"order_by": 1,
"metric": "Created Bookings",
"value": "created_bookings",
@ -9,7 +9,7 @@
"number_format": "integer",
},
{
"source": "int_core__mtd_booking_metrics",
"source": "int_mtd_vs_previous_year_metrics",
"order_by": 2,
"metric": "Cancelled Bookings",
"value": "cancelled_bookings",
@ -18,7 +18,7 @@
"number_format": "integer",
},
{
"source": "int_core__mtd_booking_metrics",
"source": "int_mtd_vs_previous_year_metrics",
"order_by": 3,
"metric": "Checkout Bookings",
"value": "check_out_bookings",
@ -190,8 +190,8 @@
},
] %}
with
int_core__mtd_booking_metrics as (
select * from {{ ref("int_core__mtd_booking_metrics") }}
int_mtd_vs_previous_year_metrics as (
select * from {{ ref("int_mtd_vs_previous_year_metrics") }}
),
int_core__mtd_guest_journey_metrics as (
select * from {{ ref("int_core__mtd_guest_journey_metrics") }}

View file

@ -41,46 +41,19 @@ with
and extract(day from b.updated_date_utc) <= d.day
and upper(b.booking_state) = {{ var("cancelled_booking_state") }}
group by 1
),
main_kpi as (
-- Final aggregation of subqueries --
select
d.year,
d.month,
d.day,
d.date,
d.is_end_of_month,
d.is_current_month,
crym.created_bookings,
coym.check_out_bookings,
caym.cancelled_bookings
from int_dates_mtd d
left join created_year_month crym on crym.date = d.date
left join check_out_year_month coym on coym.date = d.date
left join cancelled_year_month caym on caym.date = d.date
)
-- Pivoting to get previous year for each line & computing relative increment
-- (rel_incr) --
-- Final aggregation of subqueries --
select
a.year,
a.month,
a.day,
a.is_end_of_month,
a.is_current_month,
a.date,
b.date as previous_year_date,
a.created_bookings,
b.created_bookings as previous_year_created_bookings,
cast(a.created_bookings as decimal) / b.created_bookings
- 1 as relative_increment_created_bookings,
a.check_out_bookings,
b.check_out_bookings as previous_year_check_out_bookings,
cast(a.check_out_bookings as decimal) / b.check_out_bookings
- 1 as relative_increment_check_out_bookings,
a.cancelled_bookings,
b.cancelled_bookings as previous_year_cancelled_bookings,
cast(a.cancelled_bookings as decimal) / b.cancelled_bookings
- 1 as relative_increment_cancelled_bookings
from main_kpi a
left join main_kpi b on a.month = b.month and a.year = b.year + 1
where (a.is_end_of_month = 1 or (a.is_current_month = 1 and a.day = b.day))
d.year,
d.month,
d.day,
d.date,
d.is_end_of_month,
d.is_current_month,
crym.created_bookings,
coym.check_out_bookings,
caym.cancelled_bookings
from int_dates_mtd d
left join created_year_month crym on crym.date = d.date
left join check_out_year_month coym on coym.date = d.date
left join cancelled_year_month caym on caym.date = d.date