-
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
Create dim dates #37
Changes from 2 commits
6d23e0e
7207baf
d69f839
af75d74
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| {{ | ||
| config( | ||
| materialized = "table" | ||
| ) | ||
| }} | ||
|
|
||
| ## What dates to start and end from, can be adjusted easily ## | ||
|
|
||
| with parameters as ( | ||
|
|
||
| select | ||
| date('2023-01-01') as start_date, | ||
| date('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 | ||
|
|
||
|
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 information ## | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This looks good overall. One thing I’d flag is that we are mixing ISO week logic with calendar year logic here.
I think we should either add an |
||
| 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, | ||
| extract(isoweek from date_day) as week_of_year, | ||
|
|
||
| ## 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 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 | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
| 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_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: week_start_date | ||
| description: Start date of the week. | ||
| tests: | ||
| - not_null | ||
|
|
||
| - name: week_end_date | ||
| description: End date of the week. | ||
| tests: | ||
| - not_null | ||
|
|
||
| - name: week_of_year | ||
| description: Week number within the year. | ||
| 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 | ||
Uh oh!
There was an error while loading. Please reload this page.