data-dwh-dbt-project/macros/is_date_before_12th_of_previous_month.sql

28 lines
1 KiB
MySQL
Raw Normal View History

/*
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 %}