What is a Case Statement in Tableau?
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.
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'
ENDLet'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
WHENclause specifies the exact value it's looking for (e.g., 'Canada'). TheTHENclause 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
WHENconditions, it will be assigned the value you specify here (e.g., 'Other Regions'). Including anELSEstatement 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.
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".
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'
ENDStep 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'
ENDGrouping 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'
ENDTips 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
THENclauses must be the same data type. You can't haveTHEN 'High-Value'in one line andTHEN 500in 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
WHENhas aTHEN, all of your string values are enclosed in single quotes, and you remembered theENDstatement. - Unexpected NULLs: If you see "Null" appearing in your nice new dimension, it's almost certainly because you forgot the
ELSEclause. Some value in your original field didn't match any of yourWHENconditions, and Tableau didn't know what to do, so it assigned 'Null' by default. Adding anELSE '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
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.
How to Create a Photo Album in Meta Business Suite
How to create a photo album in Meta Business Suite — step-by-step guide to organizing Facebook and Instagram photos into albums for your business page.