Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using if block in dbt models

Tags:

jinja2

dbt

Apologies for asking dumb question. But I tried many different approaches but none of them seems to work.

I have a requirement to select data from 2 different tables based on the variable. I am trying to do that in dbt models with if statement but it doesn't seem to work.

Model looks something like thins:

SELECT 
*
FROM
{% if enable_whitelisting == 'true' %}
    {{ ref('accounts_whitelisted') }}    accounts
{% else %}
        {{ ref('accounts') }}   accounts
{% endif %}

Any help is appreciated.

Thanks in advance.

like image 445
Ravi Avatar asked Mar 08 '26 18:03

Ravi


1 Answers

I got this working eventually. Have to put the variable name within the var()

SELECT 
*
FROM
{% if var('enable_whitelisting') == 'true' %}
    {{ ref('accounts_whitelisted') }}    accounts
{% else %}
        {{ ref('accounts') }}   accounts
{% endif %}
like image 178
Ravi Avatar answered Mar 10 '26 14:03

Ravi