Capped new ratios
This commit is contained in:
parent
0df6d07c20
commit
e4d19b85e8
1 changed files with 33 additions and 5 deletions
|
|
@ -7,8 +7,36 @@ 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 %}
|
||||
{% macro calculate_safe_relative_increment(
|
||||
metric, current="current", previous="previous_year"
|
||||
) %}
|
||||
-- Metrics that need to be capped between -1 and 1
|
||||
{% set capped_metrics = [
|
||||
"host_resolution_payment_per_created_booking_ratio",
|
||||
"revenue_retained_post_resolutions_ratio",
|
||||
"revenue_retained_ratio",
|
||||
] %}
|
||||
{% if metric in capped_metrics %}
|
||||
-- Cap current and previous values between -1 and 1
|
||||
case
|
||||
when {{ current }}.{{ metric }} is null
|
||||
then null
|
||||
else least(1, greatest(-1, {{ current }}.{{ metric }}))
|
||||
end as {{ metric }},
|
||||
case
|
||||
when {{ previous }}.{{ metric }} is null
|
||||
then null
|
||||
else least(1, greatest(-1, {{ previous }}.{{ metric }}))
|
||||
end as {{ previous }}_{{ metric }},
|
||||
cast(least(1, greatest(-1, {{ current }}.{{ metric }})) as decimal)
|
||||
/ nullif(least(1, greatest(-1, {{ previous }}.{{ metric }})), 0)
|
||||
- 1 as relative_increment_{{ metric }}
|
||||
{% else %}
|
||||
-- No capping applied, default behavior
|
||||
{{ current }}.{{ metric }},
|
||||
{{ previous }}.{{ metric }} as {{ previous }}_{{ metric }},
|
||||
cast({{ current }}.{{ metric }} as decimal)
|
||||
/ nullif({{ previous }}.{{ metric }}, 0)
|
||||
- 1 as relative_increment_{{ metric }}
|
||||
{% endif %}
|
||||
{% endmacro %}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue