Tableau function Logical

Tableau IFNULL to SQL

IFNULL(expr1, expr2) returns expr1 if it isn't null, and expr2 otherwise. DuckDB, Snowflake and BigQuery have coalesce(expr1, expr2) (and an ifnull alias). Both arguments must be the same type. Inside an aggregate, place it exactly as Tableau does: IFNULL(AVG(x), 0) and AVG(IFNULL(x, 0)) give different averages.

Updated · Checked on the xmetric engine

What is the syntax?

IFNULL(expr1, expr2)
expr1
the value to keep when it is present.
expr2
the fallback, of the same type as expr1.

Returns expr1 if it is not null, otherwise expr2. Same type as the arguments.

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 is each backup count, with blanks as 0?

Tableau

IFNULL([Backup], 0)

SQL (DuckDB)

SELECT coalesce(Backup, 0) AS result
FROM input
ORDER BY id

Rows 2 and 5 have no backup, so they show 0.

BackupResult
1010
0
77
44
0
33

The Tableau formula and the SQL return the same 6 rows.

How do I fall back to another field?

Tableau

IFNULL([Backup], [Score])

SQL (DuckDB)

SELECT coalesce(Backup, Score) AS result
FROM input
ORDER BY id

Row 2 has no backup, so it takes its score, 80.

ScoreBackupResult
921010
8080
5077
44
4545
8033

The Tableau formula and the SQL return the same 6 rows.

How do I label missing seasons?

Tableau

IFNULL([Season], 'Unknown')

SQL (DuckDB)

SELECT coalesce(Season, 'Unknown') AS result
FROM input
ORDER BY id

Row 5 has no season.

SeasonResult
SummerSummer
WinterWinter
SpringSpring
SummerSummer
Unknown
WinterWinter

The Tableau formula and the SQL return the same 6 rows.

Where does a straight translation go wrong?

Common mistake

Why does moving coalesce inside AVG lower the average?

IFNULL around the AVG only fills regions with no backups at all. Inside the AVG, every null row counts as 0: East averages 10 and null, which is 10 in Tableau and 5 in the SQL.

Tableau

IFNULL(AVG([Backup]), 0)

Tempting SQL

SELECT Region, avg(coalesce(Backup, 0)) AS result
FROM input
GROUP BY Region
ORDER BY Region

SQL that matches

SELECT Region, coalesce(avg(Backup), 0) AS result
FROM input
GROUP BY Region
ORDER BY Region
RegionTableauTempting SQLSQL that matches
East10510
North31.53
West5.55.55.5

The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.

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 IFNULL(Backup, 0) AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT IFNULL(Backup, 0) AS result
FROM input
ORDER BY id

BigQuery documentation

What does your own IFNULL 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 IFNULL?

Is IFNULL the same as ZN?

ZN(x) is IFNULL(x, 0) for numbers. IFNULL also works with strings and dates and lets you pick the fallback.

Can the fallback be a different type?

No. IFNULL([Backup], "none") mixes a number and a string, and Tableau rejects it. Convert one side first: IFNULL(STR([Backup]), "none").

What if both arguments are null?

The result is null. For more than two fallbacks, nest it: IFNULL([a], IFNULL([b], [c])), or use coalesce(a, b, c) in SQL.

Where is this documented?