Tableau Case Function

Tableau Case Function is similar to the IF ELSE or Else If Statement. The Case function evaluates a series of conditional expressions based on the condition result, and it will return the output.

Tableau Case Function Syntax

The basic syntax behind the Case Statement or function 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 for this are:

  • Input_Expression: This may be any column or expression on which you want to operate.
  • test_condition: Desktop will compare this expression against the Input_Expression, and the TRUE result will be returned if it is correct.
  • Result:
    • If the test_condition equals 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 Tableau CASE function or statement with an example. To demonstrate this Desktop function, we will use the below-shown data. As you can see, there are 15 records in this table.

Sample Table Report Example 1

Tableau Case Function Statement Example

To demonstrate this, we have to create the Calculated Field. To create a calculated field, please navigate to the Analysis Tab and select the Create Calculated Field… option, as shown below.

Create a Calculated Field 2

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 the 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 Function or statement code

  • If Occupation = Management is TRUE, Administrator saved in Case1
  • Occupation = Professional is TRUE, and it will assign a Sr. Software Developer.
  • Occupation = Skilled manual is TRUE, then it assigns Software Developer
  • If all the above conditions fail, then it returns Fresher
Tableau Case Function Expressions 3

Let me add this Case1 calculated field to the table (by dragging the field to Rows Shelf) that we created earlier. Please refer to the Create Table Report article to understand the steps involved in creating a table.

Tableau Case Function 4

Tableau Case Function or Statement Example 2

In this case statement example, we will update the Yearly Income of an employee based on his profession.

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
Case Code 5

Let me add this calculated field to the table.

Tableau Case Function 6