What is IF Statement in Tableau ?
IF Statement in tableau is the logical function, that is used to decide which condition should give the result. Its works with TRUE and FALSE condition. Based on the IF statement syntax, if condition is true then it will give the result of TRUE value else FALSE values. The condition works on mathematical condition like, greater, equal to, less then.
IF statement in Tableau can be performed both String and Numeric condition and also give result as String or number.
Tableau IF statements also used for Operators or Logical comparisons (AND, OR, NOT). Also read one-line IF function called IIF Function.
Syntax for IF statement:
IF <condition 1> THEN <True result 1>
ELSEIF <condition 2> THEN <True result 2> //Comment: check condition to if first condition is False
ELSE <result 3> //Comment: Result 3 if all conditions are false
END
Let’s understand the various components of IF else statement in Tableau:
- IF is the keyword, which tells Tableau that IF condition should performed
- <condition 1> represents the conditional expression which is used to perform the Boolean function to check and evaluation for result either TRUE or FALSE.
- THEN is the keyword indicates to return result value
- <True result 1> is the Result/value that will be returned if the IF conditional is TRUE.
- ELSEIF is the another keyword which can be used if you want to put second condition if your first condition not fulfill your requirements
- <condition 2> This will be the second conditional expression which is used to perform the Boolean function for your second criteria/condition to check for result either TRUE or FALSE.
- ELSE is the Keyword which performed if non of the condition is True,
- END represents the end of the loop.
Important: The Condition expression used two types of Operator to compare the values
- Comparison operator
- Logical operator
There are six comparison operator are available within the Tableau:
Comparison Operator | Meaning |
---|---|
= or == | Equal To |
<> or != | Not Equal To |
< | Less Than |
> | Greater Than |
<= | Less Than or Equal To |
>= | Greater Than or Equal To |
There are three Logical operator are available within the Tableau:
Logical Operator | Meaning |
---|---|
AND | The entire condition and statement will be FALSE if any one condition of comparisons on the left and right side of the AND is False. Meaning both condition or all condition should be TRUE for AND logical Operator, then result will show. |
OR | The entire condition and statement will be FALSE, if both or all condition of comparisons on the left and right side of the OR is False. Meaning any one statement should be True for OR logical Operator, then result will show. |
NOT | NOT is used on a single expression and it returns the opposite output of the statement result. Ex: if the statement is False, NOT will make it TRUE and else FALSE. |
5 Different examples for IF statement in Tableau
Lets see the 5 different example and understand more about IF statement:
Example #1: IF Statement in Tableau for numeric value
First example for numeric number like sales, Profit, and margin. Suppose company have categories Sales based on total sales for product then we can use below IF condition in Tableau to show these categories.
IF [Total Sales] <= 1000 THEN "Normal sales" ELSEIF [Total Sales] <= 2000 THEN "Good Sales" ELSE "Expected Sales" END
The above example will show Categories for sales based on the condition used in above IF condition “Normal Sales” for Total sales which is “Less Than or Equal To” 1000, “Good Sales” for Total sales which is “Less Than or Equal To” 2000.
If both not true then it will show as “Expected Sales”. Same logic can also be calculated based on CASE statement in Tableau, which is more faster then IF statement.
Example #2: IF statement in Tableau with string
Lets take example for color fields in dataset which have different colors as members in Color field and you want to categories as Dark and Light color, Then you can use below IF condition which is used as AND operator to performed IF condition.
IF [Color]="Dark Blue" AND [Color]="Dark Green" AND [Color]="Dark Brown" THEN "Dark Color" ELSE "Light Color" END
These is second ways to do the same output with use of Left string Function which will be more simple to perform IF condition as show below formula
IF LEFT([Color],4) ="Dark" THEN "Dark Color" ELSE "Light Color" END
IN above formula what we have did, with the use of LEFT function we just minimize the length of code.
The LEFT formula just comparing if member of Color field have the “Dark” text in the beginning it will put “Dark color” else it will put “Light color”
Example #3: Multiple IF and ELSEIF statements
Suppose you have created one parameter and based on that parameter you want to show different result in Bar Chart
Example:
- Sales on bar chart if you selected Total sales in parameter
- Profit on bar chart if you selected Total Profit in parameter
- Margin% on bar chart if you selected Margin in parameter
Lets take parameter name is “Bar chart selection”
IF [Bar chart selection] = "Total sales" THEN [Sales] ELSEIF [Bar chart selection] = "Total Profit" THEN [Profit] ELSEIF [Bar chart selection] = "Margin" THEN [Profit]/[Sales] END
Example #4: IF statement with Boolean
You know that Boolean takes value as TRUE and FALSE so you can put result as TRUE and FALSE in Tableau IF statements as shown below syntax
IF [Field] = "AAA" THEN True ELSE False END
These is simple Boolean if statement which check the field member and return TRUE and FALSE based on condition is true
[Field] = “AAA”, this expression returns True, or it doesn’t equal A, so returns False.
Example #5: Tableau IF statement between two values
How to use Tableau if statements between two values or range of values. if you want to put the condition on range of values using IF statements then you need to use AND operator in tableau. As show in below syntax or calculated field
IF [PRICE]>= "2500" AND [PRICE] <= "3000" THEN [Profit] END
In the above examples IF statement checks the range of [Price] field between 2500 to 3000, if it is TRUE then it will return the [profit] filed values
Here AND operate check the range and return as condition satisfied.
Also read: Cylindrical Fill Bar Chart In Tableau
Conclusion
Here we are trying to say that you can also perform the calculation in the result output, this is really good if you need to show different numeric , measure values in table and graph.
It will get change as per the selection of your condition in parameters. You can also use IF condition for field also, it depends how you want to use and what is your requirement.