The WHEN function in Tableau is not a standalone function; it is the conditional branch inside a CASE expression, used to return a value when a specific condition is met. For example, CASE [Region] WHEN 'West' THEN 'W' ELSE 'Other' END returns ‘W’ for West and ‘Other’ for everything else.
What Is the WHEN Clause in Tableau?
In Tableau, WHEN always appears as part of a CASE expression. The syntax is:
CASE [expression]
WHEN value1 THEN result1
WHEN value2 THEN result2
...
ELSE default_result
END
The WHEN clause compares the [expression] to a specific value. If it matches, Tableau returns the corresponding THEN result. If no WHEN matches, the ELSE result is returned. If there is no ELSE, Tableau returns NULL.
This is different from IF statements, which evaluate boolean conditions. CASE is best when you are comparing a single field to a set of discrete values.
How to Use WHEN in a Calculated Field
To use WHEN, you create a calculated field. Here are the steps:
- Right-click in the Data pane (or the Measures/Dimensions area) and select Create Calculated Field.
- Name the field, e.g., “Region Code”.
- Enter the
CASEexpression. - Click OK.
Example: To map full region names to short codes, use:
CASE [Region]
WHEN 'West' THEN 'W'
WHEN 'East' THEN 'E'
WHEN 'Central' THEN 'C'
ELSE 'Other'
END
This returns ‘W’ for West, ‘E’ for East, ‘C’ for Central, and ‘Other’ for any other region. The field can then be dragged onto shelves like Color or Label.
WHEN vs. IF: Which Should You Use?
Both CASE (with WHEN) and IF can perform conditional logic, but they have different strengths:
- Use CASE WHEN when you are comparing a single expression (like a field) to a list of specific values. It is more concise and often easier to read.
- Use IF when you need complex boolean conditions, such as
[Sales] > 1000 AND [Profit] > 0, or when using comparison operators like>,<, orCONTAINS. - Performance: In most cases, Tableau optimizes both equally, but
CASEcan be slightly faster for simple equality checks because it is a direct lookup.
Example of IF that cannot be done with CASE:
IF [Sales] > 10000 THEN 'High' ELSE 'Low' END
Example of CASE that is clearer than a long IF chain:
CASE [Category]
WHEN 'Furniture' THEN 'F'
WHEN 'Office Supplies' THEN 'O'
WHEN 'Technology' THEN 'T'
ELSE 'Unknown'
END
Real-World Examples of WHEN
Here are three practical scenarios where WHEN proves useful:
- Categorizing numeric bins: You can map order sizes to labels, but only if the values are exact. For ranges, use
IF. - Renaming dimension members: When a data source uses codes (e.g., ‘A’, ‘B’, ‘C’) and you want to display full names in a view.
- Creating a flag column: For example, marking priority customers:
CASE [Customer Tier] WHEN 'Gold' THEN 1 ELSE 0 END.
Example with a numeric field (exact match only):
CASE [Quantity]
WHEN 1 THEN 'Single'
WHEN 2 THEN 'Pair'
ELSE 'Multiple'
END
Note: CASE does not support ranges like WHEN [Quantity] > 5 – that would require IF.
Common Errors and How to Fix Them
When writing CASE expressions, users often encounter these mistakes:
- Missing END: Always close with
END. Without it, Tableau shows a syntax error. - Mismatched data types: The
WHENvalues must match the data type of the expression. For example, if[Region]is a string, use quotes around ‘West’. If it is a number, do not use quotes. - Using comparison operators in WHEN:
CASEonly checks equality. For>or<, useIF. - Forgetting ELSE: If you omit
ELSE, unmatched values returnNULL, which can cause unexpected blanks in views.
Example of a correct numeric CASE:
CASE [Tier]
WHEN 1 THEN 'First'
WHEN 2 THEN 'Second'
ELSE 'Other'
END
If [Tier] is a string, you would write WHEN '1'.
WHEN in Tableau Prep and Other Tools
The WHEN clause also appears in Tableau Prep, where you use a Calculated Field step. The syntax is identical. In Tableau Desktop, you also use it in calculated fields. In Tableau Server or Cloud, you can edit calculated fields in workbooks, but the syntax remains the same.
For Excel users, the equivalent is the SWITCH function (in newer versions) or nested IFs. In SQL, it is CASE WHEN – Tableau’s syntax is a simplified version of that.
Frequently Asked Questions
Can I use WHEN with a calculated field that returns a date? Yes, as long as the WHEN values are dates. For example: CASE DATEPART('quarter', [Order Date]) WHEN 1 THEN 'Q1' ... END – but note that DATEPART returns an integer, so the WHEN values must be numbers.
What is the difference between CASE and IIF? IIF is a shorthand for IF that takes three arguments: condition, true result, false result. It cannot handle multiple conditions as elegantly as CASE. For example, IIF([Sales]>1000,'High','Low') is equivalent to a simple IF, but CASE is better for multiple discrete values.
Does WHEN work with strings that contain special characters? Yes, just enclose the string in single quotes. For example, WHEN 'New York' works fine. If the string contains an apostrophe, double it: 'O''Brien'.
Can I use a parameter in a WHEN clause? Absolutely. You can compare a field to a parameter: CASE [Region] WHEN [Selected Region] THEN 'Highlight' ELSE 'Normal' END. This is useful for dashboards.
Why does my CASE return NULL for values I know exist? Check for trailing spaces or data type mismatches. For example, if the field is numeric but you wrote WHEN '1', it will never match. Also, ensure the field is not a measure that is being aggregated – CASE works on row-level fields, not aggregated values.
Is there a limit to the number of WHEN clauses? No hard limit, but Tableau may slow down with hundreds of clauses. In practice, keep it under 50 for readability. For many categories, consider a mapping table or a join instead.
Related
- SQL Split: How to Split Strings in SQL Queries
- Generate a 6 Digit Random Number: How to Do It
- Shortcut to Lock System: Fastest Ways on Windows & Mac
Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.
More in Formulas & functions
- Excel LAMBDA Function Examples for Reusable FormulasExcel LAMBDA lets you turn a formula into a reusable function: define it with =LAMBDA(parameters, calculation), save it…
- Excel Formula for a Future Date: Add Days or MonthsIf the starting date is in A2 and the number of days to add is in B2, enter…
- Excel Split Function: How TEXTSPLIT Returns an ArrayExcel does not have a worksheet function named SPLIT; in Microsoft 365 for Windows, use TEXTSPLIT to divide…
- Aggregate Excel Columns with Formulas and PivotTablesTo aggregate Excel columns, use a function such as SUM, AVERAGE, COUNT, MIN, or MAX across the cells…
- Insert a Checkbox in Excel Using a Formula: Step-by-StepTo show a formula-driven checkbox in Excel, put a TRUE or FALSE value in A2 and, with that…
- COUNTIF Is Blank? Use These FormulasThe direct answer: there is no COUNTIF function that counts truly blank cells by itself, but you can…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.