When Function in Tableau? Uses & Examples

Formulas & functions · Updated November 23, 2026

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:

  1. Right-click in the Data pane (or the Measures/Dimensions area) and select Create Calculated Field.
  2. Name the field, e.g., “Region Code”.
  3. Enter the CASE expression.
  4. 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 >, <, or CONTAINS.
  • Performance: In most cases, Tableau optimizes both equally, but CASE can 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 WHEN values 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: CASE only checks equality. For > or <, use IF.
  • Forgetting ELSE: If you omit ELSE, unmatched values return NULL, 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

Try it: the Formula Explainer: paste any formula and see each function and reference in plain English.

More in Formulas & functions

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.