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)
SELECTabs(Change) AS result
FROM input
ORDERBY id
Row by row: -40 becomes 40, 12.5 stays 12.5, and the missing value stays null.
Change
Result
−40
40
25
25
−10
10
12.5
12.5
−30
30
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
GROUPBY Region
ORDERBY Region
East's changes add up to -25, so its net change has size 25.
Region
Result
East
25
West
17.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
GROUPBY Region
ORDERBY Region
SQL that matches
SELECT Region, abs(sum(Change)) AS result
FROM input
GROUPBY Region
ORDERBY Region
Region
Tableau
Tempting SQL
SQL that matches
East
25
75
25
West
17.5
42.5
17.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.
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.