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

14 lines
610 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.
*/
{% 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 %}