Tableau function Level of detail

Tableau FIXED total with a context filter, in SQL

A context filter runs before FIXED, so { FIXED : SUM([Revenue]) } adds only the rows that pass it: with Region = North in context, the total is 650, not 1,650. In SQL, filter the table once (a CTE with the WHERE) and build both the view and the total from it.

Updated · Checked on the xmetric engine

What is the syntax?

{ FIXED : SUM([Revenue]) }
dimensions
none here, so one value for the whole filtered table.
aggregate
an aggregate such as SUM([Revenue]).

Returns the total over the rows that pass the context filter, the same on every mark.

What does it return, and what is the SQL?

Each example runs on the same 12 sample rows twice: the Tableau formula on the xmetric engine, and the SQL in DuckDB.

What does the FIXED total return with Region = North in context?

Tableau

{ FIXED : SUM([Revenue]) }

Filter Region = North, as a context filter

SQL (DuckDB)

WITH f AS (
  SELECT *
  FROM input
  WHERE Region = 'North'
)
SELECT Team, max(t.total) AS result
FROM f, (
  SELECT sum(Revenue) AS total
  FROM f
) t
GROUP BY Team
ORDER BY Team

Both North teams carry 650, North's own total.

TeamResult
Cedar650
Pine650

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

What share of North's revenue does each team have?

Tableau

SUM([Revenue]) / MIN({ FIXED : SUM([Revenue]) })

Filter Region = North, as a context filter

SQL (DuckDB)

WITH f AS (
  SELECT *
  FROM input
  WHERE Region = 'North'
)
SELECT Team, sum(Revenue) / max(t.total) AS result
FROM f, (
  SELECT sum(Revenue) AS total
  FROM f
) t
GROUP BY Team
ORDER BY Team

Cedar's 500 over 650 is 77%; Pine's 150 is 23%. They add up to 100% now.

TeamResult
Cedar0.77
Pine0.23

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

Where does a straight translation go wrong?

Common mistake

Why does a WHERE on the outer query keep the company total?

A subquery over input reads every row, whatever the outer WHERE says. That is how an ordinary filter behaves. For a context filter the total must read the filtered rows too.

Tableau

{ FIXED : SUM([Revenue]) }

Filter Region = North, as a context filter

Tempting SQL

SELECT Team, max(t.total) AS result
FROM input, (
  SELECT sum(Revenue) AS total
  FROM input
) t
WHERE Region = 'North'
GROUP BY Team
ORDER BY Team

SQL that matches

WITH f AS (
  SELECT *
  FROM input
  WHERE Region = 'North'
)
SELECT Team, max(t.total) AS result
FROM f, (
  SELECT sum(Revenue) AS total
  FROM f
) t
GROUP BY Team
ORDER BY Team
TeamTableauTempting SQLSQL that matches
Cedar6501,650650
Pine6501,650650

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

WITH f AS (
  SELECT *
  FROM input
  WHERE Region = 'North'
)
SELECT Team, sum(Revenue) / max(t.total) AS result
FROM f, (
  SELECT sum(Revenue) AS total
  FROM f
) t
GROUP BY Team
ORDER BY Team

Snowflake documentation

BigQuery

WITH f AS (
  SELECT *
  FROM input
  WHERE Region = 'North'
)
SELECT Team, sum(Revenue) / max(t.total) AS result
FROM f, (
  SELECT sum(Revenue) AS total
  FROM f
) t
GROUP BY Team
ORDER BY Team

BigQuery documentation

What does your own FIXED with a context filter formula return?

Edit the formula and run it on the 12 sample rows. Fields: [id], [Region], [Team], [Customer], [Invoice], [Revenue], [Date].

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

Which functions go with it?

What do people ask about FIXED with a context filter?

How do I add a filter to context?

Right-click the filter on the Filters shelf and choose Add to Context. The pill turns grey and moves to the top of the shelf.

Is a context filter the same as a data source filter here?

For FIXED, yes: both run before it. A data source filter applies to every sheet using the source; a context filter only to its sheet.

Does a context filter change INCLUDE and EXCLUDE?

Yes, as does an ordinary filter: both run before INCLUDE and EXCLUDE.

Where is this documented?