Skip to content
Module 04 of 1255 min readMixed

Calculated fields and the function library

Logic, aggregation, type conversion, string and date functions. The three calculated fields you'll write hundreds of times: margin, YoY, % of total.

33%

Listen along

Read “Calculated fields and the function library” aloud

Plays in your browser using on-device text-to-speech — nothing leaves the page.

Learning objectives

By the end of this module, you should be able to:

  • 01Write calculated fields for the three most common analyst computations
  • 02Use the IF, CASE, and IIF functions for conditional logic
  • 03Recognise the difference between row-level and aggregated calculations

Out of the box, Tableau lets you sum, average, and group fields. To do anything analytical — margin, growth rate, segment classification, derived metrics — you write calculated fields. The syntax is its own small language: function calls, operators, IF/THEN/ELSE, aggregations. Most analyst work happens here.

Three calculations you'll write hundreds of times

text
// 1. Gross margin
Gross margin = (SUM([Revenue]) - SUM([Cost])) / SUM([Revenue])
// 2. Year-over-year growth (with table calc — see Module 8)
YoY growth = ([Sales] - LOOKUP([Sales], -1)) / LOOKUP([Sales], -1)
// 3. Customer segment classification
Segment = IF [Annual spend] > 100000 THEN 'Enterprise'
ELSEIF [Annual spend] > 10000 THEN 'Mid-market'
ELSE 'SMB' END
Three calculations that cover ~60% of analyst Tableau work.

Row-level vs aggregated

  • Row-level: evaluated for every row in the underlying data. [Quantity] * [Unit Price]. Used when you need a derived value at the original grain.
  • Aggregated: uses aggregation functions like SUM, AVG, MAX. SUM([Revenue]) / SUM([Cost]). Evaluated at the visualisation's level of detail.
  • Mixing the two: SUM([Revenue]) / [Cost] is invalid — Tableau will error. The fix: either aggregate both or aggregate neither.

The order of operations

Tableau executes operations in a specific order: data source filters → context filters → FIXED LODs → dimension filters → INCLUDE/EXCLUDE LODs → measure filters → table calc filters → references → table calcs. If your calc gives unexpected results, it's almost always because you assumed an operation ran at a different stage than it actually did. The order-of-operations diagram is the most printed-and-pinned Tableau reference there is.

Conditional functions

  • IF [x] > 10 THEN 'big' ELSEIF [x] > 5 THEN 'medium' ELSE 'small' END — the long form.
  • IIF([x] > 10, 'big', 'small') — single-condition ternary.
  • CASE [region] WHEN 'EA' THEN 'East Africa' WHEN 'WA' THEN 'West Africa' ELSE 'Other' END — switch syntax for discrete values.

Exercise

In a Tableau workbook, write a calculated field that classifies orders by size (Large > $1000, Medium $100-$1000, Small < $100). Drop it on Rows; put count of orders on Columns. Now add the calculation to the Tooltip with formatting. What's the level of detail of your calculation — row-level or aggregated?

Key takeaways

  • Calculated fields turn Tableau from a viewer into a tool.
  • Row-level calcs evaluate per row; aggregated calcs evaluate per view's level of detail.
  • Mixing aggregated and non-aggregated arguments is the most common DAX-style error.
Loading progress…
LeadAfrikPublic Economics Hub