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
14 lines
No EOL
610 B
SQL
14 lines
No EOL
610 B
SQL
/*
|
|
This macro displays a metric value common in two sources, current and previous,
|
|
and computes the relative increment of current vs. previous.
|
|
|
|
It's designed to be placed within a SELECT statement.
|
|
|
|
It ensure safe divide by zero division by applying a nullif function.
|
|
|
|
*/
|
|
{% macro calculate_safe_relative_increment(metric, current='current', previous='previous_year') %}
|
|
{{ current }}.{{ metric }},
|
|
{{ previous }}.{{ metric }} as {{ previous }}_{{ metric }},
|
|
cast({{ current }}.{{ metric }} as decimal) / nullif({{ previous }}.{{ metric }},0) - 1 as relative_increment_{{ metric }}
|
|
{% endmacro %} |