Tableau function Number

Tableau ZN to SQL

ZN(expression) returns the expression, or 0 when it is null. In SQL that is coalesce(x, 0) (Snowflake also has ZEROIFNULL, BigQuery IFNULL(x, 0)). Where it goes matters: ZN([a]) + ZN([b]) replaces each null before adding, while coalesce(a + b, 0) throws away the value that was there.

Updated · Checked on the xmetric engine

What is the syntax?

ZN(expression)
expression
a number or a numeric expression, row-level or aggregate.

Returns the expression, or 0 if it is null. Same type as the expression.

What does it return, and what is the SQL?

Each example runs on the same 8 sample rows twice: the Tableau formula on the xmetric engine, and the SQL in DuckDB.

What is each value, with nulls as zero?

Tableau

ZN([Value])

SQL (DuckDB)

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

Row 6 has no value, so it becomes 0.

ValueResult
2.52.5
−2.5−2.5
0.130.13
−0.4−0.4
3.753.75
0
00
1616

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

What do two fields add up to when either can be null?

Tableau

ZN([Value]) + ZN([Other])

SQL (DuckDB)

SELECT coalesce(Value, 0) + coalesce(Other, 0) AS result
FROM input
ORDER BY id

Row 3 has no Other, so the total is just its value, 0.125.

ValueOtherResult
2.546.5
−2.5−3−5.5
0.130.13
−0.4−1−1.4
3.75811.75
22
00.50.5
161632

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

Where does a straight translation go wrong?

Common mistake

Why does wrapping the sum in coalesce lose values?

In SQL a null in either field makes the whole sum null, and coalescing afterwards turns that into 0. Rows 3 and 6 become 0 instead of 0.125 and 2. Replace each field's null before adding, as ZN does.

Tableau

ZN([Value]) + ZN([Other])

Tempting SQL

SELECT coalesce(Value + Other, 0) AS result
FROM input
ORDER BY id

SQL that matches

SELECT coalesce(Value, 0) + coalesce(Other, 0) AS result
FROM input
ORDER BY id
ValueOtherTableauTempting SQLSQL that matches
2.546.56.56.5
−2.5−3−5.5−5.5−5.5
0.130.1300.13
−0.4−1−1.4−1.4−1.4
3.75811.7511.7511.75
2202
00.50.50.50.5
1616323232

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 ZEROIFNULL(Value) AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

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

BigQuery documentation

What does your own ZN formula return?

Edit the formula and run it on the 8 sample rows. Fields: [id], [Region], [Value], [Qty], [Ratio], [Other].

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

Which functions go with it?

What do people ask about ZN?

Is ZN the same as IFNULL(x, 0)?

Yes. ZN is the zero-only shortcut; IFNULL lets you pick the replacement.

Does ZN change averages?

Yes. AVG(ZN([Value])) counts null rows as zeros and divides by every row; AVG([Value]) skips them. Only use ZN when missing really means zero.

Can ZN take an aggregate?

Yes: ZN(SUM([Sales])) gives 0 for marks with no rows, common before a LOOKUP or a difference.

Where is this documented?