How to Use WHEN in Tableau

Cody Schneider8 min read

Organizing your data logically is the first step toward creating a clear and insightful analysis, and Tableau’s CASE WHEN function is one of the best tools for the job. It allows you to create custom groups and categories on the fly, transforming messy data into a structured report. This article will walk you through exactly how to use the CASE WHEN statement, with practical examples to get you started.

GraphedGraphed

Build AI Agents for Marketing

Build virtual employees that run your go to market. Connect your data sources, deploy autonomous agents, and grow your company.

Watch Graphed demo video

What is the CASE WHEN Function in Tableau?

The CASE WHEN function is a powerful conditional statement that lets you define a series of conditions and return a specific value when a condition is met. Think of it like a set of instructions: "If you see this value, label it as X. If you see that other value, label it as Y. And for everything else, label it as Z."

While Tableau also offers IF-THEN-ELSE statements, CASE WHEN is often a cleaner and more readable choice, especially when you're dealing with multiple distinct conditions for a single field. Instead of nesting multiple ELSEIF statements, which can become confusing, CASE WHEN lines up each condition neatly.

The Anatomy of a CASE WHEN Statement

Before writing one, let's break down the basic syntax. Understanding each piece makes it much easier to write and troubleshoot your own statements.

CASE [Field Name]
    WHEN 'Value1' THEN 'Result1'
    WHEN 'Value2' THEN 'Result2'
    WHEN 'Value3' THEN 'Result3'
    ELSE 'Default Result'
END
  • CASE [Field Name]: This initiates the statement and specifies the field you want to evaluate. For example, CASE [Country].
  • WHEN 'Value': This is the condition. It checks for a specific value within the field you defined. For example, WHEN 'USA'.
  • THEN 'Result': This is what you want to output if the WHEN condition is true. For example, THEN 'North America'.
  • ELSE 'Default Result': This is an optional but highly recommended clause. It specifies a fallback value for any records that don't match any of the WHEN conditions. Without it, unmatched values will be assigned NULL.
  • END: This is crucial! It closes the entire CASE statement. Forgetting the END is one of the most common rookie mistakes.

A Step-by-Step Guide to Creating Your First Calculation

Theory is great, but the best way to learn is by doing. Let's create a simple calculated field to categorize sales deals into different sizes based on their value.

Imagine you have a "Deal Size" field with numerical values, and you want to group them into 'Small', 'Medium', and 'Large' buckets.

Free PDF · the crash course

AI Agents for Marketing Crash Course

Learn how to deploy AI marketing agents across your go-to-market — the best tools, prompts, and workflows to turn your data into autonomous execution without writing code.

Step 1: Create a New Calculated Field

In your Tableau worksheet, right-click on any field in the Data pane (the left-hand sidebar) and select Create > Calculated Field. You can also go to the Analysis menu at the top and select Create Calculated Field. This will open the calculation editor.

Step 2: Name Your Calculated Field

Give your new field a descriptive name at the top of the editor. Something like "Deal Size Category" is perfect because it clearly explains what the field does.

Step 3: Write the CASE WHEN Statement

Here, we'll need to use a slightly different CASE structure that doesn't reference a single field at the start. Instead, it evaluates a series of logical expressions. This version is incredibly flexible.

Type the following logic into the editor:

CASE
    WHEN [Deal Size] < 5000 THEN 'Small Deal'
    WHEN [Deal Size] >= 5000 AND [Deal Size] < 20000 THEN 'Medium Deal'
    WHEN [Deal Size] >= 20000 THEN 'Large Deal'
    ELSE 'Other'
END

Notice how each WHEN is a full logical test. Tableau evaluates them sequentially from top to bottom. Once a condition is met, it returns the corresponding THEN value and stops, so the order matters if you have overlapping conditions.

Step 4: Save and Use Your New Field

Click OK to save the calculated field. You'll now see "Deal Size Category" in your Data pane under Dimensions. You can drag this new field onto Rows, Columns, or even the Color mark on the Marks card to start segmenting your visualizations by these new categories.

Practical Examples and Common Use Cases

Now that you have the basics down, let's explore some common scenarios where CASE WHEN really shines.

1. Grouping Geographic Data

One of the most frequent uses is to consolidate countries into larger regions or territories. This is perfect for high-level summary dashboards.

CASE [Country]
    WHEN 'United States' THEN 'North America'
    WHEN 'Canada' THEN 'North America'
    WHEN 'Mexico' THEN 'North America'
    WHEN 'United Kingdom' THEN 'Europe'
    WHEN 'Germany' THEN 'Europe'
    WHEN 'France' THEN 'Europe'
    ELSE 'Other Regions'
END
GraphedGraphed

Build AI Agents for Marketing

Build virtual employees that run your go to market. Connect your data sources, deploy autonomous agents, and grow your company.

Watch Graphed demo video

2. Standardizing Messy Data

Your source data isn't always clean. You might have inconsistent capitalization, typos, or different terms for the same thing. CASE WHEN is an excellent clean-up tool.

Imagine a "Subscription Plan" field with values like 'pro', 'professional', and 'Pro Plan'. You can standardize them like this:

CASE [Subscription Plan]
    WHEN 'pro' THEN 'Pro Plan'
    WHEN 'professional' THEN 'Pro Plan'
    WHEN 'Pro Plan' THEN 'Pro Plan'
    WHEN 'starter' THEN 'Starter Plan'
    WHEN 'basic' THEN 'Starter Plan'
    ELSE 'Unknown'
END

3. Creating Custom Date Periods

You can use CASE WHEN with date functions to group records into dynamic time-based buckets. This example flags sales from this year versus last year.

CASE
    WHEN YEAR([Order Date]) = YEAR(TODAY()) THEN 'This Year'
    WHEN YEAR([Order Date]) = YEAR(TODAY()) - 1 THEN 'Last Year'
    ELSE 'Older'
END

4. Bucketing Customer Segments

Much like our sales deal example, you can segment customers based on their purchasing behavior or other attributes.

CASE [Customer Segment]
    WHEN 'New Customers' THEN 'Growth'
    WHEN 'Returning Customers' THEN 'Retention'
    WHEN 'Churned Customers' THEN 'Risk'
    ELSE 'Uncategorized'
END

Tips and Best Practices for Writing CASE Statements

While CASE WHEN is fairly straightforward, following a few best practices will save you from common headaches.

Always Include an ELSE Clause

It's tempting to skip the ELSE if you think you've covered every possibility, but don't. Data sources can change, or you might overlook a value. An ELSE clause acts as a safety net, catching any unexpected values and grouping them into a category like 'Other' or 'Unknown'. This prevents them from becoming NULL values that disappear from your charts unexpectedly.

Free PDF · the crash course

AI Agents for Marketing Crash Course

Learn how to deploy AI marketing agents across your go-to-market — the best tools, prompts, and workflows to turn your data into autonomous execution without writing code.

Ensure Data Types Match

All values specified in your THEN statements must be of the same data type. You cannot have THEN 'Big Sale' in one line and THEN 100 in another. One returns a string (text), and the other returns an integer (number). Tableau will give you an error, usually something like "Expected type string, found integer." Stick to one output type for the entire calculation.

CASE is Evaluated in Order

Remember that Tableau processes the WHEN conditions from top to bottom and stops at the first true one. If you have a record that could meet multiple conditions, it will only be assigned the result of the first condition it satisfies. Be mindful of this when structuring logic, especially with numerical ranges.

Don't Forget the END

It bears repeating: every single CASE statement needs to be closed with an END keyword. If your calculation shows an error and you can't see anything wrong with your logic, double-check that you have END at the very end.

Troubleshooting Common Errors

  • "All expressions must have the same type" or "Expected type [X], found [Y]": This is the data type mismatch error we discussed above. Check all your THEN statements and make sure they all output the same type of data (all strings, or all numbers, or all dates).
  • "...is being called with (string, integer), did you mean (string, string)?": This is another common variation of the data type error. Review the values in each WHEN and THEN line carefully.
  • Syntax Errors: General errors often come from small typos. Look for missing colons (:) which are not used in Tableau's CASE, missing keywords like WHEN or THEN, or the forgotten END.

Final Thoughts

In short, Tableau's CASE WHEN statement is an incredibly flexible tool for bringing conditional logic right into your visualizations. Whether you are standardizing messy data, grouping values into reporting categories, or creating custom segments, it allows you to manipulate and organize your data without having to modify the original source.

While Tableau is a fantastic tool for deep analysis, we found that marketing and sales teams often spend too much time getting their data ready and writing calculations rather than acting on insights. We created Graphed to simplify this entire workflow. By connecting directly to sources like Google Analytics, Shopify, and Salesforce, we let you create real-time dashboards and reports just by asking questions in plain English - no calculated fields necessary.

Related Articles