Concept Flow - if/else logic in models
Start
Evaluate condition
Execute IF block
Execute ELSE block
Continue with rest of model
The model checks a condition. If true, it runs the IF part; otherwise, it runs the ELSE part, then continues.
select
case
when sales > 100 then 'High'
else 'Low'
end as sales_category
from sales_data| Row | sales | Condition (sales > 100) | Branch Taken | Output sales_category |
|---|---|---|---|---|
| 1 | 150 | True | IF | 'High' |
| 2 | 90 | False | ELSE | 'Low' |
| 3 | 100 | False | ELSE | 'Low' |
| 4 | 200 | True | IF | 'High' |
| 5 | 50 | False | ELSE | 'Low' |
| Variable | Row 1 | Row 2 | Row 3 | Row 4 | Row 5 |
|---|---|---|---|---|---|
| sales | 150 | 90 | 100 | 200 | 50 |
| Condition (sales > 100) | True | False | False | True | False |
| sales_category | 'High' | 'Low' | 'Low' | 'High' | 'Low' |
if/else logic in dbt models uses SQL CASE statements. Syntax: CASE WHEN condition THEN result ELSE other_result END. Evaluates condition row-by-row. Runs THEN part if True, ELSE part if False. Useful for categorizing or branching logic in models.