data-dwh-dbt-project/macros/is_date_before_12th_of_previous_month.sql
Oriol Roqué Paniagua baf9d2767c Merged PR 5215: Switch from 20th to 12th for invoicing cap
# Description

After a discussion with Nathan, the invoicing cycle has again been reduced by quite a few days.
We agreed to move the cap from 20th to 12th of the following month.

# 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.
- [ ] I have checked for DRY opportunities with other models and docs.
- [ ] 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: #30147
2025-05-15 14:10:27 +00:00

27 lines
1 KiB
SQL

/*
This macro returns a boolean value for dates depending on the current day of the month
Logic:
- If today is the 12th or later of the current month:
- Returns **False** for all dates within the current month.
- Returns **True** for dates before the current month.
- If today is before the 12th:
- Returns **False** for all dates within the current and previous month.
- Returns **True** for dates before the previous month.
*/
{% macro is_date_before_12th_of_previous_month(date) %}
(
case
-- If today is the 12th or later, only exclude current month
when extract(day from now()) >= 12
then
date_trunc('month', ({{ date }})::date)::date
< date_trunc('month', now())::date
-- If today is before the 12th, exclude both current and previous month
else
date_trunc('month', ({{ date }})::date)::date
< date_trunc('month', now() - interval '1 month')::date
end
)
{% endmacro %}