Tableau function Number

Tableau SIGN to SQL

SIGN(number) returns -1 for a negative number, 0 for zero and 1 for a positive number; a null stays null. DuckDB, Snowflake and BigQuery all call it sign(). A two-way CASE WHEN x > 0 THEN 1 ELSE -1 END looks equivalent but sends both zero and null to -1.

Updated · Checked on the xmetric engine

What is the syntax?

SIGN(number)
number
a number, a numeric field or an aggregate such as SUM([Qty]).

Returns an integer: -1, 0 or 1. Null if the input is null.

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.

Is each value negative, zero or positive?

Tableau

SIGN([Value])

SQL (DuckDB)

SELECT sign(Value) AS result
FROM input
ORDER BY id

-2.5 gives -1, 0 gives 0, and the null row stays null.

ValueResult
2.51
−2.5−1
0.131
−0.4−1
3.751
null
00
161

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

Is each region's total quantity up or down?

Tableau

SIGN(SUM([Qty]))

SQL (DuckDB)

SELECT Region, sign(sum(Qty)) AS result
FROM input
GROUP BY Region
ORDER BY Region

North's quantities add up to -1, so its sign is -1.

RegionResult
East1
North−1
West1

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

Where does a straight translation go wrong?

Common mistake

Why does a two-way CASE give -1 for zero?

A CASE with only "> 0" and ELSE has no branch for zero or null, so row 7 (0) and row 6 (null) both become -1.

Tableau

SIGN([Value])

Tempting SQL

SELECT CASE WHEN Value > 0 THEN 1 ELSE - 1 END AS result
FROM input
ORDER BY id

SQL that matches

SELECT sign(Value) AS result
FROM input
ORDER BY id
ValueTableauTempting SQLSQL that matches
2.5111
−2.5−1−1−1
0.13111
−0.4−1−1−1
3.75111
null−1null
00−10
16111

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

Snowflake documentation

BigQuery

SELECT SIGN(Value) AS result
FROM input
ORDER BY id

BigQuery documentation

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

What does SIGN return for zero?

0. Row 7 has a value of 0.

Can SIGN take an aggregate?

Yes: SIGN(SUM([Qty])) gives the direction of each region's total, which is handy for colouring marks.

What does SIGN return for a null?

Null. Row 6 has no value.

Where is this documented?