data-dwh-dbt-project/models/intermediate/cross/int_dates_mtd.sql
Oriol Roqué Paniagua 94bdc53adf Merged PR 2498: Materialise int_dates_mtd and int_dates_by_deal as table to improve performance
# Description

Materialise int_dates_mtd and int_dates_by_deal as tables. This should improve the run speed as seen in local by quite a bit, and hopefully provide a better starting point for adding new dimensionality on business kpis.

I also documented these 2 models, that were missing :)

# 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: #19514
2024-08-06 15:03:32 +00:00

49 lines
1.6 KiB
SQL

{{ config(materialized="table", unique_key="date") }}
with
int_dates as (select * from {{ ref("int_dates") }} where date_day >= {{ var("start_date") }}),
raw_dates as (
select
id.year_number as year,
id.month_of_year as month,
id.day_of_month as day,
id.date_day as date,
id.month_start_date as first_day_month,
id.month_end_date as last_day_month,
now()::date as today
from int_dates id
)
-- Dates keeps only end of month (EOM) dates, as well as
-- dates corresponding to the current month and the same month last year
select distinct
rd.year,
rd.month,
rd.day,
rd.date,
rd.first_day_month,
rd.last_day_month,
case when rd.date = rd.last_day_month then 1 else 0 end as is_end_of_month,
case
when date_trunc('month', rd.date) = date_trunc('month', rd.today) then 1
-- If today is 1st of Month, include last day of month for MTD display
when rd.today = rd.last_day_month + 1 then 1
else 0
end as is_current_month
from raw_dates rd
where
rd.today > rd.date
-- include only up-to yesterday
and (
rd.date = rd.last_day_month
-- keep all last day of the month
or rd.date >= date_trunc('month', rd.today)
-- keep all individual days of the current month
or (
rd.year = extract(year from rd.today) - 1
and rd.month = extract(month from rd.today)
and rd.day < extract(day from rd.today)
)
-- keep all days of same month last day up to yesterday's day
)