How to Use CASE Function in Tableau
The CASE function in Tableau is one of the most practical tools for organizing your data exactly how you want it. It’s a clean and efficient way to create custom groups, reclassify messy data, or build new categories on the fly without changing your original data source. This article will walk you through what the CASE function is, its basic structure, and several real-world examples to help you start using it today.
What is a CASE Function, Anyway?
Think of the CASE function as a neater, more organized version of a series of IF-THEN-ELSE statements. It looks at the values in a single field you specify and, based on what it finds, returns a corresponding result you define. It works like a sorting machine: if a record matches "Condition A," it goes into "Bin A", if it matches "Condition B," it goes into "Bin B," and so on.
So, when would you use it? The most common scenarios include:
- Grouping countries into regions: Turning a long list of countries ('USA', 'Canada', 'Mexico') into tidy continent-level groups ('North America').
- Categorizing sales performance: Bucketing products into performance tiers ('High Performer', 'Average', 'Underperformer') based on their category name.
- Creating custom channel groupings: Consolidating messy UTM source data ('google', 'cpc', 'g-ads') into a clean 'Paid Search' channel.
The main advantage of CASE over a bunch of nested IF statements is readability. When you're checking a single field against a long list of possible values, a CASE statement is much easier for you (and your teammates) to read and debug.
The Basic Syntax
Every CASE statement follows a consistent and logical structure. Once you get the hang of it, you'll be writing them in seconds. Here is the basic template for a CASE function in Tableau's calculated field editor:
CASE [Your Field]
WHEN "Value 1" THEN "Result 1"
WHEN "Value 2" THEN "Result 2"
WHEN "Value 3" THEN "Result 3"
ELSE "Default Result"
ENDLet’s break that down piece by piece:
CASE [Your Field]: This kicks things off. You tell Tableau which field (or "dimension") you want to evaluate. This could be [Country], [Product Category], or [UTM Source].WHEN "Value": This is your condition. The statement looks for an exact match to the value you specify here. For example,WHEN "USA".THEN "Result": If theWHENcondition is met, this is the output Tableau will return. For example,THEN "North America". You can have dozens ofWHEN/THENlines.ELSE "Default Result": This is the optional but highly recommended "catch-all." If a value in your field doesn't match any of yourWHENconditions, it will be assigned this default result. It’s perfect for grouping all other values under a label like "Other" or "Uncategorized."END: This simply tells Tableau that your CASE statement is finished. Don't forget it, or you'll get an error!
Practical Examples: Putting the CASE Function to Work
The best way to learn is by doing. Here are a few step-by-step examples that show how you can use the CASE function to solve common data analysis problems.
Example 1: Grouping Countries into Continents
Let's say your dataset includes a long list of individual countries, but for your dashboard, you want to show a map or bar chart summarizing sales by continent. A CASE statement is perfect for this.
The Goal: Create a new field called "Continent" from the existing "Country" field.
- Open the calculated field editor in Tableau (Analysis > Create Calculated Field).
- Name your new field something descriptive, like "Continent".
- Enter the following logic into the formula box:
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"
WHEN "Japan" THEN "Asia"
WHEN "China" THEN "Asia"
ELSE "Other"
ENDClick "OK," and you’ll see your new "Continent" dimension in the data pane. Now you can drag this field onto your sheet to instantly aggregate all your metrics (like Sales or User Count) at the continent level.
Example 2: Creating Custom Marketing Channel Groups
Pretty much every marketer has faced the chaos of UTM parameters. Your data might contain sources like "google," "t.co," "facebook.com," and "linkedin," but you want to report on broad channels like 'Paid Search,' 'Organic Social,' and 'Referral'.
The Goal: Clean up the [UTM Source] field and group it into clean marketing channels.
- Create a new calculated field and name it "Marketing Channel".
- Write a CASE statement to group the different sources. You can even combine it with other functions like
CONTAINS()if you need more flexibility, but for direct matches, CASE is king.
CASE [UTM Source]
WHEN "google" THEN "Organic Search"
WHEN "bing" THEN "Organic Search"
WHEN "linkedin.com" THEN "Organic Social"
WHEN "facebook" THEN "Organic Social"
WHEN "t.co" THEN "Organic Social"
WHEN "mailchimp" THEN "Email"
ELSE "Direct / Other"
ENDNow, instead of building a bar chart with 50 different traffic sources, you can build one with five clean channels, making it much easier to see which strategies are performing best.
Example 3: Categorizing Product Priority Level
Imagine you manage a product inventory. You have a [Category] field with values like "Essential Supplies," "Seasonal Goods," and "Speciality Items." You could use a CASE statement to assign a handling priority level to each.
The Goal: Label each product with a priority level based on its category.
- Create a new calculated field named "Handling Priority".
- Use the CASE function to assign a priority status to each product category.
CASE [Category]
WHEN "Essential Supplies" THEN "High Priority"
WHEN "Top Sellers" THEN "High Priority"
WHEN "Seasonal Goods" THEN "Medium Priority"
WHEN "Standard Stock" THEN "Normal Priority"
ELSE "Low Priority"
ENDThis calculated field lets you filter your dashboards to show only high-priority items or see your inventory broken down by handling needs.
A Note on CASE vs. IF Statements
New Tableau users often wonder when to use a CASE statement versus an IF statement. They can sometimes accomplish similar things, but there's a key difference.
- Use
CASEfor checking a single field against a list of exact matching values. This is what we did in all the examples above. It is simpler and faster for Tableau to compute in these scenarios. - Use
IFfor more complex logical tests. This is the right choice when you need to check for ranges of values (e.g., numbers greater than or less than something) or evaluate conditions across multiple fields at once.
For example, if you wanted to categorize sales deals into sizes ('Small', 'Medium', 'Large') based on their value, you couldn't use a simple CASE statement because you're dealing with numerical ranges, not exact values. You would need to use an IF...THEN...ELSEIF statement instead:
IF [Sales Value] < 1000 THEN "Small Deal"
ELSEIF [Sales Value] >= 1000 AND [Sales Value] < 5000 THEN "Medium Deal"
ELSEIF [Sales Value] >= 5000 THEN "Large Deal"
ENDRemember this simple rule: Single field, exact values? Use CASE. Complex conditions or numeric ranges? Use IF.
Common Pitfalls and Quick Tips
As you get started, you might run into a few common issues. Here are some quick tips to help you avoid them.
- Forgetting the
END: This is the most frequent error. If Tableau gives you an error about an "unterminated" statement, check that you have anENDat the close of your function. - Mixing Data Types: All of the results in your
THENstatements must be the same data type. You can’t have one return a string ("High Performer") and another return a number (2). Make sure your outputs are consistent. - Handling Nulls: If a value is NULL in your original field, the CASE statement won't match any of your
WHENconditions. It will automatically fall into theELSEbucket. This is another great reason to always include anELSEclause to catch anything unexpected. - Case Sensitivity: Tableau's calculations are sensitive to capitalization. "USA" is not the same as "usa". To avoid issues, you can wrap your field in the
LOWER()function to standardize it, like this:CASE LOWER([Country]) WHEN "usa" THEN ...
Final Thoughts
The CASE function is a fundamental building block for moving beyond basic charts in Tableau. By mastering it, you can take control of your data, cleaning up messy fields and creating new categories that help you tell a clearer story with your visualizations. It's a quick step that transforms your raw data into meaningful insights.
Of course, becoming proficient with tools like Tableau means learning syntax and spending time building calculated fields. Here at Graphed, we designed our platform to remove that friction completely. Instead of writing custom logic, our users connect their data sources and simply ask in plain English, "create a chart grouping sales by continent." Our AI handles all the underlying data transformations and visualizations, allowing your team to get from question to insight in seconds, not hours.
Related Articles
How to Enable Data Analysis in Excel
Enable Excel's hidden data analysis tools with our step-by-step guide. Uncover trends, make forecasts, and turn raw numbers into actionable insights today!
What SEO Tools Work with Google Analytics?
Discover which SEO tools integrate seamlessly with Google Analytics to provide a comprehensive view of your site's performance. Optimize your SEO strategy now!
Looker Studio vs Metabase: Which BI Tool Actually Fits Your Team?
Looker Studio and Metabase both help you turn raw data into dashboards, but they take completely different approaches. This guide breaks down where each tool fits, what they are good at, and which one matches your actual workflow.