Tableau Lesson 37 – Conditional Calculations | Dataplexa
Section IV — Lesson 37

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:

IF
The opening keyword. States the first condition to test. If this condition is true, Tableau returns the value after THEN and stops — no further conditions are checked. There is always exactly one IF per block.
ELSEIF
Only reached if the conditions above it were all false. States an additional condition to test. You can chain as many ELSEIF clauses as needed — each is a new branch. Like IF, it returns its THEN value and stops as soon as its condition is true. ELSEIF is optional — use it only when you have more than two outcomes.
ELSE
The catch-all. Only reached if every condition above it was false. Does not take a condition — it returns its value for any row that did not match any earlier branch. ELSE is optional but strongly recommended. Without it, any unmatched row returns NULL silently.
END
Closes the IF block. Every IF statement must end with END — Tableau will show a syntax error if it is missing.

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

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")

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

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

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)

Count of Loss-Making Orders — conditional COUNTIF pattern

SUM(IF [Profit] < 0 THEN 1 ELSE 0 END)

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

Customer Tier — conditional calculated field driving Color channel
Platinum ≥20 orders 847 customers Gold 10–19 orders 1,204 customers Silver 5–9 orders 2,118 customers Bronze <5 orders Customers
📌 Teacher's Note

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.