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 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 Tableau If function statement demonstration, we are going to use the data contained in the Global Super Store Excel Worksheet. So, Please refer Connecting to Excel Files article to understand the connection settings.
Tableau If Statement Example
The Tableau If statement returns the result only if the given condition is True; otherwise, it returns nothing.
To demonstrate this Tableau 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 calculation name as If Exam. 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 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 Function 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.
Tableau If Else Statement Syntax
The syntax of the If Else statement in Tableau is as follows:
IF <Expression> THEN <True_statement> ELSE <False_statement> END
In this Tableau 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 Tableau if else Statement contains is
IF(SUM([Profit]) > 0) THEN 'Performing Good' ELSE 'Bad Performance' END
Let me add this Tableau If Else calculated field to the table
Tableau ElseIf Function Example
The ElseIf function is handy to check multiple conditions. Remember, these Tableau conditions will only execute if its previous IF or ELSEIF statement fails.
The syntax of the ElseIf 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 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