Tableau Case Function is similar to the IF ELSE or Else If function. Case function in Tableau evaluates series of conditional expressions and based on the condition result, and it will return the output.
Tableau Case Function Syntax
The basic syntax behind the Case Statement in Tableau is as shown below:
CASE <Input_Expression> WHEN <test_condition1> THEN result_1 WHEN <test_condition2> THEN result_2 ......... ELSE default_result END
Arguments of this Tableau case statement are:
- Input_Expression: This may be any column or expression on which you want to perform the CASE operation.
- test_condition: Tableau CASE function will compare this expression against the Input_Expression, and if it the TRUE result will be returned.
- If the test_condition is equal to Input_Expression, then this will return as an output.
- If they are not equal, default_result will return as output.
In this article, we will show you how to write a CASE function in Tableau with an example. To demonstrate this Tableau case function, we are going to use the below shown data.
As you can see, there are 15 records in this table.
Tableau Case Function Example 1
To demonstrate this Tableau Case function, we have to create the Calculated Field. To create a calculated field, please navigate to Analysis Tab and select the Create Calculated Field… option, as shown below.
Once you click on the Create Calculated Field… option, the following window will be opened. Here, we renamed the default calculation name as case1. And you can see the code of Tableau case statement
CASE [Occupation] WHEN 'Management' THEN 'Administrators' WHEN 'Professional' THEN 'Sr. Software Developer' WHEN 'Skilled Manual' THEN 'Software Developer' ELSE 'Freshers' END
Analysis of the above Tableau case statement code
- If Occupation = Management is TRUE, Administrator saved in Case 1
- Occupation = Professional is TRUE, and it will assign Sr. Software Developer
- Occupation = Skilled manual is TRUE then it assigns Software Developer
- If all the above conditions fail then, it returns Fresher
Let me add this Case 1 calculated field to the table (by dragging the field to Rows Shelf) that we created earlier. Please refer Create Table Report article to understand the steps involved in creating a table
Tableau Case Statement Example 2
In this example, we will update the Yearly Income of an employee based on his profession using this Tableau case function
CASE [Occupation] WHEN 'Management' THEN ([Yearly Income] + 100000) WHEN 'Professional' THEN ([Yearly Income] + 50000) WHEN 'Skilled Manual' THEN ([Yearly Income] + 35000) ELSE ([Yearly Income] + 25000) END
Let me add this calculated field to the table