diff --git a/macros/is_date_before_20th_of_previous_month.sql b/macros/is_date_before_20th_of_previous_month.sql new file mode 100644 index 0000000..bba73e9 --- /dev/null +++ b/macros/is_date_before_20th_of_previous_month.sql @@ -0,0 +1,27 @@ +/* +This macro returns a boolean value for dates depending on the current day of the month + +Logic: +- If today is the 20th 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 20th: + - Returns **False** for all dates within the current and previous month. + - Returns **True** for dates before the previous month. + +*/ +{% macro is_date_before_20th_of_previous_month(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 %} diff --git a/models/reporting/general/mtd_aggregated_metrics.sql b/models/reporting/general/mtd_aggregated_metrics.sql index 686a469..257aff1 100644 --- a/models/reporting/general/mtd_aggregated_metrics.sql +++ b/models/reporting/general/mtd_aggregated_metrics.sql @@ -62,7 +62,8 @@ where ( ( -- Not show current + previous month if the metric depends on invoicing - -- cycle + -- cycle and it is before the 20th of the month, if it is the 20th of + -- the month or after, only exclude the current month. ( lower(metric) like '%total revenue%' or lower(metric) like '%resolutions%' @@ -71,7 +72,7 @@ where or lower(metric) like '%mrr%' or lower(metric) like '%damage host%' ) - and {{ is_date_before_previous_month("date") }} + and {{ is_date_before_20th_of_previous_month("date") }} ) -- Keep all history for the rest of metrics or not