What is a Case Statement in Tableau?

Cody Schneider8 min read

If you've ever found yourself with messy, uncategorized data in Tableau, the CASE statement is the function you need to know. It’s an essential tool for transforming raw data fields into clean, logical groups that make your dashboards clearer and your reports more insightful. This article explains exactly what a CASE statement is, how to write one, and how it can help you organize your data more effectively.

GraphedGraphed

Still Building Reports Manually?

Watch how growth teams are getting answers in seconds — not days.

Watch Graphed demo video

What Exactly Is a CASE Statement?

Think of a CASE statement as a sorting hat for your data. You give it a field (like "Country," "Lead Source," or "Product SKU") and then provide a series of instructions. It checks each row in that field one by one and assigns it to a new category based on the rules you set.

For example, imagine you have a list of lead sources from your CRM: 'Organic Search', 'Paid Search - Google', 'google / cpc', 'linkedin ads', 'webinar'. You could instruct a CASE statement:

  • If the source is 'Organic Search', put it in the "Organic" bucket.
  • If the source is 'Paid Search - Google' or 'google / cpc', put it in the "Paid Search" bucket.
  • If the source is 'linkedin ads', put it in the "Paid Social" bucket.
  • If the source is 'webinar', put it in the "Events" bucket.

The CASE statement systematically checks each lead source and assigns it to the appropriate new bucket, instantly giving you a clean dimension called "Marketing Channel" without altering your original data source.

The Basic Syntax of a Tableau CASE Statement

Learning syntax can feel intimidating, but the structure of a CASE statement is straightforward and logical. It's built on a series of "WHEN...THEN..." pairs.

Here’s the basic template:

CASE [Your Field]
WHEN 'value_1' THEN 'new_category_1'
WHEN 'value_2' THEN 'new_category_2'
WHEN 'value_3' THEN 'new_category_3'
ELSE 'fallback_category'
END

Let's break that down:

  • CASE [Your Field]: This is where you declare which dimension or measure you want to evaluate. It could be [Country], [Lead Status], or any other field in your dataset.
  • WHEN 'value_1' THEN 'new_category_1': This is a single condition. The WHEN clause specifies the exact value it's looking for (e.g., 'Canada'). The THEN clause defines what new value to assign if the condition is met (e.g., 'North America'). You can have as many conditions as needed.
  • ELSE 'fallback_category': This is your catch-all. If a value in your field doesn't match any of your WHEN conditions, it will be assigned the value you specify here (e.g., 'Other Regions'). Including an ELSE statement is crucial to avoid having "Null" values suddenly appear in your new dimension.
  • END: This is the final and most important part. It tells Tableau that your CASE statement is complete. If you forget this, Tableau will show an error.
GraphedGraphed

Still Building Reports Manually?

Watch how growth teams are getting answers in seconds — not days.

Watch Graphed demo video

CASE vs. IF Statements: When to Use Which?

Tableau also offers IF statements, which can accomplish similar goals, leading to a common question: which one should you use?

The answer comes down to what you're trying to test.

  • Use a CASE statement for exact equality on a single field. It's perfect when you have a list of precise values you want to group. For example, checking the [State] field and grouping "California," "Oregon," and "Washington" into a "West Coast" region. The syntax is cleaner and often runs more efficiently for these types of tasks.
  • Use an IF statement for complex logic, ranges, or conditions involving multiple fields. IF statements give you more flexibility. You can use operators like greater than (>), less than (<), or "AND" and "OR" connectors. For example, grouping sales deals into sizes: IF [Deal Size] > 50000 THEN 'Enterprise' or checking two fields at once: IF [Country] = "USA" AND [Account Type] = "Active" THEN 'US Active Customer'.

Think of it this way: CASE is a specialist for exact-match sorting, while IF is a generalist for complex logical tests. When CASE fits the job, it’s usually the better, more readable choice.

Step-by-Step Guide: Grouping Marketing Channels in Tableau

Let’s walk through a practical, real-world example of creating clean marketing channels from a messy UTM Source dimension.

Imagine your data source has a [UTM Source] field with values like 'google', 'facebook', 'linkedin', 'bing', 'hubspot', and some typos like 'Google'. Our goal is to create a new field called "Channel Group" containing 'Paid Search', 'Paid Social', and 'Marketing Automation'.

Step 1: Open the Calculated Field Editor

In your Tableau workbook, navigate to the top menu and click Analysis > Create Calculated Field…. This will open a new window where you can write custom formulas.

Step 2: Name Your New Field

At the top of the calculated field window, give your new field a descriptive name. Let’s call it "Channel Group".

GraphedGraphed

Still Building Reports Manually?

Watch how growth teams are getting answers in seconds — not days.

Watch Graphed demo video

Step 3: Write the CASE Statement

In the main formula box, type out your logic. To handle potential capitalization inconsistencies (like 'google' vs. 'Google'), we can wrap our field in the LOWER() function. This converts every value to lowercase before evaluating it.

CASE LOWER([UTM Source])
WHEN 'google' THEN 'Paid Search'
WHEN 'bing' THEN 'Paid Search'
WHEN 'facebook' THEN 'Paid Social'
WHEN 'linkedin' THEN 'Paid Social'
WHEN 'hubspot' THEN 'Marketing Automation'
ELSE 'Other'
END

Step 4: Check for Errors and Apply

After typing the formula, look at the bottom of the editor. Tableau will display a message that says "The calculation is valid." If it shows an error, double-check your syntax for missing quotes, commas, or the final END keyword. Once it's valid, click OK.

Step 5: Use Your New Dimension

Look in the Data pane on the left side of your screen. Under "Dimensions," you'll see your newly created field, Channel Group. You can now drag and drop this field into your visualizations just like any other field to see your metrics neatly grouped by channel!

More Practical Examples for Sales and E-commerce

Categorizing Salesforce Lead Statuses into Funnel Stages

Clean up dozens of granular lead statuses into simple, high-level funnel stages for clearer pipeline reporting.

CASE [Lead Status]
WHEN 'New' THEN '1. Top of Funnel'
WHEN 'Contacted' THEN '1. Top of Funnel'
WHEN 'Working' THEN '2. Middle of Funnel'
WHEN 'Nurturing' THEN '2. Middle of Funnel'
WHEN 'Qualified' THEN '3. Bottom of Funnel'
WHEN 'Contracts' THEN '3. Bottom of Funnel'
ELSE 'Out of Funnel'
END
GraphedGraphed

Still Building Reports Manually?

Watch how growth teams are getting answers in seconds — not days.

Watch Graphed demo video

Grouping Products into Broad Categories in a Shopify Export

Simplify product analytics by grouping individual SKUs based on their high-level category name.

CASE [Product Type]
WHEN 'T-Shirt' THEN 'Apparel'
WHEN 'Hoodie' THEN 'Apparel'
WHEN 'Coffee Mug' THEN 'Accessories'
WHEN 'Sticker' THEN 'Accessories'
WHEN 'Poster' THEN 'Home Goods'
ELSE 'Miscellaneous'
END

Tips for Troubleshooting Common Errors

Writing your first few CASE statements is a great start, but you might run into some speed bumps. Here are a few common issues and how to fix them:

  • Mixed Data Types: All outcomes in your THEN clauses must be the same data type. You can't have THEN 'High-Value' in one line and THEN 500 in another. Decide if you want your new category to be text (strings) or numbers (integers) and stick with it.
  • Syntax Errors: Small typos are the most common source of errors. Carefully check that every WHEN has a THEN, all of your string values are enclosed in single quotes, and you remembered the END statement.
  • Unexpected NULLs: If you see "Null" appearing in your nice new dimension, it's almost certainly because you forgot the ELSE clause. Some value in your original field didn't match any of your WHEN conditions, and Tableau didn't know what to do, so it assigned 'Null' by default. Adding an ELSE 'Other' will catch these cases and fix the problem.

Final Thoughts

Mastering CASE statements is a huge step up for any Tableau user. They provide a simple yet powerful way to clean, categorize, and organize your data, turning messy reports into clean dimensions for analysis. While they are straightforward once you get the hang of them, they represent an essential part of traditional BI – the core of your analysis and dashboard building.

This is one reason we've built Graphed. Learning BI tool syntax functions is a time-consuming manual process that can slow teams down. Instead of having to learn and write calculations yourself, we built an AI data analyst that allows you to simply describe what you want to see. You can ask, "Show me my sales performance by funnel stage" in plain English, and Graphed automatically connects to your sources, performs the necessary calculations, and creates interactive dashboards for you. It's reporting without the steep learning curve, designed so anyone on the team can analyze data in seconds, not hours.

Related Articles