Skip to content

Commit

Permalink
feat: implement diff_weekdays
Browse files Browse the repository at this point in the history
  • Loading branch information
cverhoef authored and KosmasH committed Mar 28, 2024
1 parent 262fe3d commit f8e5d98
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Expand Down
27 changes: 27 additions & 0 deletions integration_tests/models/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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`'
25 changes: 25 additions & 0 deletions integration_tests/models/test_diff_weekdays.sql
Original file line number Diff line number Diff line change
@@ -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`
11 changes: 11 additions & 0 deletions macros/multiple_databases/diff_weekdays.sql
Original file line number Diff line number Diff line change
@@ -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 -%}

0 comments on commit f8e5d98

Please sign in to comment.