Introduction to Tableau IF statement
In order to test a condition for accomplish a task based on the value returned post testing of the condition. Tableau provides a logical statement to check a particular condition, represented by an expression, against a certain value or assumption, to determine whether a given condition is TRUE or FALSE, based on which certain tasks get performed if the condition is met, so as to facilitate correct and effective decision making is termed as Tableau IF statement.
Tableau IF Statement:
Logical calculations help you in determining if certain given conditions are true or false. In order to do this decision making it is important to have a function to this. In Tableau, there are different functions that are provided. The IF function helps in determining if a series of expressions are true then the value for the first expression will be returned. In Tableau, there are three variants of the IF function. It has IF, IF-ELSE, and ELSE IF.
1) The IF Condition:
The IF Condition returns the result only if the given condition is true. If this condition is not true, then it returns nothing.
Example: If [Marks] > 35 then ‘PASS’
2) The IF ELSE function:
This condition also tests for particular conditions. If the test condition in IF is true, then the statement after the THEN keyword will be returned. If this is False, then the statement after Else keyword is returned. The syntax for If Else in Tableau is as follows:
IF <Expression> THEN <True Statement>
ELSE <False Statement>
3) The ELSE IF Function:
Whenever there is a requirement of checking multiple conditions the Else If the condition is very useful. The Else If conditions can be used only when the is a previous If or Else if statement which is being failed. This function executes in a sequential manner. It starts checking from the first condition. If the first condition is true then it will execute the statement which is present after the Then keyword. If this condition is false then it will check the next Else if Condition. It will keep on continuing with the else if statements until the condition are satisfied.
IF <Expression1> THEN <True_statement1>
ELSEIF <Expression2> THEN <True_statement2>
ESEIF <Expression3> THEN <True_statement3>
The IIF Function
- IIF is a useful function which helps in getting logical functions done. When using this function there are arguments which are to be passed and the argument which is expected is should be a Boolean. If it is not Boolean, then it can be the result of the logical expressions. These results can be further used to provide a Boolean result. If the results of this logical expression evaluate to be True, then IIF will return the value which follows Then.
- If the results of these logical expressions evaluate to be false, then IIF will return the Else value. As the values that are being returned are Boolean there can be cases where the results are unknown that means they are neither true nor false. This is usually the case when there are null values present in the data. When this happens then the last argument which is passed to IIF is returned as Unknown result. If this is not considered, then the Null value is returned.
IIF(test, then, else [unknown])
Let us now have a look at a few examples in order to understand these functions properly.
- Consider that A grade has been earned, a certificate needs to be issued. A field should be there to calculate and identify these instances. This can be done easily by either using If Else statement of using the IIF statement.
IF (Letter Grade (Nulls))= ‘A’ then ‘Create Certificate’
Else ‘Do Nothing’
IIF(Letter Grade (Nulls))= ‘A’, ‘Create Certificate’, ‘Do Nothing’)
The only difference between these statements is the syntax. The IIF statement is less prone to errors but is not much readable or familiar. The basic difference between these two is that data contains values which yield unknown result through Boolean comparison. This may happen when there is a null value in data. When this happens in If Else statement the unknown results are treated as the same as false results. But when the IIF statement is used then it returns ‘Null’ value for results which will have unknown values.
- IF test THEN value END/ IF test THEN value ELSE else END
The above function is similar to the IIF function. It distinguishes True, False and Unknown values. On the other hand, the IF statement only cares about the True and not True values. To get the appropriate values in return all logical tests must be performed by using IF THEN ELSE statements.
4.5 (2,927 ratings)
IF(SUM([Profit]) > 0) THEN 'Performing Good'
ELSE 'Bad Performance'
This statement clearly defines when the business is performing good and when it is performing badly. There is no ambiguity present in this scenario.
- In order to have multiple conditions checked the If THEN ELSE IF can be used.
IF (Letter Grade (Nulls))= ‘A’ then 4
ElseIF (Letter Grade (Nulls))= ‘B’ then 3
ElseIF (Letter Grade (Nulls))= ‘C’ then 2
ElseIF (Letter Grade (Nulls))= ‘D’ then 1
These statements are easier and less ambiguous when it comes to the IIF statement. They have their separate conditions defined. It is much easier to write and understand. When it comes to If statements in Tableau, IIF should be used when the calculation is simple, or you are not sure about the data quality of the field under question.
Conclusion for Tableau IF Statement
- Tableau, unlike other tools, does provide logical functions. These functions can be easily used as requirements. The If conditions with its variants can be easily implemented in Tableau. It also has IIF and ELSEIF which provide a mechanism for unknown values being replaced with NULL. It provides normal conditional checks as well.
- The other logical functions are also useful, and Tableau is hence being used in the market with these features in hand. It also has the functionality to create logical calculations where all these logical functions can be used, and the reports can provide more accurate data and give an accurate analysis to the users.
This has been a guide to Tableau IF Statement. Here we discuss the Introduction, three different variants of the IF function with their respective syntax and explanation. You can also go through our other suggested articles to learn more –