Calculated Fields in Tableau: The Four Kinds and How to Tell Them Apart
The calculation editor looks the same for all of them, which is the problem. Four kinds of calculation, one dialog, and the only way to tell which one you have written is to know what the parts do.
What you do: decide the grain before you type. Per row, per mark, per named grain, or across the finished table.
The short version. The question is always at what grain does this run.
1. Row level
Runs once per row of the underlying data, before any grouping.
[Profit] / [Sales]
IF [Status] = "Cancelled" THEN 0 ELSE [Amount] END
DATEDIFF('day', [Ordered], [Shipped])
Use these for classifying and cleaning: a flag column, a band, a difference between two dates on the same row. They behave like a new column in the source, and Tableau will aggregate them for you when they land on a shelf.
The trap: AVG([Profit]/[Sales]) averages the per-row ratios. On rows of very different sizes that is not the profit margin, it is the average of margins, and it can be several points away from the real answer.
2. Aggregate
Runs once per mark, after Tableau has grouped by whatever is on the view.
SUM([Profit]) / SUM([Sales])
COUNTD([Customer])
MIN([Order Date])
This is where a ratio belongs. SUM([Profit])/SUM([Sales]) is the true margin at every level of aggregation, and it re-computes correctly when you change what is on the view.
SUM, MIN or ATTR. ATTR is the honest one for text: it returns the value if it is the same for every row in the mark, and an asterisk if it is not, which is information rather than a guess.3. Level of detail expressions
Computed at a grain you specify, in braces, independent of the view.
| Keyword | Means | Typical use |
|---|---|---|
FIXED | This grain, ignoring the view | Customer total shown on a region chart |
INCLUDE | The view grain plus these dimensions | Average of per-customer totals |
EXCLUDE | The view grain minus these dimensions | Percent of a total that ignores one dimension |
{FIXED [Customer]: SUM([Sales])}
{INCLUDE [Customer]: SUM([Sales])}
{EXCLUDE [Region]: SUM([Sales])}
The classic use is a cohort or a first-order date: {FIXED [Customer]: MIN([Order Date])} gives every row the customer's first order date, which is the foundation of any retention view.
FIXED runs before dimension filters, which is the single most common surprise in Tableau. If a filtered view shows an unfiltered denominator, put the filter in context. That sequence is set out in the order of operations.
4. Table calculations
Run last, on the table of results the view has already produced.
RUNNING_SUM(SUM([Sales]))
INDEX()
SUM([Sales]) / TOTAL(SUM([Sales]))
LOOKUP(SUM([Sales]), -1)
Because they operate on the result table, they depend on how the table is laid out, which is what Compute Using controls. Change the view and a table calculation can change meaning without you editing it. That property is covered in table calculations.
Choosing between them
| You want | Kind | Example |
|---|---|---|
| A flag or band per row | Row level | IF [Days] > 30 THEN "Late" ELSE "On time" END |
| A ratio at any level | Aggregate | SUM([Profit])/SUM([Sales]) |
| Each customer's first order date | FIXED | {FIXED [Customer]: MIN([Order Date])} |
| Rank within the view | Table calc | RANK(SUM([Sales])) |
| Difference from last month | Table calc | ZN(SUM([Sales]) - LOOKUP(SUM([Sales]), -1)) |
| Average order size per customer | INCLUDE | AVG({INCLUDE [Customer]: SUM([Sales])}) |
Habits that make calculations maintainable
- Name them for what they mean, not for what they do.
Margin, notCalc1and notSUM Profit over SUM Sales. - Put a comment at the top with
//. Two lines saying what grain it runs at and why saves the next person an hour. - Build in steps. Three small named calculations beat one long nested one, and each can be dropped on a sheet and checked.
- Check every calculation against a number you know, at two different levels of aggregation. Many calculation bugs are correct at one level and wrong at another.
- Use
ZNaround anything that can be null in an arithmetic chain, or one missing month quietly empties a running total.
How to apply this to your own work
- Open a workbook you inherited and classify every calculated field into one of the four kinds.
- Find any
AVGof a row-level ratio and compare it to theSUMoverSUMversion. Note the gap. - Check every
FIXEDagainst the filters that ought to affect it. - Add a two-line comment to your five most-used calculations.
- Validate one calculation at the row level and again at the total. Agreement at both is the test.
The one habit to keep
Say the grain out loud before typing: per row, per mark, per customer, or across the table. Every Tableau calculation question is that question wearing different clothes.
Which kind is the calculation you rely on most, and does it respect your filters?
Tableau for Analysts is 110 pages on how Tableau actually thinks: dimensions against measures, relationships against joins, sets, and level of detail. Enough that you stop dragging fields until it looks right.
Tableau for Analysts, $19 →Table calculations covers the fourth kind in depth, and the order of operations explains when each one runs.
Read Table Calculations →