Fixed macro
This commit is contained in:
parent
e4d19b85e8
commit
720185f235
3 changed files with 68 additions and 56 deletions
|
|
@ -10,33 +10,8 @@ It ensure safe divide by zero division by applying a nullif function.
|
||||||
{% macro calculate_safe_relative_increment(
|
{% macro calculate_safe_relative_increment(
|
||||||
metric, current="current", previous="previous_year"
|
metric, current="current", previous="previous_year"
|
||||||
) %}
|
) %}
|
||||||
-- Metrics that need to be capped between -1 and 1
|
{{ current }}.{{ metric }},
|
||||||
{% set capped_metrics = [
|
{{ previous }}.{{ metric }} as {{ previous }}_{{ metric }},
|
||||||
"host_resolution_payment_per_created_booking_ratio",
|
cast({{ current }}.{{ metric }} as decimal) / nullif({{ previous }}.{{ metric }}, 0)
|
||||||
"revenue_retained_post_resolutions_ratio",
|
- 1 as relative_increment_{{ metric }}
|
||||||
"revenue_retained_ratio",
|
|
||||||
] %}
|
|
||||||
{% if metric in capped_metrics %}
|
|
||||||
-- Cap current and previous values between -1 and 1
|
|
||||||
case
|
|
||||||
when {{ current }}.{{ metric }} is null
|
|
||||||
then null
|
|
||||||
else least(1, greatest(-1, {{ current }}.{{ metric }}))
|
|
||||||
end as {{ metric }},
|
|
||||||
case
|
|
||||||
when {{ previous }}.{{ metric }} is null
|
|
||||||
then null
|
|
||||||
else least(1, greatest(-1, {{ previous }}.{{ metric }}))
|
|
||||||
end as {{ previous }}_{{ metric }},
|
|
||||||
cast(least(1, greatest(-1, {{ current }}.{{ metric }})) as decimal)
|
|
||||||
/ nullif(least(1, greatest(-1, {{ previous }}.{{ metric }})), 0)
|
|
||||||
- 1 as relative_increment_{{ metric }}
|
|
||||||
{% else %}
|
|
||||||
-- No capping applied, default behavior
|
|
||||||
{{ current }}.{{ metric }},
|
|
||||||
{{ previous }}.{{ metric }} as {{ previous }}_{{ metric }},
|
|
||||||
cast({{ current }}.{{ metric }} as decimal)
|
|
||||||
/ nullif({{ previous }}.{{ metric }}, 0)
|
|
||||||
- 1 as relative_increment_{{ metric }}
|
|
||||||
{% endif %}
|
|
||||||
{% endmacro %}
|
{% endmacro %}
|
||||||
|
|
|
||||||
20
macros/return_capped_value.sql
Normal file
20
macros/return_capped_value.sql
Normal file
|
|
@ -0,0 +1,20 @@
|
||||||
|
/*
|
||||||
|
This macro caps a given value between a specified bottom and top limit,
|
||||||
|
returning `NULL` if the input value is `NULL`.
|
||||||
|
|
||||||
|
It uses the `LEAST` and `GREATEST` SQL functions to enforce the caps while
|
||||||
|
preserving the `NULL` values in the input.
|
||||||
|
|
||||||
|
Parameters:
|
||||||
|
- `value`: The value to be capped.
|
||||||
|
- `cap_bottom`: The minimum limit for the value.
|
||||||
|
- `cap_top`: The maximum limit for the value.
|
||||||
|
|
||||||
|
*/
|
||||||
|
{% macro return_capped_value(value, cap_bottom, cap_top) %}
|
||||||
|
CASE
|
||||||
|
WHEN {{ value }} IS NULL THEN NULL
|
||||||
|
ELSE LEAST({{ cap_top }}, GREATEST({{ cap_bottom }}, {{ value }}))
|
||||||
|
END
|
||||||
|
{% endmacro %}
|
||||||
|
|
||||||
|
|
@ -289,8 +289,14 @@ with
|
||||||
cast(host_resolutions.xero_host_resolution_amount_paid_in_gbp as decimal)
|
cast(host_resolutions.xero_host_resolution_amount_paid_in_gbp as decimal)
|
||||||
/ created_bookings.created_bookings
|
/ created_bookings.created_bookings
|
||||||
as host_resolution_amount_paid_per_created_booking,
|
as host_resolution_amount_paid_per_created_booking,
|
||||||
cast(host_resolutions.xero_host_resolution_payment_count as decimal)
|
{{
|
||||||
/ created_bookings.created_bookings
|
return_capped_value(
|
||||||
|
"cast(host_resolutions.xero_host_resolution_payment_count as decimal)
|
||||||
|
/ created_bookings.created_bookings",
|
||||||
|
-1,
|
||||||
|
1
|
||||||
|
)
|
||||||
|
}}
|
||||||
as host_resolution_payment_per_created_booking_ratio,
|
as host_resolution_payment_per_created_booking_ratio,
|
||||||
|
|
||||||
-- GUEST REVENUE AND PAYMENTS --
|
-- GUEST REVENUE AND PAYMENTS --
|
||||||
|
|
@ -364,31 +370,42 @@ with
|
||||||
+ coalesce(invoiced_revenue.xero_waiver_paid_back_to_host_in_gbp, 0),
|
+ coalesce(invoiced_revenue.xero_waiver_paid_back_to_host_in_gbp, 0),
|
||||||
0
|
0
|
||||||
) as revenue_retained_in_gbp,
|
) as revenue_retained_in_gbp,
|
||||||
nullif(
|
{{
|
||||||
coalesce(guest_payments.total_guest_payments_in_gbp, 0)
|
return_capped_value(
|
||||||
+ coalesce(invoiced_revenue.xero_operator_net_fees_in_gbp, 0)
|
"nullif(
|
||||||
+ coalesce(invoiced_revenue.xero_apis_net_fees_in_gbp, 0)
|
coalesce(guest_payments.total_guest_payments_in_gbp, 0)
|
||||||
+ coalesce(invoiced_revenue.xero_waiver_paid_back_to_host_in_gbp, 0),
|
+ coalesce(invoiced_revenue.xero_operator_net_fees_in_gbp, 0)
|
||||||
0
|
+ coalesce(invoiced_revenue.xero_apis_net_fees_in_gbp, 0)
|
||||||
) / nullif(
|
+ coalesce(invoiced_revenue.xero_waiver_paid_back_to_host_in_gbp, 0),
|
||||||
coalesce(guest_payments.total_guest_payments_in_gbp, 0)
|
0
|
||||||
+ coalesce(invoiced_revenue.xero_operator_net_fees_in_gbp, 0)
|
) / nullif(
|
||||||
+ coalesce(invoiced_revenue.xero_apis_net_fees_in_gbp, 0),
|
coalesce(guest_payments.total_guest_payments_in_gbp, 0)
|
||||||
0
|
+ coalesce(invoiced_revenue.xero_operator_net_fees_in_gbp, 0)
|
||||||
) as revenue_retained_ratio,
|
+ coalesce(invoiced_revenue.xero_apis_net_fees_in_gbp, 0),
|
||||||
nullif(
|
0
|
||||||
coalesce(guest_payments.total_guest_payments_in_gbp, 0)
|
)",
|
||||||
+ coalesce(invoiced_revenue.xero_operator_net_fees_in_gbp, 0)
|
-1,
|
||||||
+ coalesce(invoiced_revenue.xero_apis_net_fees_in_gbp, 0)
|
1
|
||||||
+ coalesce(invoiced_revenue.xero_waiver_paid_back_to_host_in_gbp, 0)
|
)}} as revenue_retained_ratio,
|
||||||
+ coalesce(host_resolutions.xero_host_resolution_amount_paid_in_gbp, 0),
|
{{
|
||||||
0
|
return_capped_value(
|
||||||
) / nullif(
|
"nullif(
|
||||||
coalesce(guest_payments.total_guest_payments_in_gbp, 0)
|
coalesce(guest_payments.total_guest_payments_in_gbp, 0)
|
||||||
+ coalesce(invoiced_revenue.xero_operator_net_fees_in_gbp, 0)
|
+ coalesce(invoiced_revenue.xero_operator_net_fees_in_gbp, 0)
|
||||||
+ coalesce(invoiced_revenue.xero_apis_net_fees_in_gbp, 0),
|
+ coalesce(invoiced_revenue.xero_apis_net_fees_in_gbp, 0)
|
||||||
0
|
+ coalesce(invoiced_revenue.xero_waiver_paid_back_to_host_in_gbp, 0)
|
||||||
) as revenue_retained_post_resolutions_ratio,
|
+ coalesce(host_resolutions.xero_host_resolution_amount_paid_in_gbp, 0),
|
||||||
|
0
|
||||||
|
) / nullif(
|
||||||
|
coalesce(guest_payments.total_guest_payments_in_gbp, 0)
|
||||||
|
+ coalesce(invoiced_revenue.xero_operator_net_fees_in_gbp, 0)
|
||||||
|
+ coalesce(invoiced_revenue.xero_apis_net_fees_in_gbp, 0),
|
||||||
|
0
|
||||||
|
)",
|
||||||
|
-1,
|
||||||
|
1
|
||||||
|
)
|
||||||
|
}} as revenue_retained_post_resolutions_ratio,
|
||||||
|
|
||||||
-- INCOME RETAINED POST RESOLUTIONS--
|
-- INCOME RETAINED POST RESOLUTIONS--
|
||||||
nullif(
|
nullif(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue