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.
score is greater than 50?status for a row where score = 75?
SELECT
score,
CASE
WHEN score >= 90 THEN 'Excellent'
WHEN score >= 70 THEN 'Good'
ELSE 'Average'
END AS status
FROM scores_tableSELECT
user_id,
CASE
WHEN age > 18 THEN 'Adult'
WHEN age <= 18 THEN 'Minor'
ELSE 'Unknown'
FROM userscategory in your dbt model based on sales amount: if sales > 1000, category is 'High'; if sales between 500 and 1000 inclusive, category is 'Medium'; otherwise, 'Low'. Which CASE statement correctly implements this logic?Pass the test and this pattern moves unknown