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 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
- Input_Expression: This may be any column or an expression on which you want to perform CASE operation.
- test_condition: CASE statement 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 be returned as output.
- If they are not equal then default_result will be returned 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 Case function, we have to create a Calculated Field. In order 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, following window will be opened. Here, we renamed the default calculation name as case1.
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 code
- If Occupation = Management is TRUE then Administrator is saved in Case 1
- Occupation = Professional is TRUE, it will assign Sr. Software Developer
- Occupation = Skilled manual is TRUE then it assigns Software Developer
- If all the above conditions fail then, it will return Fresher
Let me add this Case 1 calculated field to the table (by dragging a 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 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