Merged PR 4483: Adding targets (to be revised). Allows multi-year last-date computation

# Description

Changes:
* Adds template for targets -> These need to be revised
* Adds comparison of values vs. targets
* Allows for multi-year last date computation (allowing multiple years to be selected, not just the last one)

# Checklist

- [X] The edited models and dependants run properly with production data.
- [X] The edited models are sufficiently documented.
- [X] The edited models contain PK tests, and I've ran and passed them.
- [X] I have checked for DRY opportunities with other models and docs.
- [X] I've picked the right materialization for the affected models.

# Other

- [ ] Check if a full-refresh is required after this PR is merged.

Related work items: #27609, #27805
This commit is contained in:
Oriol Roqué Paniagua 2025-02-24 10:39:45 +00:00
parent cbed87404e
commit 85c2c73da7
6 changed files with 332 additions and 40 deletions

View file

@ -153,39 +153,73 @@
with
int_ytd_mtd_main_metrics_overview as (
select * from {{ ref("int_ytd_mtd_main_metrics_overview") }}
),
stg_seed__main_metrics_targets as (
select * from {{ ref("stg_seed__main_metrics_targets") }}
),
main_metrics_aggregation as (
{% for metric in metrics %}
select
calendar_year,
financial_year,
date,
previous_year_date,
dimension,
dimension_value,
{{ metric.id_metric }} as id_metric,
-- quotation marks added because text format
'{{ metric.name }}' as metric_name,
{{ metric.requires_invoicing_data }} as requires_invoicing_data,
{{ metric.current_month_MTD }} as current_month_mtd,
{{ metric.previous_month_EOM }} as previous_month_eom,
{{ metric.previous_year_MTD }} as previous_year_mtd,
{{ metric.current_YTD }} as current_year_ytd,
{{ metric.previous_YTD }} as previous_year_ytd,
{{ metric.current_month_MTD }}
- {{ metric.previous_month_EOM }}
as diff_current_month_mtd_vs_previous_month_eom,
{{ metric.current_month_MTD }}
- {{ metric.previous_year_MTD }}
as diff_current_month_mtd_vs_previous_year_mtd,
{{ metric.current_YTD }}
- {{ metric.previous_YTD }} as diff_current_ytd_vs_previous_ytd,
{{ metric.current_month_MTD }}
/ nullif({{ metric.previous_month_EOM }}, 0)
- 1 as rel_diff_current_month_mtd_vs_previous_month_eom,
{{ metric.current_month_MTD }}
/ nullif({{ metric.previous_year_MTD }}, 0)
- 1 as rel_diff_current_month_mtd_vs_previous_year_mtd,
{{ metric.current_YTD }} / nullif({{ metric.previous_YTD }}, 0)
- 1 as rel_diff_current_ytd_vs_previous_ytd
from int_ytd_mtd_main_metrics_overview
{% if not loop.last %}
union all
{% endif %}
{% endfor %}
)
{% for metric in metrics %}
select
calendar_year,
financial_year,
date,
previous_year_date,
dimension,
dimension_value,
{{ metric.id_metric }} as id_metric,
-- quotation marks added because text format
'{{ metric.name }}' as metric_name,
{{ metric.requires_invoicing_data }} as requires_invoicing_data,
{{ metric.current_month_MTD }} as current_month_mtd,
{{ metric.previous_month_EOM }} as previous_month_eom,
{{ metric.previous_year_MTD }} as previous_year_mtd,
{{ metric.current_YTD }} as current_year_ytd,
{{ metric.previous_YTD }} as previous_year_ytd,
{{ metric.current_month_MTD }}
- {{ metric.previous_month_EOM }}
as diff_current_month_mtd_vs_previous_month_eom,
{{ metric.current_month_MTD }}
- {{ metric.previous_year_MTD }} as diff_current_month_mtd_vs_previous_year_mtd,
{{ metric.current_YTD }}
- {{ metric.previous_YTD }} as diff_current_ytd_vs_previous_ytd,
{{ metric.current_month_MTD }} / nullif({{ metric.previous_month_EOM }}, 0)
- 1 as rel_diff_current_month_mtd_vs_previous_month_eom,
{{ metric.current_month_MTD }} / nullif({{ metric.previous_year_MTD }}, 0)
- 1 as rel_diff_current_month_mtd_vs_previous_year_mtd,
{{ metric.current_YTD }} / nullif({{ metric.previous_YTD }}, 0)
- 1 as rel_diff_current_ytd_vs_previous_ytd
from int_ytd_mtd_main_metrics_overview
{% if not loop.last %}
union all
{% endif %}
{% endfor %}
select
metrics.*,
-- EOM target --
targets.target_eom_value as target_eom_value,
metrics.current_month_mtd
- targets.target_eom_value as diff_current_month_mtd_vs_eom_target,
metrics.current_month_mtd / nullif(targets.target_eom_value, 0)
- 1 as rel_diff_current_month_mtd_vs_eom_target,
-- YTD target --
targets.target_ytd_value as target_ytd_value,
metrics.current_year_ytd
- targets.target_ytd_value as diff_current_ytd_vs_ytd_target,
metrics.current_year_ytd / nullif(targets.target_ytd_value, 0)
- 1 as rel_diff_current_ytd_vs_ytd_target,
-- EOFY target --
targets.target_eofy_value as target_eofy_value,
metrics.current_year_ytd
- targets.target_eofy_value as diff_current_ytd_vs_eofy_target,
metrics.current_year_ytd / nullif(
targets.target_eofy_value, 0
) as achievement_rate_current_ytd_vs_eofy_target
from main_metrics_aggregation as metrics
left join
stg_seed__main_metrics_targets as targets
on metrics.id_metric = targets.id_metric
and date_trunc('month', metrics.date) = date_trunc('month', targets.target_date)

View file

@ -2312,3 +2312,61 @@ models:
data_type: numeric
description: |
Relative difference between the current year YTD and the previous year YTD.
- name: target_eom_value
data_type: numeric
description: |
The EOM target value for this metric. This is the value that we aim to
achieve by the end of the month. It can be null if the target is not
available.
- name: target_ytd_value
data_type: numeric
description: |
The YTD target value for this metric. This is the cumulative value that we
aim to achieve by the end of each month with respect to the beginning of the
financial year, that will put us to reach the EOFY target. It can be null if
the target is not available.
- name: target_eofy_value
data_type: numeric
description: |
The EOFY target value for this metric. This is the value that we aim to
achieve by the end of the financial year. It can be null if the target is
not available.
- name: diff_current_month_mtd_vs_eom_target
data_type: numeric
description: |
Difference between the current month MTD and the EOM target. It can be null
if the target is not available.
- name: diff_current_ytd_vs_ytd_target
data_type: numeric
description: |
Difference between the current year YTD and the YTD target. It can be null
if the target is not available.
- name: diff_current_ytd_vs_eofy_target
data_type: numeric
description: |
Difference between the current year YTD and the EOFY target. It can be null
if the target is not available.
- name: rel_diff_current_month_mtd_vs_eom_target
data_type: numeric
description: |
Relative difference between the current month MTD and the EOM target. It can be null
if the target is not available.
- name: rel_diff_current_ytd_vs_ytd_target
data_type: numeric
description: |
Relative difference between the current year YTD and the YTD target. It can be null
if the target is not available.
- name: achievement_rate_current_ytd_vs_eofy_target
data_type: numeric
description: |
Achievement rate between the current year YTD and the EOFY target. It can be null
if the target is not available.