Merged PR 4232: Update rule for metric depends on invoicing

# Description

Update rule for when to display metrics that depend on invoicing cycle.
Now they will show previous month values from the 20th onward of the current 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.
- [x] I have checked for DRY opportunities with other models and docs.
- [x] I've picked the right materialization for the affected models.

# Other

- [ ] Check if a full-refresh is required after this PR is merged.

Update rule for metric depends on invoicing

Related work items: #27180
This commit is contained in:
Joaquin Ossa 2025-01-31 08:32:35 +00:00
commit d793f610c9
4 changed files with 58 additions and 48 deletions

View file

@ -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 %}

View file

@ -1,18 +0,0 @@
/*
This macro provides a boolean answer to the question:
- Is this date before the previous month?
The computation is based on the current date by using now()
Inputs:
- date: the date from which you want to check
Output:
- boolean; true for is before the previous month
false for is not before the previous month
*/
{% macro is_date_before_previous_month(date) %}
(
date_trunc('month', ({{ date }})::date)::date
+ interval '1 month'
)::date
< date_trunc('month', now())::date
{% endmacro %}

View file

@ -32,32 +32,32 @@ select
listings_booked_in_12_months as listings_booked_in_12_months,
-- Avoid displaying revenue figures until invoicing period finishes
case
when {{ is_date_before_previous_month("date") }}
when {{ is_date_before_20th_of_previous_month("date") }}
then total_revenue_in_gbp
else null
end as total_revenue_in_gbp,
case
when {{ is_date_before_previous_month("date") }}
when {{ is_date_before_20th_of_previous_month("date") }}
then total_revenue_per_created_booking
else null
end as total_revenue_per_created_booking,
case
when {{ is_date_before_previous_month("date") }}
when {{ is_date_before_20th_of_previous_month("date") }}
then total_revenue_per_created_guest_journey
else null
end as total_revenue_per_created_guest_journey,
case
when {{ is_date_before_previous_month("date") }}
when {{ is_date_before_20th_of_previous_month("date") }}
then total_revenue_per_listings_booked_in_month
else null
end as total_revenue_per_listings_booked_in_month,
case
when {{ is_date_before_previous_month("date") }}
when {{ is_date_before_20th_of_previous_month("date") }}
then xero_operator_net_fees_in_gbp
else null
end as xero_operator_net_fees_in_gbp,
case
when {{ is_date_before_previous_month("date") }}
when {{ is_date_before_20th_of_previous_month("date") }}
then xero_apis_net_fees_in_gbp
else null
end as xero_apis_net_fees_in_gbp,
@ -66,92 +66,92 @@ select
as guest_revenue_per_completed_guest_journey,
guest_payments_per_paid_guest_journey as guest_revenue_per_paid_guest_journey,
case
when {{ is_date_before_previous_month("date") }}
when {{ is_date_before_20th_of_previous_month("date") }}
then xero_host_resolution_amount_paid_in_gbp
else null
end as xero_host_resolution_amount_paid_in_gbp,
case
when {{ is_date_before_previous_month("date") }}
when {{ is_date_before_20th_of_previous_month("date") }}
then xero_host_resolution_payment_count
else null
end as xero_host_resolution_payment_count,
case
when {{ is_date_before_previous_month("date") }}
when {{ is_date_before_20th_of_previous_month("date") }}
then host_resolution_amount_paid_per_created_booking
else null
end as host_resolution_amount_paid_per_created_booking,
case
when {{ is_date_before_previous_month("date") }}
when {{ is_date_before_20th_of_previous_month("date") }}
then host_resolution_payment_per_created_booking_ratio
else null
end as host_resolution_payment_per_created_booking_ratio,
case
when {{ is_date_before_previous_month("date") }}
when {{ is_date_before_20th_of_previous_month("date") }}
then xero_booking_net_fees_in_gbp
else null
end as xero_booking_net_fees_in_gbp,
case
when {{ is_date_before_previous_month("date") }}
when {{ is_date_before_20th_of_previous_month("date") }}
then xero_listing_net_fees_in_gbp
else null
end as xero_listing_net_fees_in_gbp,
case
when {{ is_date_before_previous_month("date") }}
when {{ is_date_before_20th_of_previous_month("date") }}
then xero_verification_net_fees_in_gbp
else null
end as xero_verification_net_fees_in_gbp,
case
when {{ is_date_before_previous_month("date") }}
when {{ is_date_before_20th_of_previous_month("date") }}
then xero_basic_protection_net_fees_in_gbp
else null
end as xero_basic_protection_net_fees_in_gbp,
case
when {{ is_date_before_previous_month("date") }}
when {{ is_date_before_20th_of_previous_month("date") }}
then xero_waiver_pro_net_fees_in_gbp
else null
end as xero_waiver_pro_net_fees_in_gbp,
case
when {{ is_date_before_previous_month("date") }}
when {{ is_date_before_20th_of_previous_month("date") }}
then xero_id_verification_net_fees_in_gbp
else null
end as xero_id_verification_net_fees_in_gbp,
case
when {{ is_date_before_previous_month("date") }}
when {{ is_date_before_20th_of_previous_month("date") }}
then xero_protection_plus_net_fees_in_gbp
else null
end as xero_protection_plus_net_fees_in_gbp,
case
when {{ is_date_before_previous_month("date") }}
when {{ is_date_before_20th_of_previous_month("date") }}
then xero_screening_plus_net_fees_in_gbp
else null
end as xero_screening_plus_net_fees_in_gbp,
case
when {{ is_date_before_previous_month("date") }}
when {{ is_date_before_20th_of_previous_month("date") }}
then xero_sex_offenders_check_net_fees_in_gbp
else null
end as xero_sex_offenders_check_net_fees_in_gbp,
case
when {{ is_date_before_previous_month("date") }}
when {{ is_date_before_20th_of_previous_month("date") }}
then xero_protection_pro_net_fees_in_gbp
else null
end as xero_protection_pro_net_fees_in_gbp,
case
when {{ is_date_before_previous_month("date") }}
when {{ is_date_before_20th_of_previous_month("date") }}
then xero_guesty_net_fees_in_gbp
else null
end as xero_guesty_net_fees_in_gbp,
case
when {{ is_date_before_previous_month("date") }}
when {{ is_date_before_20th_of_previous_month("date") }}
then xero_e_deposit_net_fees_in_gbp
else null
end as xero_e_deposit_net_fees_in_gbp,
case
when {{ is_date_before_previous_month("date") }}
when {{ is_date_before_20th_of_previous_month("date") }}
then xero_waiver_paid_back_to_host_in_gbp
else null
end as xero_waiver_paid_back_to_host_in_gbp,
case
when {{ is_date_before_previous_month("date") }}
when {{ is_date_before_20th_of_previous_month("date") }}
then waiver_net_fees_in_gbp
else null
end as waiver_net_fees_in_gbp,
@ -159,22 +159,22 @@ select
waiver_payments_in_gbp as waiver_payments_in_gbp,
checkin_cover_fees_in_gbp as checkin_cover_fees_in_gbp,
case
when {{ is_date_before_previous_month("date") }}
when {{ is_date_before_20th_of_previous_month("date") }}
then revenue_retained_in_gbp
else null
end as revenue_retained_in_gbp,
case
when {{ is_date_before_previous_month("date") }}
when {{ is_date_before_20th_of_previous_month("date") }}
then revenue_retained_ratio
else null
end as revenue_retained_ratio,
case
when {{ is_date_before_previous_month("date") }}
when {{ is_date_before_20th_of_previous_month("date") }}
then revenue_retained_post_resolutions_in_gbp
else null
end as revenue_retained_post_resolutions_in_gbp,
case
when {{ is_date_before_previous_month("date") }}
when {{ is_date_before_20th_of_previous_month("date") }}
then revenue_retained_post_resolutions_ratio
else null
end as revenue_retained_post_resolutions_ratio

View file

@ -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%'
@ -70,7 +71,7 @@ where
or lower(metric) like '%retained%'
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