Tableau function Number

Tableau SQRT to SQL

SQRT(number) returns the square root: SQRT(25) is 5. A negative number has no real square root and Tableau returns null; DuckDB's sqrt() raises an error, so the SQL is CASE WHEN x >= 0 THEN sqrt(x) END. Rewriting it as pow(x, 0.5) avoids the error but returns NaN instead of null.

Updated · Checked on the xmetric engine

What is the syntax?

SQRT(number)
number
a number, zero or above.

Returns a non-negative number. Null if the input is null or negative.

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 the square root of each value?

Tableau

SQRT([Value])

SQL (DuckDB)

SELECT CASE WHEN Value >= 0 THEN sqrt(Value) END AS result
FROM input
ORDER BY id

SQRT(16) is 4 and SQRT(0) is 0. The negatives return null.

ValueResult
2.51.58
−2.5null
0.130.35
−0.4null
3.751.94
null
00
164

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

Where does a straight translation go wrong?

Common mistake

Why does pow(x, 0.5) show NaN where Tableau shows null?

A fractional power of a negative number is not a number, and DuckDB returns NaN for rows 2 and 4. NaN is not null: it survives filters on IS NULL and turns sums into NaN. Tableau's SQRT gives null.

Tableau

SQRT([Value])

Tempting SQL

SELECT pow(Value, 0.5) AS result
FROM input
ORDER BY id

SQL that matches

SELECT CASE WHEN Value >= 0 THEN sqrt(Value) END AS result
FROM input
ORDER BY id
ValueTableauTempting SQLSQL that matches
2.51.581.581.58
−2.5nullNaNnull
0.130.350.350.35
−0.4nullNaNnull
3.751.941.941.94
nullnullnull
0000
16444

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 CASE WHEN Value >= 0 THEN SQRT(Value) END AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT SAFE.SQRT(Value) AS result
FROM input
ORDER BY id

BigQuery documentation

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

What does SQRT return for a negative?

Null: rows 2 and 4 hold -2.5 and -0.4. DuckDB raises an error on them, so the SQL keeps the Value >= 0 guard. In BigQuery use SAFE.SQRT.

Is SQRT the inverse of SQUARE?

For non-negative numbers. SQRT(SQUARE(-3)) is 3.

What does SQRT return for a null?

Null. Row 6 has no value.

Where is this documented?