Tableau function Number

Tableau DIV to SQL

DIV(integer1, integer2) divides and keeps the integer part: DIV(11, 2) is 5 and DIV(-7, 2) is -3, because the fraction is cut toward zero. In DuckDB that is trunc(a / b). Floor division (fdiv, or Python's //) agrees on positive numbers but gives -4 for -7 / 2.

Updated · Checked on the xmetric engine

What is the syntax?

DIV(integer1, integer2)
integer1
the dividend, an integer.
integer2
the divisor, an integer.

Returns an integer: integer1 divided by integer2, with the fraction dropped toward zero.

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.

How many whole pairs are in each quantity?

Tableau

DIV([Qty], 2)

SQL (DuckDB)

SELECT trunc(Qty / 2) AS result
FROM input
ORDER BY id

7 makes 3 pairs and 10 makes 5. -7 gives -3: the fraction is dropped toward zero.

QtyResult
73
−7−3
105
94
21
42
−10
00

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

Where does a straight translation go wrong?

Common mistake

Why does floor division give -4 where Tableau gives -3?

DuckDB's fdiv (and // in Python) round the quotient down. DIV cuts toward zero, so -7 / 2 is -3 in Tableau and -4 with floor division; -1 / 2 is 0 and -1.

Tableau

DIV([Qty], 2)

Tempting SQL

SELECT fdiv(Qty, 2) AS result
FROM input
ORDER BY id

SQL that matches

SELECT trunc(Qty / 2) AS result
FROM input
ORDER BY id
QtyTableauTempting SQLSQL that matches
7333
−7−3−4−3
10555
9444
2111
4222
−10−10
0000

The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.

What does DIV return when the quotient is negative?

Dividing by a negative number flips the sign, and the fraction is still dropped toward zero. Columns 1 to 8 are the sample rows, by id.

CaseTableau, then SQL (DuckDB)12345678
negative quotientsDIV([Qty], -4)trunc(Qty / -4)−11−2−20−100

On every row, each Tableau formula and its SQL return the same value.

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 TRUNC(Qty / 2) AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT DIV(Qty, 2) AS result
FROM input
ORDER BY id

BigQuery documentation

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

Is DIV floor division?

No. It truncates toward zero, so negative quotients round up: DIV(-7, 2) is -3, not -4. For floor division write FLOOR([a] / [b]).

How do I get the remainder?

With %: [Qty] % 2. DIV and % together give back the dividend: DIV(a, b) * b + a % b = a.

Can DIV take decimals?

No, both arguments must be integers. For decimals, write INT([a] / [b]), which also cuts toward zero.

Where is this documented?