Tableau function Type conversion

Tableau INT to SQL

INT(expression) converts to an integer. For a number it drops the fraction toward zero: INT(-9.7) is -9 and INT(8/3) is 2. In DuckDB write CAST(trunc(x) AS BIGINT). A bare CAST(x AS INTEGER) rounds to the nearest integer, so -9.7 becomes -10. True becomes 1 and false 0.

Updated · Checked on the xmetric engine

What is the syntax?

INT(expression)
expression
a number, a numeric expression or a boolean.

Returns an integer. 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 each amount as a whole number?

Tableau

INT([Amount])

SQL (DuckDB)

SELECT CAST (trunc(Amount) AS BIGINT) AS result
FROM input
ORDER BY id

2.5 becomes 2, -9.7 becomes -9 and 1234567.891 becomes 1234567. The missing amount stays null.

AmountResult
2.52
−9.7−9
33
0.10
null
1,234,5681,234,567

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

How many whole halves are in each quantity?

Tableau

INT([Qty] / 2)

SQL (DuckDB)

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

7 / 2 is 3.5, so 3; -3 / 2 is -1.5, so -1.

QtyResult
73
−3−1
00
126
52
null

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

How do I count flags as 1 and 0?

Tableau

INT([Flag])

SQL (DuckDB)

SELECT CAST (Flag AS INTEGER) AS result
FROM input
ORDER BY id

true becomes 1, false 0, unknown null.

FlagResult
true1
false0
null
true1
false0
true1

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

Where does a straight translation go wrong?

Common mistake

Why does CAST(x AS INTEGER) give -10 for -9.7?

DuckDB rounds when it casts a decimal to an integer, so -9.7 becomes -10 and 1234567.891 becomes 1234568. Tableau's INT truncates toward zero. Truncate first.

Tableau

INT([Amount])

Tempting SQL

SELECT CAST (Amount AS INTEGER) AS result
FROM input
ORDER BY id

SQL that matches

SELECT CAST (trunc(Amount) AS BIGINT) AS result
FROM input
ORDER BY id
AmountTableauTempting SQLSQL that matches
2.5222
−9.7−9−10−9
3333
0.1000
nullnullnull
1,234,5681,234,5671,234,5681,234,567

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 CAST (TRUNC(Amount) AS INTEGER) AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT CAST (TRUNC(Amount) AS INT64) AS result
FROM input
ORDER BY id

BigQuery documentation

What does your own INT formula return?

Edit the formula and run it on the 6 sample rows. Fields: [id], [Amount], [Qty], [Code], [Flag].

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

Which functions go with it?

What do people ask about INT?

Does INT round or truncate?

It truncates toward zero for numbers: 2.5 becomes 2 and -9.7 becomes -9. Use ROUND to round, or FLOOR to always go down.

What does INT return for a boolean?

1 for true, 0 for false and null for unknown; in SQL CAST(Flag AS INTEGER).

What about strings?

Tableau's help says a string is first converted to a float and then rounded. This page leaves strings out; check the result on your data, and use INT(FLOAT([Code])) or ROUND when you need to be explicit.

Where is this documented?