forked from dbt-labs/dbt_metrics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
secondary_calculation_period_over_period.sql
50 lines (41 loc) · 1.86 KB
/
secondary_calculation_period_over_period.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
{%- macro default__secondary_calculation_period_over_period(metric_name, grain, dimensions, calc_config, metric_config) -%}
{%- set calc_sql %}
lag(
{{ metric_name }}, {{ calc_config.interval }}
) over (
{% if dimensions -%}
partition by {{ dimensions | join(", ") }}
{% endif -%}
order by date_{{grain}}
)
{%- endset-%}
{%- if calc_config.comparison_strategy == 'difference' -%}
{% do return (adapter.dispatch('metric_comparison_strategy_difference', 'metrics')(metric_name, calc_sql, metric_config)) %}
{%- elif calc_config.comparison_strategy == 'ratio' -%}
{% do return (adapter.dispatch('metric_comparison_strategy_ratio', 'metrics')(metric_name, calc_sql, metric_config)) %}
{-% else -%}
{% do exceptions.raise_compiler_error("Bad comparison_strategy for period_over_period: " ~ calc_config.comparison_strategy ~ ". calc_config: " ~ calc_config) %}
{%- endif -%}
{% endmacro %}
{% macro default__metric_comparison_strategy_difference(metric_name, calc_sql, metric_config) -%}
{%- if not metric_config.get("treat_null_values_as_zero", True) %}
{{ metric_name }} - {{ calc_sql }}
{%- else -%}
coalesce({{ metric_name }}, 0) - coalesce(
{{ calc_sql }}
, 0)
{%- endif %}
{%- endmacro -%}
{% macro default__metric_comparison_strategy_ratio(metric_name, calc_sql, metric_config) -%}
{%- if not metric_config.get("treat_null_values_as_zero", True) %}
cast({{ metric_name }} as {{ type_float() }}) / nullif(
{{ calc_sql }}
, 0)
{%- else -%}
coalesce(
cast({{ metric_name }} as {{ type_float() }}) / nullif(
{{ calc_sql }}
, 0)
, 0)
{%- endif %}
{%- endmacro %}