brunondjoyer
brunondjoyer

Reputation: 23

DBT: var function with two internal functions as variables

dbt 'var function' looks can not accept sql function as a variable, is there any way can handel this kinda problem? Thanks!

As the official document says, The var() function takes an optional second argument, default. If this argument is provided, then it will be the default value for the variable if one is not explicitly defined. Right now, I wanna set current_date function as default variable, and it will excute the value that I define the var() in project.yml if there is one.

what I did now:

select
user_id,
'{{ var("date","current_date") }}' as dt
from table1

in yml:

vars:
#    date: XXX
    current_date: current_date

Upvotes: 0

Views: 1205

Answers (1)

Adam Kipnis
Adam Kipnis

Reputation: 11001

Option 1: Use the datetime package from DBT's modules library (https://docs.getdbt.com/reference/dbt-jinja-functions/modules). This will give you the date from the server where you are running DBT at compliation time.

select
user_id,
'{{ var("date", modules.datetime.date.today()) }}' as dt
from table1

Option 2: Use a dummy for comparison in Jinja and output the appropriate SQL. This will give you the date from the destination server at the time the query is run.

select
user_id,
{%- if var("date", "dummy") == 'dummy' %}
  current_date() as dt
{%- else %}
  {{ var("date", "dummy") }} as dt 
{%- endif %}
from table1

Upvotes: 0

Related Questions