What is IF and ELSE Statement in Tableau

The IF and ELSE statement in Tableau are used to create conditional logic within calculated fields or table calculations. These statements allow you to check for specific conditions and return different results based on whether those conditions are TRUE or FALSE.

You can use IF statement in three different ways:

  • IF statement in Tableau
  • IF and ELSE statement in Tableau
  • IF, ELSEIF and ELSE statement in Tableau

Let go one by one and see the usage of all three IF statement:

1. IF Statement in Tableau:

IF statement is used to evaluate a given condition and return a value if the condition is TRUE. If the condition is FALSE, you can specify an alternative condition using ELSE or either leave the result as NULL.

Syntax:

IF <condition> THEN <result> END    
<condition>: The expression you want to evaluate (e.g., Sales > 1000).   
<result>: The value to return if the condition is true.

 

Example:

IF [Sales] > 1000 THEN 'High Sales' END

This will return ‘High Sales‘ for rows where the sales value is greater than 1000.

If the condition is false (i.e., sales ≤ 1000), it will return NULL.

2. IF and ELSE Statement in Tableau:

IF and ELSE extends the logic of simple IF statement with allowing you to define an alternative value or result if the condition is FALSE. If the condition is TRUE, then THEN part is executed, and if FALSE, then ELSE part is executed.

Syntax:

IF <condition> THEN <result1> ELSE <result2> END     
<condition>: The condition you want to test.   
<result1>: The result if the condition is true.   
<result2>: The result if the condition is false.

Example:

IF [Sales] > 1000 THEN 'High Sales' 
ELSE 'Low Sales' END

This will return ‘High Sales’ if the sales are greater than 1000, and ‘Low Sales’ if the sales are 1000 or less as per the ELSE condition

3. IF, ELSEIF and ELSE Statement in Tableau:

You can also use ELSEIF to check multiple conditions in sequence in IF statement in tableau.

Tableau will check each condition in order and execute the corresponding result for the first condition that evaluates to TRUE.

Syntax:

IF <condition1> THEN <result1>
ELSEIF <condition2> THEN <result2>
ELSE <result3> END

Example:

IF [Sales] > 1000 THEN 'High Sales' 
ELSEIF [Sales] > 500 THEN 'Medium Sales'
ELSE 'Low Sales' END

Below is the sequence where above IF, ELSEIF and ELSE statement is working:

    If sales are greater than 1000, it returns ‘High Sales‘.

    If sales are between 500 and 1000, it returns ‘Medium Sales‘.

    If sales are 500 or less, it returns ‘Low Sales‘.

Summary:

    IF Statement: Tests a single condition, returns a result if true.

    IF and ELSE Statement: Tests a condition, returns one result if true and another if false.

    IF, ELSEIF and ELSE Statement: Tests multiple conditions in sequence and returns a result for the first true condition, with an optional final else case.

Scroll to Top