Skip to content

Commit

Permalink
Merge pull request #30 from calogica/fix/get-base-dates-datepart
Browse files Browse the repository at this point in the history
Add support for date parts in get_base_dates
  • Loading branch information
clausherther authored May 31, 2021
2 parents 8de32b0 + 409fa2e commit f291dd6
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 19 deletions.
4 changes: 0 additions & 4 deletions dbt_project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,3 @@ quoting:
vars:
'dbt_date:time_zone': 'America/Los_Angeles'

models:
dbt_date:
example_models:
enabled: false
6 changes: 6 additions & 0 deletions integration_tests/models/dates.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{{
config(
materialized = "ephemeral"
)
}}
{{ dbt_date.get_date_dimension('2015-01-01', '2022-12-31') }}
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
{{
config(
materialized = "table"
)
}}
with date_dimension as (
select * from {{ ref('dates') }}
),
Expand All @@ -6,7 +11,7 @@ fiscal_periods as (
)
select
d.*,
f.fiscal_week_of_year,
f.fiscal_week_of_year,
f.fiscal_week_of_period,
f.fiscal_period_number,
f.fiscal_quarter_number,
Expand All @@ -15,4 +20,4 @@ from
date_dimension d
left outer join
fiscal_periods f
on d.date_day = f.date_day
on d.date_day = f.date_day
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
{{
config(
materialized = "table"
)
}}
with fp as (
{{ dbt_date.get_fiscal_periods(ref('dates'), year_end_month=1, week_start_day=1) }}
)
select
f.*
from
fp f
fp f
12 changes: 12 additions & 0 deletions integration_tests/models/dim_hour.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{{
config(
materialized = "table"
)
}}
with periods_hours as (
{{ dbt_date.get_base_dates(n_dateparts=24*28, datepart="hour") }}
)
select
d.*
from
periods_hours d
12 changes: 12 additions & 0 deletions integration_tests/models/dim_week.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{{
config(
materialized = "table"
)
}}
with periods_weeks as (
{{ dbt_date.get_base_dates(n_dateparts=52, datepart="week") }}
)
select
d.*
from
periods_weeks d
10 changes: 5 additions & 5 deletions macros/get_base_dates.sql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{% macro get_base_dates(start_date=None, end_date=None, n_dateparts=None, datepart=None) %}
{% macro get_base_dates(start_date=None, end_date=None, n_dateparts=None, datepart="day") %}
{{ adapter.dispatch('get_base_dates', packages = dbt_date._get_utils_namespaces()) (start_date, end_date, n_dateparts, datepart) }}
{% endmacro %}

Expand All @@ -16,15 +16,15 @@ with date_spine as
{% endif %}

{{ dbt_utils.date_spine(
datepart="day",
datepart=datepart,
start_date=start_date,
end_date=end_date,
)
}}

)
select
cast(d.date_day as date) as date_day
cast(d.date_{{ datepart }} as {{ dbt_utils.type_timestamp() }}) as date_{{ datepart }}
from
date_spine d
{% endmacro %}
Expand All @@ -43,15 +43,15 @@ with date_spine as
{% endif %}

{{ dbt_utils.date_spine(
datepart="day",
datepart=datepart,
start_date=start_date,
end_date=end_date,
)
}}

)
select
cast(d.date_day as date) as date_day
cast(d.date_{{ datepart }} as {{ dbt_utils.type_timestamp() }}) as date_{{ datepart }}
from
date_spine d
{% endmacro %}
7 changes: 0 additions & 7 deletions models/example_models/dates.sql

This file was deleted.

0 comments on commit f291dd6

Please sign in to comment.