Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions models/marts/dim_date.sql
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

Comment thread
rosie-t marked this conversation as resolved.
),

## One row per day ##

date_spine as (

select
date_day
from parameters,
unnest(generate_date_array(start_date, end_date)) as date_day

)

select

Comment thread
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 ##

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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.

week_of_year uses extract(isoweek ...), but year_number uses the calendar year. Around year boundaries that can produce confusing combinations, for example ISO week 1 paired with the previous calendar year, or vice versa.

I think we should either add an iso_year field to keep the week logic consistent, or switch week_of_year to a non-ISO definition if we want this dimension to stay purely calendar-based.

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
118 changes: 118 additions & 0 deletions models/marts/dim_date.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
version: 2

Comment thread
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
Loading