RAWSQLAGG_STR("MIN(%1)", [Product]) sends MIN(Product) to the database and runs it once per mark: per region, in a view with Region on Rows. In SQL that is the aggregate plus a GROUP BY on the view's dimensions. String MIN and MAX follow the database's sort order, so check its collation.
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 string 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 product comes first alphabetically in each region?
Tableau
RAWSQLAGG_STR("MIN(%1)", [Product])
SQL (DuckDB)
SELECT Region, min(Product) AS result
FROM input
GROUPBY Region
ORDERBY Region
West's line with no product name is skipped, so West returns Shelf.
Region
Result
East
Chair
North
Desk
West
Shelf
✓ The Tableau formula and the SQL return the same 3 rows.
Which product comes last alphabetically in each region?
Tableau
RAWSQLAGG_STR("MAX(%1)", [Product])
SQL (DuckDB)
SELECT Region, max(Product) AS result
FROM input
GROUPBY Region
ORDERBY Region
Lamp sorts after Desk and Chair, so East returns Lamp.
Region
Result
East
Lamp
North
Desk
West
Shelf
✓ The Tableau formula and the SQL return the same 3 rows.
Where does a straight translation go wrong?
Common mistake
Why does every region get the same product?
The aggregate runs per mark, not over the whole table. Computing max(Product) once for all rows and pasting it next to each region gives Shelf everywhere; East's own maximum is Lamp.
Tableau
RAWSQLAGG_STR("MAX(%1)", [Product])
Tempting SQL
SELECT Region,
(
SELECTmax(Product)
FROM input
) AS result
FROM input
GROUPBY Region
ORDERBY Region
SQL that matches
SELECT Region, max(Product) AS result
FROM input
GROUPBY Region
ORDERBY Region
Region
Tableau
Tempting SQL
SQL that matches
East
Lamp
Shelf
Lamp
North
Desk
Shelf
Desk
West
Shelf
Shelf
Shelf
✓ 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, MIN(Product) AS result
FROM input
GROUPBY Region
ORDERBY Region
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.
Why do lowercase names sort after uppercase ones?
MIN and MAX compare strings in the database's collation. DuckDB compares bytes by default, so "apple" sorts after "Zebra". A case-insensitive collation, or comparing lower() values, changes the answer.