Tableau function Logical

Tableau IIF to SQL

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)

SELECT
  CASE WHEN Flag THEN 'yes' WHEN NOT Flag THEN 'no' END AS result
FROM input
ORDER BY id

Rows 3 and 6 have no flag, so the test is null and IIF returns null.

FlagResult
trueyes
falseno
null
trueyes
falseno
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)

SELECT
  CASE
    WHEN Flag THEN 'yes'
    WHEN NOT Flag THEN 'no'
    ELSE 'unknown'
    END AS result
FROM input
ORDER BY id

The fourth argument catches rows 3 and 6.

FlagResult
trueyes
falseno
unknown
trueyes
falseno
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)

SELECT
  CASE
    WHEN Score >= 80 THEN 'high'
    WHEN NOT (Score >= 80) THEN 'low'
    END AS result
FROM input
ORDER BY id

Row 4 has no score, so the comparison is null and so is the result.

ScoreResult
92high
80high
50low
null
45low
80high

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

SELECT CASE WHEN Flag THEN 'yes' ELSE 'no' END AS result
FROM input
ORDER BY id

SQL that matches

SELECT
  CASE WHEN Flag THEN 'yes' WHEN NOT Flag THEN 'no' END AS result
FROM input
ORDER BY id
FlagTableauTempting SQLSQL that matches
trueyesyesyes
falsenonono
nullnonull
trueyesyesyes
falsenonono
nullnonull

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.

CaseTableau, then SQL (DuckDB)123456
no unknown argumentIIF([Flag], 'yes', 'no')CASE WHEN Flag THEN 'yes' WHEN NOT Flag THEN 'no' ENDyesnonullyesnonull
with unknown argumentIIF([Flag], 'yes', 'no', 'unknown')CASE WHEN Flag THEN 'yes' WHEN NOT Flag THEN 'no' ELSE 'unknown' ENDyesnounknownyesnounknown

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

SELECT
  CASE WHEN Flag THEN 'yes' WHEN NOT Flag THEN 'no' END AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT
  CASE WHEN Flag THEN 'yes' WHEN NOT Flag THEN 'no' END AS result
FROM input
ORDER BY id

BigQuery documentation

What does your own IIF formula return?

Edit the formula and run it on the 6 sample rows. Fields: [id], [Region], [Season], [Score], [Flag], [Backup].

Runs in your browser on the xmetric engine. Nothing is uploaded.

Which functions go with it?

What do people ask about IIF?

Is IIF the same as IF ... THEN ... ELSE ... END?

Not for nulls. IF sends a null test to ELSE; IIF returns null (or the unknown value). Rows 3 and 6 have an unknown flag and show the difference.

How do I chain several tests with IIF?

Nest them, usually in the else slot: IIF([Score] >= 80, "A", IIF([Score] >= 50, "B", "C")). An IF ... ELSEIF chain reads better.

Can IIF count rows?

SUM(IIF([Flag], 1, 0)) counts true flags; the null flags give null and are skipped by SUM.

Where is this documented?