Tableau function Logical

Tableau IN to SQL

[Field] IN (value1, value2, ...) returns true when the field matches any value in the list; the list can also be a set or a combined field. DuckDB, Snowflake and BigQuery write Field IN (...) the same way, and NOT [Field] IN (...) becomes Field NOT IN (...). Spelled out with comparisons, "not in" needs AND, not OR.

Updated · Checked on the xmetric engine

What is the syntax?

<expr1> IN <expr2>
expr1
a field or expression, row-level or aggregate (for example SUM([Cost])).
expr2
a list of literal values in parentheses, a set, or a combined field.

Returns a boolean: true if expr1 matches any value in expr2.

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.

Which rows are in East or West?

Tableau

[Region] IN ('East', 'West')

SQL (DuckDB)

SELECT Region IN ('East', 'West') AS result
FROM input
ORDER BY id

Rows 5 and 6 are in North.

RegionResult
Easttrue
Easttrue
Westtrue
Westtrue
Northfalse
Northfalse

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

How do I label coastal regions?

Tableau

IIF([Region] IN ('East', 'West'), 'Coast', 'Inland')

SQL (DuckDB)

SELECT
  CASE
    WHEN Region IN ('East', 'West') THEN 'Coast'
    ELSE 'Inland'
    END AS result
FROM input
ORDER BY id

North is Inland.

RegionResult
EastCoast
EastCoast
WestCoast
WestCoast
NorthInland
NorthInland

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

Where does a straight translation go wrong?

Common mistake

Why does "not in" spelled with OR return true for everything?

Every region is either not East or not West, so Region <> 'East' OR Region <> 'West' is true on all six rows. NOT IN means not East and not West: only North.

Tableau

NOT [Region] IN ('East', 'West')

Tempting SQL

SELECT Region <> 'East' OR Region <> 'West' AS result
FROM input
ORDER BY id

SQL that matches

SELECT Region NOT IN ('East', 'West') AS result
FROM input
ORDER BY id
RegionTableauTempting SQLSQL that matches
Eastfalsetruefalse
Eastfalsetruefalse
Westfalsetruefalse
Westfalsetruefalse
Northtruetruetrue
Northtruetruetrue

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 Region IN ('East', 'West') AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT Region IN ('East', 'West') AS result
FROM input
ORDER BY id

BigQuery documentation

What does your own IN formula return?

Edit the formula and run it on the 6 sample rows. Fields: [id], [Region], [Season], [Score], [Flag], [Backup].

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

Which functions go with it?

What do people ask about IN?

Can IN test an aggregate?

Yes: SUM([Score]) IN (172, 50) works per mark. In SQL, compute the sum in the GROUP BY and test it there.

How do I use IN with a set?

[Region] IN [Top Regions] tests set membership. In SQL the set becomes a subquery or a join to the list of members.

Is IN the same as a chain of ORs?

Yes: [Region] IN ("East", "West") is [Region] = "East" OR [Region] = "West". Its negation is a chain of ANDs.

Where is this documented?