Tableau If Statement

In report design, Tableau If Function is one of the most useful decision-making statements. The If function tests the condition, and depending upon the condition result, it will return the statement output.

Tableau contains three variants of if functions: If, If Else, and ElseIf statements. In this article, we will show you how to use these three functions with examples.

For this If function statement demonstration, we are going to use the data contained in the Global Super Store Excel Worksheet. So, please refer to the Connecting to Excel Files article to understand the connection settings.

Tableau If Statement Example

The If statement returns the result only if the given condition is True; otherwise, it returns nothing. To demonstrate this If function, we need a Calculated Field. To create a calculated field, please navigate to the 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 calculated field name as If Exam. For more functions, please visit the Functions article. The code is

IF(SUM([Profit]) > 0) THEN 'Performing Good'
END

Let me add this calculated field to the table (by dragging a 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.

From the above screenshot, you can see it is returning NULL values for the failed condition. To avoid this NULLS, you have to use If ELSE

Tableau If Else Statement Example

The If Else function will test the condition.

  • When the test condition is true, the statement after the THEN keyword will return
  • When it is False, the statement after the Else keyword will return.

If Else Statement Syntax

The syntax of the If Else statement is as follows:

IF <Expression> THEN <True_statement>
ELSE <False_statement>
END

In this if else function contains calculation, we are going to check the statement whether the Profit is greater than 0 or not.

  • If the condition is TRUE, then Performing Good will be returned
  • If it is FALSE, Bad Performance will be stored in the IfElse Exam field

The code that we used for this if else Statement contains is

IF(SUM([Profit]) > 0) THEN 'Performing Good'
ELSE 'Bad Performance'
END

Let me add this If Else calculated field to the table.

Tableau ElseIf Statement Example

The ElseIf function is handy for checking multiple conditions. Remember, these conditions will only execute if its previous IF or ELSEIF statement fails, and the syntax is as follows:

IF <Expression1> THEN <True_statement1>
ELSEIF <Expression2> THEN <True_statement2>
ESEIF <Expression3> THEN <True_statement3>
.....
ELSE <False_statement>
END

The ElseIf function sequentially executes the statement. It will check the first condition,

  • If the condition is TRUE, it executes the statement after the THEN keyword.
  • If it is FALSE, it checks the Next one (ElseIf condition), and so on.

The code that we used for this else if is below

IF(SUM([Profit]) > 6000000) THEN 'Profit'
    ELSEIF(SUM([Profit]) > 0) THEN 'Breakeven'
    ELSE 'Loss'
END

Let me add this Tableau If, Else, and elseif Statement outputs to the existing table