How to Use CASE Statement in Tableau

When I was building a Tableau dashboard for sales analysis, I faced a situation where the Sub-Category field had too many detailed values, such as Chairs, Tables, Bookcases, and Furnishings, etc.

As a solution for this, I used a CASE statement to group them into simple categories like Seating, Storage, and Other. By using the CASE function in a formula, I was able to filter out the sub-categories we want to focus on while still keeping the rest of them.

In this tutorial, I’ll explain how to use a CASE statement in Tableau with the help of examples.

Case Statements in Tableau

In Tableau, a CASE statement is a type of calculated field that functions like an “if-then” condition, allowing us to test conditions and return specific results. Instead of writing multiple IF statements, we can use CASE to check a field for different values and assign an output.

For example, we can use a CASE statement to classify subcategories into custom groups like Seating, Furnishing, and Other. It makes calculations easier to maintain when we’re working with multiple conditions.

Use a Case Statement in Tableau

In this example, we will use a CASE statement to group Sub-Categories into custom groups. For example, we’ll put chairs and tables in Seating, Bookcases and Furnishings in Storage, and the rest into the Other group.

Now connect the Tableau with the Superstore data set and follow the steps below to see how we can use the CASE statement in Tableau.

  1. Create a bar chart to show the subcategory sales, and for this, drag Sub-Category to Columns and Sales to Rows.
Use Case Statement in Tableau
  1. Now, we will create a calculated field using the CASE statement. For this, click on the Analysis tab and select Create Calculated Field.
Create a Calculated field using CASE statement in Tableau
  1. Enter the Name for the calculated field and enter the formula below to group the sub-category, and click OK.
CASE [Sub-Category]
  WHEN "Chairs" THEN "Seating"
  WHEN "Tables" THEN "Seating"
  WHEN "Bookcases" THEN "Storage"
  WHEN "Furnishings" THEN "Storage"
  ELSE "Other"
END
Tableau Case statement
  1. Now, add the calculated field “Product Analysis” to the Columns. With this, sub-categories will be grouped in the Seating, Storage, and Other.
Tableau Formula Case statement
  1. For a better comparison view, drag the calculated field in the Color card. This will assign a different color to each group that we have created.
Group fields using Case statement in Tableau
  1. To remove the calculated field column from the chart, right-click on the label and select Hide Field Labels for Columns.
Use of Case statement in Tableau
  1. At last, click on the Text icon in the navigation bar to show sales for the grouped values in the bar chart.
Tableau Case Statement Calculation

This way, we can use the CASE statement to create a calculated field that can be used as a dimension, which we can add to Rows, Columns, Filters, Color, or even use in another calculated field.

Use Case Statement with Multiple Conditions in Tableau

In Tableau calculated fields, we can also use nested CASE statements with multiple conditions to define complex logic using a calculated field.

In this example, we will use a nested CASE statement with multiple conditions to group the Ship mode into two categories. In this, we will divide it into two categories: Fast shipping and Regular shipping, to analyze Sales from both groups.

Now, follow the steps below to create and use the calculated field using the CASE statement with multiple conditions.

  1. To create the calculated field, click on the Analysis tab -> Create Calculated Field.
Case statement with multiple condition in Tableau
  1. Enter the label for the calculated field as “Shipping Group,” enter the formula below, and click OK.
CASE [Ship Mode]
  WHEN "First Class" THEN "Fast Shipping"
  WHEN "Same Day" THEN "Fast Shipping"
  WHEN "Second Class" THEN "Regular Shipping"
  WHEN "Standard Class" THEN "Regular Shipping"
  ELSE "Other"
END
Use multiple case statement in Tableau calculated field

The above CASE formula categorizes the Ship Mode field into groups: First Class and Same Day are assigned to Fast Shipping, then Second Class and Standard Class are assigned to Regular Shipping, and all other options are labeled as Other.

  1. Now add the calculated field Shipping Group to the Rows and Sales to the Columns.
Create a Nested Case formula in Tableau

Now, we can see the groups that we have divided in Shipping mode categories using the CASE statement.

  1. In this grouping, First Class and Same Day are assigned to Fast Shipping, then Second Class and Standard Class are assigned to Regular Shipping. To view all values in the bars, we can add the ship mode to the column or in the color card for the stacked chart view.
Tableau Case statement with multiple conditions

This way, we can use a CASE statement with multiple conditions in Tableau and show multiple detailed values in simplified categories.

Conclusion

In this Tableau tutorial, we learned how to use the CASE statement in Tableau to group values into simple categories. In the above examples, we have used a CASE statement in the formula field by grouping sub-categories.

Then, in the next example, we used the nested CASE statement with multiple conditions to create custom groups like Fast Shipping and Regular Shipping.

By following the above example, you can use CASE statements in Tableau calculated fields and use them in charts, tables, or dashboards to group data and apply conditions.

You may also like to read:

Agentforce in Salesforce

DOWNLOAD FREE AGENTFORCE EBOOK

Start with AgentForce in Salesforce. Create your first agent and deploy to your Salesforce Org.

Salesforce flows complete guide

FREE SALESFORCE FLOW EBOOK

Learn how to work with flows in Salesforce with 5 different real time examples.