Setting dbt date variable
Asked Answered
dbt
C

1

6

I am trying to set a date variable within a dbt model to be the date 7 days ago. The model will run against a Redshift database. I have done the following to set the variable, however I get the error DATE_ADD is not defined:

{%- set start_date = TRUNC(DATE_ADD(day, -7, CURRENT_DATE)) -%}

What is the correct way to set the variable.

Cardie answered 19/1, 2023 at 16:42 Comment(0)
G
7

jinja is a templating language. When you run dbt, it first executes the jinja to "compile" your model, and then it executes your compiled code against your database.

jinja doesn't have functions called trunc or date_add or current_date, since those are SQL functions.

You have two choices:

  1. Set the variable equal to a string and include that string in your model code, so that the database calculates this date. That would look like this (note the extra quotes):

    {%- set start_date = "TRUNC(DATE_ADD(day, -7, CURRENT_DATE))" -%}
    select {{ start_date }}
    

    If you compile this and check the code generated in your target directory, you'll see it becomes this SQL:

    select TRUNC(DATE_ADD(day, -7, CURRENT_DATE))
    
  2. Use jinja's context to calculate the date and include the date literal in your SQL. dbt's jinja context includes a special variable called run_started_at, and also Python's datetime module. Putting those together looks like this:

    {%- set start_datetime = run_started_at - modules.datetime.timedelta(days=7) -%}
    {%- set start_date = start_datetime.strftime("%Y-%m-%d") -%}
    select '{{ start_date }}'
    

    This will compile to:

    select '2023-01-12'
    
Googins answered 19/1, 2023 at 17:0 Comment(1)
Thanks. Both of the above methods worked. I went with #1.Cardie

© 2022 - 2024 — McMap. All rights reserved.