Tableau function Number

Tableau ABS to SQL

ABS(number) drops the sign: ABS(-40) is 40, and a null stays null. The SQL is the same word, abs(), in DuckDB, Snowflake and BigQuery. The one thing to get right is where ABS sits relative to SUM: ABS(SUM(x)) and SUM(ABS(x)) answer different questions.

Updated · Checked on the xmetric engine

What is the syntax?

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

Returns a number of the same type, without its sign. Null if the input is null.

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 the size of each change?

Tableau

ABS([Change])

SQL (DuckDB)

SELECT abs(Change) AS result
FROM input
ORDER BY id

Row by row: -40 becomes 40, 12.5 stays 12.5, and the missing value stays null.

ChangeResult
−4040
2525
−1010
12.512.5
−3030
null

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

What is the size of the net change per region?

Tableau

ABS(SUM([Change]))

SQL (DuckDB)

SELECT Region, abs(sum(Change)) AS result
FROM input
GROUP BY Region
ORDER BY Region

East's changes add up to -25, so its net change has size 25.

RegionResult
East25
West17.5

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

Where does a straight translation go wrong?

Common mistake

Why is SUM(ABS(x)) not the same as ABS(SUM(x))?

Summing the sizes counts every move, up or down; taking the size of the sum lets moves cancel out. East moves 75 in total but nets 25. Translate the order exactly as Tableau has it.

Tableau

ABS(SUM([Change]))

Tempting SQL

SELECT Region, sum(abs(Change)) AS result
FROM input
GROUP BY Region
ORDER BY Region

SQL that matches

SELECT Region, abs(sum(Change)) AS result
FROM input
GROUP BY Region
ORDER BY Region
RegionTableauTempting SQLSQL that matches
East257525
West17.542.517.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 ABS(Change) AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT ABS(Change) AS result
FROM input
ORDER BY id

BigQuery documentation

What does your own ABS formula return?

Edit the formula and run it on the 6 sample rows. Fields: [id], [Region], [Change].

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

Which functions go with it?

What do people ask about ABS?

What does ABS return for a null?

Null. Row 6 of the sample has no change, and ABS returns null there. Wrap it in ZN() (SQL: coalesce(abs(x), 0)) only if a missing value really means zero.

Can ABS take an aggregate?

Yes. ABS(SUM([Change])) is the size of the net change per mark. It's an aggregate, so it can sit next to other aggregates in the same formula.

How do I flag values that moved by more than 20 either way?

ABS([Change]) > 20 returns true or false per row; in SQL, abs(Change) > 20.

Where is this documented?