Skip to content

Commit

Permalink
Merge pull request #6 from calogica/feature/add-dynamic-date-support-…
Browse files Browse the repository at this point in the history
…to-get_base_dates

Update base date logic to allow to dynamic date ranges
  • Loading branch information
clausherther authored Oct 26, 2020
2 parents 714a51b + 688e73e commit 5f24102
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions macros/get_base_dates.sql
Original file line number Diff line number Diff line change
@@ -1,15 +1,26 @@
{% macro get_base_dates(start_date, end_date) %}
{% macro get_base_dates(start_date=None, end_date=None, n_dateparts=None, datepart=None) %}
with date_spine as
(
{% if start_date and end_date %}
{%- set start_date="cast('" ~ start_date ~ "' as datetime)" -%}
{%- set end_date="cast('" ~ end_date ~ "' as datetime)" -%}

{% elif n_dateparts and datepart %}

{%- set start_date = dbt_utils.dateadd(datepart, -1 * n_dateparts, dbt_date.today()) -%}
{%- set end_date = dbt_date.tomorrow() -%}
{% endif %}

{{ dbt_utils.date_spine(
datepart="day",
start_date="cast('" ~ start_date ~ "' as datetime)",
end_date="cast('" ~ end_date ~ "' as datetime)",
start_date=start_date,
end_date=end_date,
)
}}

)
select
d.date_day
date(d.date_day) as date_day
from
date_spine d
{% endmacro %}

0 comments on commit 5f24102

Please sign in to comment.