[a] - [b] subtracts b from a; a null on either side gives null, as in SQL. Watch the aggregation when you translate: SUM([a] - [b]) skips rows where either value is null, SUM([a]) - SUM([b]) does not.
Updated · Checked on the xmetric engine
What is the syntax?
a - b
a, b
numbers, numeric fields or aggregates.
Returns a number. Null if either side 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.
What is a - b on each row?
Tableau
[a]-[b]
SQL (DuckDB)
SELECT a - b AS result
FROM input
ORDERBY id
Row 2 is -7 - 2 = -9; row 8 is -3 - (-2) = -1.
a
b
Result
7
2
5
−7
2
−9
2
5
−3
9
4
5
5
3
2
2
null
1
null
−3
−2
−1
✓ The Tableau formula and the SQL return the same 8 rows.
Where does a straight translation go wrong?
Common mistake
Why does sum(a) - sum(b) give a different total?
SUM([a] - [b]) loses rows 6 and 7, where one side is null: -1. Subtracting the column totals keeps them: -2.
Tableau
SUM([a]-[b])
Tempting SQL
SELECTsum(a) -sum(b) AS result
FROM input
SQL that matches
SELECTsum(a - b) AS result
FROM input
Tableau
Tempting SQL
SQL that matches
−1
−2
−1
✓ 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.