From 396d19b549a4a2417354e1f9a9da5f110bf70900 Mon Sep 17 00:00:00 2001 From: Joaquin Ossa Date: Thu, 30 Jan 2025 15:43:53 +0100 Subject: [PATCH] changed exclusion rule for metrics depending on invoicing --- ...is_date_before_previous_month_fixed_20.sql | 30 +++++++++++++++++++ .../general/mtd_aggregated_metrics.sql | 4 +-- 2 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 macros/is_date_before_previous_month_fixed_20.sql diff --git a/macros/is_date_before_previous_month_fixed_20.sql b/macros/is_date_before_previous_month_fixed_20.sql new file mode 100644 index 0000000..e66810e --- /dev/null +++ b/macros/is_date_before_previous_month_fixed_20.sql @@ -0,0 +1,30 @@ +/* +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 %} diff --git a/models/reporting/general/mtd_aggregated_metrics.sql b/models/reporting/general/mtd_aggregated_metrics.sql index ec0e853..bc3f1fb 100644 --- a/models/reporting/general/mtd_aggregated_metrics.sql +++ b/models/reporting/general/mtd_aggregated_metrics.sql @@ -70,7 +70,7 @@ where or lower(metric) like '%retained%' or lower(metric) like '%damage host%' ) - and {{ is_date_before_previous_month("date") }} + and {{ is_date_before_previous_month_fixed_20("date") }} ) -- Not show current month if the metric is Expected MRR, unlike other -- revenue metrics, Expected MRR is calculated for the next month, so it is not @@ -78,7 +78,7 @@ where or not ( lower(metric) like '%mrr%' - and date_trunc('month', m.date)::date = date_trunc('month', now())::date + and date_trunc('month', date)::date = date_trunc('month', now())::date ) -- Keep all history for the rest of metrics or not