Using if block in dbt models
Asked Answered
F

1

10

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.

Fog answered 7/1, 2021 at 14:20 Comment(2)
Hey Ravi. Would you be able to share a bit more information? Do you get an error when it runs? Could you share what the compiled code looks like? How are you passing the variable?Rimple
Hi @dylanbaker, I fixed my issue. I answered below. The variable name had to be placed in var()Fog
F
20

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 %}
Fog answered 7/1, 2021 at 17:23 Comment(3)
Could you explain why?Siderosis
where enable_whitelisting was coming from? is it not a column from the model?Nedra
It's a variable from either the command line or from the configFog

© 2022 - 2024 — McMap. All rights reserved.