IIF(test, then, else, [unknown]) returns then when the test is true, else when it is false, and unknown when the test is null; with no fourth argument a null test gives null. SQL's CASE WHEN test THEN a ELSE b END, Snowflake's IFF and BigQuery's IF all send a null test to the else branch. The matching SQL needs a separate WHEN NOT test branch.
Updated · Checked on the xmetric engine
What is the syntax?
IIF(test, then, else, [unknown])
test
a boolean expression.
then
returned when the test is true.
else
returned when the test is false.
unknown
optional, returned when the test is null. Without it, a null test gives null.
Returns the then, else or unknown value. Null if the test is null and unknown is omitted.
What does it return, and what is the SQL?
Each example runs on the same 6 sample rows twice: the Tableau formula on the xmetric engine, and the SQL in DuckDB.
What does each flag say, without an unknown branch?
Tableau
IIF([Flag], 'yes', 'no')
SQL (DuckDB)
SELECTCASEWHEN Flag THEN'yes'WHENNOT Flag THEN'no'ENDAS result
FROM input
ORDERBY id
Rows 3 and 6 have no flag, so the test is null and IIF returns null.
Flag
Result
true
yes
false
no
null
true
yes
false
no
null
✓ The Tableau formula and the SQL return the same 6 rows.
How do I label the unknown flags?
Tableau
IIF([Flag], 'yes', 'no', 'unknown')
SQL (DuckDB)
SELECTCASEWHEN Flag THEN'yes'WHENNOT Flag THEN'no'ELSE'unknown'ENDAS result
FROM input
ORDERBY id
The fourth argument catches rows 3 and 6.
Flag
Result
true
yes
false
no
unknown
true
yes
false
no
unknown
✓ The Tableau formula and the SQL return the same 6 rows.
Is each score high or low?
Tableau
IIF([Score]>=80, 'high', 'low')
SQL (DuckDB)
SELECTCASEWHEN Score >=80THEN'high'WHENNOT (Score >=80) THEN'low'ENDAS result
FROM input
ORDERBY id
Row 4 has no score, so the comparison is null and so is the result.
Score
Result
92
high
80
high
50
low
null
45
low
80
high
✓ The Tableau formula and the SQL return the same 6 rows.
Where does a straight translation go wrong?
Common mistake
Why does a CASE with ELSE say "no" where IIF gives null?
A null test is not true, so CASE falls through to ELSE: rows 3 and 6 become "no". IIF keeps them null unless you pass the fourth argument.
Tableau
IIF([Flag], 'yes', 'no')
Tempting SQL
SELECTCASEWHEN Flag THEN'yes'ELSE'no'ENDAS result
FROM input
ORDERBY id
SQL that matches
SELECTCASEWHEN Flag THEN'yes'WHENNOT Flag THEN'no'ENDAS result
FROM input
ORDERBY id
Flag
Tableau
Tempting SQL
SQL that matches
true
yes
yes
yes
false
no
no
no
null
no
null
true
yes
yes
yes
false
no
no
no
null
no
null
✓ The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.
What does IIF return with and without the unknown argument?
The same test on the flags, with three and with four arguments. Columns 1 to 6 are the sample rows, by id.
Case
Tableau, then SQL (DuckDB)
1
2
3
4
5
6
no unknown argument
IIF([Flag], 'yes', 'no')CASE WHEN Flag THEN 'yes' WHEN NOT Flag THEN 'no' END
yes
no
null
yes
no
null
with unknown argument
IIF([Flag], 'yes', 'no', 'unknown')CASE WHEN Flag THEN 'yes' WHEN NOT Flag THEN 'no' ELSE 'unknown' END
yes
no
unknown
yes
no
unknown
✓ On every row, each Tableau formula and its SQL return the same value.
How do I write it in Snowflake or BigQuery?
The first example, written for each warehouse from its documentation. These are not run here; DuckDB is.
Snowflake
SELECTCASEWHEN Flag THEN'yes'WHENNOT Flag THEN'no'ENDAS result
FROM input
ORDERBY id