[a] % [b] is the remainder of a divided by b, with the sign of a: -7 % 2 is -1. DuckDB's % and mod() agree. The floor-based formula a - b * floor(a / b), Python's convention, gives 1 instead.
Updated · Checked on the xmetric engine
What is the syntax?
a % b
a, b
numbers, numeric fields or aggregates.
Returns the remainder, with the sign of a. 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 the remainder of a divided by b?
Tableau
[a]%[b]
SQL (DuckDB)
SELECT a % b AS result
FROM input
ORDERBY id
Row 2: -7 % 2 is -1. Row 8: -3 % -2 is -1.
a
b
Result
7
2
1
−7
2
−1
2
5
2
9
4
1
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 the floor formula return 1 for -7 % 2?
a - b * floor(a / b) rounds the quotient down, so the remainder takes the sign of b. Tableau truncates toward zero and keeps the sign of a.
Tableau
[a]%[b]
Tempting SQL
SELECT a - b *floor(a / b) AS result
FROM input
ORDERBY id
SQL that matches
SELECT a % b AS result
FROM input
ORDERBY id
a
b
Tableau
Tempting SQL
SQL that matches
7
2
1
1
1
−7
2
−1
1
−1
2
5
2
2
2
9
4
1
1
1
5
3
2
2
2
2
null
null
null
1
null
null
null
−3
−2
−1
−1
−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.