IF statements

IF is the main logical function in Tableau and a complete IF function is often called an IF statement. Within any IF statement there are usually at least 3 other functions: THEN, ELSE, and END. These functions split up the IF statement in different parts and signify where the arguments for one part of the statement end and begin. The IF statements are structured in a following way: first, you need to define a condition to be met; second, you need to specify what you want to happen if the condition is met; and finally you must specify what should happen if the condition is not met. Here the IF part is in green, the THEN part is in blue, and the ELSE part is in red.

Let’s work though an example. We want to find out which US states have sum of sales higher than 50,000 and then label it as “High Sales” or “Low Sales” accordingly. The IF statement will look like this:

This IF statement first poses a condition to be met or a question to be answered: are sum of sales higher then 50000? If the answer is yes then show “High Sales”, else, if the answer is no, then show “Low Sales”. If we then drag our new calculated field named “IF STATEMENT” onto colour mark, the states that matched our condition are highlighted.


You can also combine an IF statement with ELSEIF function, which operates as an IF statement in itself. You can use it to specify more conditions. For example, if we want to have one more category for sum of sales that are between 10,000 and 50,000 we can specify it using ELSEIF statement. Notice that here I am also including an AND function which requires that both conditions are met (the sales are higher than 10,000 but lower than 50,000) for the associated THEN statement to be executed.



The resulting map will look like this:

There is one key difference between ELSE and ELSEIF statements. With ELSEIF statement you will specify the desired outcome if the condition is met. For that you will use THEN function. That is, the outcome specified after THEN function is only going to be applied to the data that met the initial condition of the ELSEIF statement (i.e., if sum of sales is larger than 10,000 but smaller than 50,000). On the other hand, ELSE statement will simply group together all the remaining data that did not meet the initial condition.


END

Author:
Skomantas Tamulaitis
Powered by The Information Lab
1st Floor, 25 Watling Street, London, EC4M 9BR
Subscribe
to our Newsletter
Get the lastest news about The Data School and application tips
Subscribe now
© 2024 The Information Lab