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

30 lines
1.1 KiB
SQL

/*
This macro provides a boolean answer to the question:
- Is this date before the previous month?
- When the current day is before the 20th of the month, it returns:
- False for both the current and previous month.
- When the current day is the 20th or later, it returns:
- False only for the current month.
Inputs:
- date: the date to check
Output:
- boolean; true if the date is before the previous month
false if the date is within the current or previous month
(with special handling for the 20th or later of the month)
*/
{% macro is_date_before_previous_month_fixed_20(date) %}
(
case
-- If today is the 20th or later, only exclude current month
when extract(day from now()) >= 20
then
date_trunc('month', ({{ date }})::date)::date
< date_trunc('month', now())::date
-- If today is before the 20th, exclude both current and previous month
else
date_trunc('month', ({{ date }})::date)::date
< date_trunc('month', now() - interval '1 month')::date
end
)
{% endmacro %}