Tableau function Pass-through (RAWSQL)

Tableau RAWSQLAGG_BOOL to SQL

RAWSQLAGG_BOOL("SUM(%1) > 300", [Sales]) asks the database, once per mark, whether the total of Sales passes 300. In SQL that is sum(Sales) > 300 with a GROUP BY on the view's dimensions. Keep the aggregate where the text puts it: a test on the total is not a test on any single row.

Updated · Checked on the xmetric engine

What is the syntax?

RAWSQLAGG_BOOL("sql_expression", [argument1], [argument2], ...)
sql_expression
a string constant of SQL in your data source's dialect. %1, %2, ... are placeholders for the arguments that follow, numbered from 1.
argument, ...
fields or expressions. Tableau puts the first one where %1 is, the second where %2 is, and so on.

Returns one boolean per mark: the database's answer to an aggregate expression, grouped by the view's dimensions.

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 regions sold more than 300 in total?

Tableau

RAWSQLAGG_BOOL("SUM(%1) > 300", [Sales])

SQL (DuckDB)

SELECT Region, sum(Sales) > 300 AS result
FROM input
GROUP BY Region
ORDER BY Region

North has no sales at all, so its sum is null and the test is null.

RegionResult
Easttrue
Northnull
Westtrue

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

Which regions have a single line above 300?

Tableau

RAWSQLAGG_BOOL("MAX(%1) > 300", [Sales])

SQL (DuckDB)

SELECT Region, max(Sales) > 300 AS result
FROM input
GROUP BY Region
ORDER BY Region

East passes 300 in total but none of its lines does on its own.

RegionResult
Eastfalse
Northnull
Westtrue

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

Where does a straight translation go wrong?

Common mistake

Why does a per-row test fail East?

bool_or(Sales > 300) asks whether any line is over 300. The RAWSQL text sums first and tests the total. East's lines are 240.5 and 85, so it fails row by row but passes in total.

Tableau

RAWSQLAGG_BOOL("SUM(%1) > 300", [Sales])

Tempting SQL

SELECT Region, bool_or(Sales > 300) AS result
FROM input
GROUP BY Region
ORDER BY Region

SQL that matches

SELECT Region, sum(Sales) > 300 AS result
FROM input
GROUP BY Region
ORDER BY Region
RegionTableauTempting SQLSQL that matches
Easttruefalsetrue
Northnullnullnull
Westtruetruetrue

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, SUM(Sales) > 300 AS result
FROM input
GROUP BY Region
ORDER BY Region

Snowflake documentation

BigQuery

SELECT Region, SUM(Sales) > 300 AS result
FROM input
GROUP BY Region
ORDER BY Region

BigQuery documentation

What does your own RAWSQLAGG_BOOL formula return?

Edit the formula and run it on the 6 sample rows. Fields: [id], [Region], [Product], [Units], [Boxes], [Sales], [Ordered], [Shipped].

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

Which functions go with it?

What do people ask about RAWSQLAGG_BOOL?

What does %1 stand for?

The first argument after the SQL string. %2 is the second, and so on, whatever order the placeholders appear in the text. To translate a RAWSQL call, replace each %n with the column it points at and keep the rest of the text.

Does Tableau check the SQL inside RAWSQL?

No. Tableau passes the text to the database as it is, so a typo or a function your database lacks fails at query time, in the database. The text is also tied to that database's dialect: point the workbook at a different database and the same text can fail or mean something else.

What is the difference between RAWSQL and RAWSQLAGG?

RAWSQL_ functions are row-level: the expression runs once per row. RAWSQLAGG_ functions hold an aggregate such as SUM(%1) and run once per mark, grouped by the dimensions in the view. In SQL the first is a plain column expression; the second needs a GROUP BY on the view's dimensions.

Where is this documented?