diff --git a/README.md b/README.md index 5d97507..ee58b66 100644 --- a/README.md +++ b/README.md @@ -106,6 +106,12 @@ This macro computes the difference between two date or datetime expressions base Usage: `{{ pm_utils.datediff('[datepart]', '[start_date_expression]', '[end_date_expression]') }}` +#### diff_weekdays ([source](macros/multiple_databases/diff_weekdays.sql)) +This macro computes the number of days between a start and end date. It returns one day when the start and end date are on the same date. The Saturdays and Sundays are excluded from the number of days. + +Usage: +`{{ pm_utils.diff_weekdays('[start_date_expression]', '[end_date_expression]') }}` + #### id ([source](macros/multiple_databases/id.sql)) This macro generates an id field that can be used as a column for the current model. diff --git a/integration_tests/models/schema.yml b/integration_tests/models/schema.yml index c6887f2..fbadbb8 100644 --- a/integration_tests/models/schema.yml +++ b/integration_tests/models/schema.yml @@ -144,3 +144,30 @@ models: - equal_value: actual: '`Millisecond`' expected: '`Millisecond_expected`' + + - name: test_diff_weekdays + tests: + - equal_value: + actual: '`Two_weekends`' + expected: '`Two_weekends_expected`' + - equal_value: + actual: '`One_half_weekends`' + expected: '`One_half_weekends_expected`' + - equal_value: + actual: '`One_weekend`' + expected: '`One_weekend_expected`' + - equal_value: + actual: '`One_weekend_end`' + expected: '`One_weekend_end_expected`' + - equal_value: + actual: '`Half_weekend`' + expected: '`Half_weekend_expected`' + - equal_value: + actual: '`Start_weekend`' + expected: '`Start_weekend_expected`' + - equal_value: + actual: '`Start_half_weekend`' + expected: '`Start_half_weekend_expected`' + - equal_value: + actual: '`Only_weekend`' + expected: '`Only_weekend_expected`' diff --git a/integration_tests/models/test_diff_weekdays.sql b/integration_tests/models/test_diff_weekdays.sql new file mode 100644 index 0000000..8a7709b --- /dev/null +++ b/integration_tests/models/test_diff_weekdays.sql @@ -0,0 +1,25 @@ +select + {# Compute difference for different use cases where the complete and half of the weekend falls in the date range. #} + {{ pm_utils.diff_weekdays("'2024-03-14'", "'2024-03-25'") }} as `Two_weekends`, + 8 as `Two_weekends_expected`, + + {{ pm_utils.diff_weekdays("'2024-03-14'", "'2024-03-23'") }} as `One_half_weekends`, + 7 as `One_half_weekends_expected`, + + {{ pm_utils.diff_weekdays("'2024-03-21'", "'2024-03-25'") }} as `One_weekend`, + 3 as `One_weekend_expected`, + + {{ pm_utils.diff_weekdays("'2024-03-21'", "'2024-03-24'") }} as `One_weekend_end`, + 2 as `One_weekend_end_expected`, + + {{ pm_utils.diff_weekdays("'2024-03-21'", "'2024-03-23'") }} as `Half_weekend`, + 2 as `Half_weekend_expected`, + + {{ pm_utils.diff_weekdays("'2024-03-16'", "'2024-03-19'") }} as `Start_weekend`, + 2 as `Start_weekend_expected`, + + {{ pm_utils.diff_weekdays("'2024-03-17'", "'2024-03-19'") }} as `Start_half_weekend`, + 2 as `Start_half_weekend_expected`, + + {{ pm_utils.diff_weekdays("'2024-03-16'", "'2024-03-17'") }} as `Only_weekend`, + 0 as `Only_weekend_expected` diff --git a/macros/multiple_databases/diff_weekdays.sql b/macros/multiple_databases/diff_weekdays.sql new file mode 100644 index 0000000..9569567 --- /dev/null +++ b/macros/multiple_databases/diff_weekdays.sql @@ -0,0 +1,11 @@ +{%- macro diff_weekdays(start_date_field, end_date_field) -%} + +{# Take two dates as input and compute the total number of days between the two dates. Also count it as one day in case the "from date" and the "to date" are on the same day. +From this total number of days, we subtract the weekend days (Saturday and Sunday). We use the function datediff with parameter week. +This function returns 1 for every complete week, where a week is defined from Sunday to Saturday. +Since the function only returns full weeks, we need to adjust the parameters to account for date ranges that start on Sunday or end at Saturday. #} +{{ pm_utils.datediff('day', start_date_field, end_date_field) }} + 1 +- {{ pm_utils.datediff('week', start_date_field, pm_utils.dateadd('day', 1, end_date_field)) }} +- {{ pm_utils.datediff('week', pm_utils.dateadd('day', -1, start_date_field), end_date_field) }} + +{%- endmacro -%}