-
Notifications
You must be signed in to change notification settings - Fork 0
Create dim dates #37
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Create dim dates #37
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
6d23e0e
adding a dim_date table and tests
rosie-taylor 7207baf
adding a dim_date table that's not macro depdendent
rosie-taylor d69f839
adding better ISO logic, a date key column and an additional singular…
rosie-taylor af75d74
adding the singular tests
rosie-taylor File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| {{ | ||
| config( | ||
| materialized = "table" | ||
| ) | ||
| }} | ||
|
|
||
| ## What dates to start and end from, can be adjusted easily ## | ||
|
|
||
| with parameters as ( | ||
|
|
||
| select | ||
| date('{{ var("dim_date_start", "2023-01-01") }}') as start_date, | ||
| date('{{ var("dim_date_end", "2050-12-31") }}') as end_date | ||
|
|
||
| ), | ||
|
|
||
| ## One row per day ## | ||
|
|
||
| date_spine as ( | ||
|
|
||
| select | ||
| date_day | ||
| from parameters, | ||
| unnest(generate_date_array(start_date, end_date)) as date_day | ||
|
|
||
| ) | ||
|
|
||
| select | ||
| -- date key | ||
| cast(format_date('%Y%m%d', date_day) as int64) as date_key, | ||
|
|
||
|
rosie-t marked this conversation as resolved.
|
||
| -- day information | ||
| date_day, | ||
| extract(dayofweek from date_day) as day_of_week, | ||
| format_date('%A', date_day) as day_of_week_name, | ||
| case | ||
| when extract(dayofweek from date_day) in (1, 7) then true | ||
| else false | ||
| end as is_weekend, | ||
| extract(day from date_day) as day_of_month, | ||
| extract(dayofyear from date_day) as day_of_year, | ||
|
|
||
| -- week (ISO) information | ||
| extract(isoyear from date_day) as iso_year, | ||
| extract(isoweek from date_day) as iso_week_of_year, | ||
| date_trunc(date_day, week(monday)) as week_start_date, | ||
| date_add(date_trunc(date_day, week(monday)), interval 6 day) as week_end_date, | ||
|
|
||
| -- month information | ||
| format_date('%B', date_day) as month_name, | ||
| extract(month from date_day) as month_of_year, | ||
| date_trunc(date_day, month) as month_start_date, | ||
| date_sub(date_add(date_trunc(date_day, month), interval 1 month), interval 1 day) as month_end_date, | ||
|
|
||
| -- quarter information | ||
| extract(quarter from date_day) as quarter_of_year, | ||
| date_trunc(date_day, quarter) as quarter_start_date, | ||
| date_sub(date_add(date_trunc(date_day, quarter), interval 3 month), interval 1 day) as quarter_end_date, | ||
|
|
||
| -- year (calendar) information | ||
| extract(year from date_day) as year_number, | ||
| date_trunc(date_day, year) as year_start_date, | ||
| date_sub(date_add(date_trunc(date_day, year), interval 1 year), interval 1 day) as year_end_date | ||
|
|
||
| from date_spine | ||
| order by date_day | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| version: 2 | ||
|
|
||
|
rosie-t marked this conversation as resolved.
|
||
| models: | ||
| - name: dim_date | ||
| description: Date dimension table with one row per day and useful calendar attributes. | ||
|
|
||
| columns: | ||
| - name: date_key | ||
| description: Integer date key in YYYYMMDD format. | ||
| tests: | ||
| - not_null | ||
| - unique | ||
|
|
||
| - name: date_day | ||
| description: The calendar date. | ||
| tests: | ||
| - not_null | ||
| - unique | ||
|
|
||
| - name: day_of_week | ||
| description: Day of week number where 1 = Sunday and 7 = Saturday. | ||
| tests: | ||
| - not_null | ||
| - accepted_values: | ||
| values: [1, 2, 3, 4, 5, 6, 7] | ||
| quote: false | ||
|
|
||
| - name: day_of_week_name | ||
| description: Name of the day of the week. | ||
| tests: | ||
| - not_null | ||
| - accepted_values: | ||
| values: ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'] | ||
|
|
||
| - name: is_weekend | ||
| description: True when the date falls on Saturday or Sunday. | ||
| tests: | ||
| - not_null | ||
| - accepted_values: | ||
| values: [true, false] | ||
| quote: false | ||
|
|
||
| - name: day_of_month | ||
| description: Day number within the month. | ||
| tests: | ||
| - not_null | ||
|
|
||
| - name: day_of_year | ||
| description: Day number within the year. | ||
| tests: | ||
| - not_null | ||
|
|
||
| - name: iso_year | ||
| description: ISO week-numbering year. | ||
| tests: | ||
| - not_null | ||
|
|
||
| - name: iso_week_of_year | ||
| description: ISO week number within the ISO year. | ||
| tests: | ||
| - not_null | ||
|
|
||
| - name: week_start_date | ||
| description: Start date of the ISO week, beginning on Monday. | ||
| tests: | ||
| - not_null | ||
|
|
||
| - name: week_end_date | ||
| description: End date of the ISO week, ending on Sunday. | ||
| tests: | ||
| - not_null | ||
|
|
||
| - name: month_name | ||
| description: Name of the month. | ||
| tests: | ||
| - not_null | ||
| - accepted_values: | ||
| values: ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'] | ||
|
|
||
| - name: month_of_year | ||
| description: Month number within the year. | ||
| tests: | ||
| - not_null | ||
| - accepted_values: | ||
| values: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12] | ||
| quote: false | ||
|
|
||
| - name: month_start_date | ||
| description: First date of the month. | ||
| tests: | ||
| - not_null | ||
|
|
||
| - name: month_end_date | ||
| description: Last date of the month. | ||
| tests: | ||
| - not_null | ||
|
|
||
| - name: quarter_of_year | ||
| description: Quarter number within the year. | ||
| tests: | ||
| - not_null | ||
| - accepted_values: | ||
| values: [1, 2, 3, 4] | ||
| quote: false | ||
|
|
||
| - name: quarter_start_date | ||
| description: First date of the quarter. | ||
| tests: | ||
| - not_null | ||
|
|
||
| - name: quarter_end_date | ||
| description: Last date of the quarter. | ||
| tests: | ||
| - not_null | ||
|
|
||
| - name: year_number | ||
| description: Calendar year. | ||
| tests: | ||
| - not_null | ||
|
|
||
| - name: year_start_date | ||
| description: First date of the year. | ||
| tests: | ||
| - not_null | ||
|
|
||
| - name: year_end_date | ||
| description: Last date of the year. | ||
| tests: | ||
| - not_null | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| select * | ||
| from {{ ref('dim_date') }} | ||
| where (day_of_week in (1, 7) and is_weekend != true) | ||
| or (day_of_week not in (1, 7) and is_weekend != false) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| select * | ||
| from {{ ref('dim_date') }} | ||
| where | ||
| ( | ||
| date_day = '2021-01-01' | ||
| and not (iso_year = 2020 and iso_week_of_year = 53 and year_number = 2021) | ||
| ) | ||
| or | ||
| ( | ||
| date_day = '2021-01-04' | ||
| and not (iso_year = 2021 and iso_week_of_year = 1 and year_number = 2021) | ||
| ) | ||
| or | ||
| ( | ||
| date_day = '2020-12-31' | ||
| and not (iso_year = 2020 and iso_week_of_year = 53 and year_number = 2020) | ||
| ) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.