data-dwh-dbt-project/macros/tests/kpis_outlier_detector.sql

34 lines
1.1 KiB
MySQL
Raw Normal View History

2024-11-26 09:27:13 +01:00
{% test kpis_outlier_detector(
model, column_name, sigma_threshold=3, days_to_consider=14
) %}
with
-- Retrieve recent data based on the defined days_to_consider
recent_data as (
select *
from {{ model }}
where
date_day between (
current_date - interval '{{ days_to_consider + 1 }} days'
) and (current_date - interval '1 day')
),
metrics_stats as (
select
avg({{ column_name }}) as avg_value,
stddev({{ column_name }}) as stddev_value
from recent_data
),
outliers as (
select
{{ column_name }} as value,
abs({{ column_name }} - metrics_stats.avg_value)
> (metrics_stats.stddev_value * {{ sigma_threshold }}) as is_outlier
from {{ model }}
cross join metrics_stats
where date_day = current_date - interval '1 day'
)
-- Return failing rows if any values are flagged as outliers
select *
from outliers
where is_outlier = true
{% endtest %}