data-dwh-dbt-project/macros/calculate_safe_relative_increment.sql

18 lines
622 B
MySQL
Raw Normal View History

/*
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.
*/
2025-01-24 11:36:22 +01:00
{% macro calculate_safe_relative_increment(
metric, current="current", previous="previous_year"
) %}
2025-01-24 14:17:29 +01:00
{{ current }}.{{ metric }},
{{ previous }}.{{ metric }} as {{ previous }}_{{ metric }},
cast({{ current }}.{{ metric }} as decimal) / nullif({{ previous }}.{{ metric }}, 0)
- 1 as relative_increment_{{ metric }}
2025-01-24 11:36:22 +01:00
{% endmacro %}