Tableau Case Function

Tableau Case Function is similar to the IF ELSE or Else If function. 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

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 if it is correct, the TRUE result will be returned.
  • result:
    • 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 Desktop function, we are going to use the below-shown data. As you can see, there are 15 records in this table.

Sample Table Report Example 1

Tableau Case Function Example

To demonstrate this, 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.

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 Tableau case statement.

CASE [Occupation]
    WHEN 'Management' THEN 'Administrators'
    WHEN 'Professional' THEN 'Sr. Software Developer'
    WHEN 'Skilled Manual' THEN 'Software Developer'
    ELSE 'Freshers'

Analysis of the above code

  • If Occupation = Management is TRUE, Administrator saved in Case1
  • 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
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 Create Table Report article to understand the steps involved in creating a table.

Tableau Case Function 4

Tableau Case Function Example 2

In this Tableau 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)
Case Code 5

Let me add this calculated field to the table.

Tableau Case Function 6