Tableau Course
Conditional Calculations
Conditional calculations evaluate a logical test on each row and return one value when true and another when false. They are the primary tool for building business segments, flags, custom labels, and conditional aggregations — all without modifying the source data.
IF, ELSEIF, ELSE — the Full Structure
The IF statement evaluates conditions in order from top to bottom and returns the result of the first condition that is true. Tableau stops evaluating as soon as it finds a match — so condition order matters when ranges overlap. Every IF block must close with END.
Each keyword in the block has a specific job:
THEN and stops — no further conditions are checked. There is always exactly one IF per block.The full skeleton always follows this shape: IF [condition] THEN [value] ELSEIF [condition] THEN [value] … ELSE [value] END. The IF and END are mandatory. ELSEIF and ELSE are optional but almost always present in real calculations.
Customer Tier — four tiers based on total orders per customer
IF [Orders] >= 20 THEN "Platinum"
ELSEIF [Orders] >= 10 THEN "Gold"
ELSEIF [Orders] >= 5 THEN "Silver"
ELSE "Bronze"
END
Orders = 14 → "Gold"
Orders = 7 → "Silver"
Orders = 2 → "Bronze" (falls through all conditions to ELSE)
Order matters: placing ELSEIF [Orders] >= 5 before ELSEIF [Orders] >= 10 would incorrectly classify Gold customers as Silver.
IIF — Inline If for Simple Two-Way Tests
IIF() is a compact single-line version of IF for cases with exactly two outcomes. It takes three arguments: the condition, the value if true, and the value if false. It also accepts an optional fourth argument — the value to return when the condition itself is unknown (NULL). IIF is cleaner than a full IF/ELSE block when you only need a binary true/false result.
Profitable Order — Boolean-style flag per row
IIF([Profit] > 0, "Profitable", "Loss")
Profit = -$45 → "Loss"
Profit = NULL → NULL (no fourth argument supplied — add "Unknown" as a fourth argument to catch NULLs explicitly)
IIF([Profit] > 0, "Profitable", "Loss", "Unknown") handles the NULL case cleanly.
CASE — Exact Value Matching
CASE evaluates a single expression and matches it against a series of exact WHEN values. It is structurally cleaner than IF/ELSEIF when all conditions test the same field for equality — no repeated field references, no comparison operators. Use IF when conditions involve ranges, inequalities, or multiple fields. Use CASE when matching exact string or integer values of one field.
Region Group — mapping four regions to two groups
CASE [Region]
WHEN "East" THEN "East Coast"
WHEN "West" THEN "West Coast"
WHEN "Central" THEN "Inland"
WHEN "South" THEN "Inland"
ELSE "Unknown"
END
"West" → "West Coast"
"Central" → "Inland"
"South" → "Inland"
Any unmatched value → "Unknown"
Multiple WHEN values can map to the same result — Central and South both return "Inland" here.
Logical Operators — AND, OR, NOT
Multiple conditions can be combined inside an IF statement using AND, OR, and NOT. AND requires both conditions to be true. OR requires at least one to be true. NOT inverts a condition. Parentheses control evaluation order when mixing AND and OR in the same expression — always use them to make the logic explicit.
High Value Flag — large orders with positive profit
IF [Sales] >= 1000 AND [Profit] > 0
THEN "High Value"
ELSE "Standard"
END
Sales = $1,500, Profit = -$30 → "Standard" (Sales qualifies but Profit fails)
Sales = $400, Profit = $200 → "Standard" (Profit qualifies but Sales fails)
Both conditions must be true simultaneously for AND to return true.
Conditional Aggregation — Filtering Inside a Measure
One of the most powerful conditional calculation patterns is filtering a Measure inside a SUM — counting or summing only the rows that meet a condition, without removing any rows from the view. This lets you show multiple conditional totals side by side on the same chart.
Sales of Profitable Orders Only — conditional SUM
SUM(IF [Profit] > 0 THEN [Sales] ELSE 0 END)
The outer SUM() adds up all included values — giving total Sales from profitable orders only.
The total Sales Measure still shows all orders. Both can sit on the same chart for direct comparison.
Using NULL instead of 0 in the ELSE clause also works — SUM ignores NULLs automatically.
Count of Loss-Making Orders — conditional COUNTIF pattern
SUM(IF [Profit] < 0 THEN 1 ELSE 0 END)
SUM() adds all the 1s — the result is the count of loss-making order rows.
This is Tableau's equivalent of Excel's COUNTIF function — there is no native COUNTIF in Tableau, so SUM(IF ... THEN 1 ELSE 0 END) is the standard pattern.
IF vs IIF vs CASE — Choosing the Right Tool
| Function | Use When | NULL Handling |
|---|---|---|
| IF / ELSEIF / ELSE | Multiple conditions, ranges, multi-field logic, more than two outcomes | Returns NULL if all conditions are NULL — ELSE catches most cases |
| IIF | Exactly two outcomes from a single condition — compact and readable | Supports an explicit fourth argument for the unknown/NULL case |
| CASE / WHEN | Exact value matching on a single field — cleaner than repeated ELSEIF equality checks | Returns ELSE value for NULLs — always include an ELSE clause |
Segment Labels Mockup — Conditional Colour by Tier
The conditional aggregation pattern — SUM(IF [condition] THEN [field] ELSE 0 END) — is Tableau's substitute for Excel's SUMIF and COUNTIF. The IF runs at row level before the SUM aggregates, so each row either contributes its value or contributes zero. Once you internalise this, you can build side-by-side comparisons for profitable vs unprofitable orders or Q1 vs Q2 sales on the same axis — all without duplicating data. Always include an ELSE clause in every CASE statement — a CASE without ELSE returns NULL for unmatched values silently.
Practice Questions
1. Write the calculated field that returns the total Sales from only profitable orders — without filtering out the loss-making rows from the view.
2. Write Tableau's equivalent of a COUNTIF that counts the number of order rows where Profit is negative.
3. A calculation maps six exact Ship Mode string values to three group labels. Why is CASE a better choice than IF/ELSEIF for this calculation?
Quiz
1. An IF/ELSEIF tier calculation places the condition [Orders] >= 5 before [Orders] >= 10. Customers with 14 orders are incorrectly classified as Silver instead of Gold. What causes this?
2. A calculated field must return "Profitable" when Profit is positive, "Loss" when negative, and "Unknown" when Profit is NULL. Which IIF syntax handles all three cases?
3. A CASE statement maps four known Ship Mode values to group labels but has no ELSE clause. A fifth Ship Mode value is added to the data. What happens to rows with the new Ship Mode?
Next up — Lesson 38: Level of Detail expressions — using FIXED, INCLUDE, and EXCLUDE to compute aggregations at a different granularity than the current view.