Tableau provides various Logical Functions to perform logical operations on our data. They are AND, NOT, OR, IF, ELSEIF, IF Else, CASE, ISNULL, IFNULL, ZN, IIF etc. In this article, we will show you, How to use Tableau Logical Functions with examples.
To demonstrate these Tableau Logical Functions, we are going to use the data present in Global Super Store Excel Work sheet so, Please refer Connecting to Excel Files in Tableau article to understand the connection settings.
Tableau Logical Functions
The following examples will show you the list of Logical Functions in Tableau
Tableau AND Function
The Tableau AND function is used to check multiple expressions. The syntax of the Tableau AND Function is as shown below:
Expression_1 AND Expression_2
As you can see from the above syntax, Tableau AND function accepts two arguments: If both the conditions are True then it will return True otherwise, it return False.
To demonstrate these logical functions, we have to use Calculated Field. In order to create a calculated field, please navigate to Analysis Tab and select the Create Calculated Field… option as shown below.
Once you click on the Create Calculated Field… option, following window will be opened. Here, we renamed the default calculation name as AND Func.
This will check whether the Profit is greater than 0, and Quantity is greater than 25000. If both these conditions are true then function will return Good otherwise, it will return Bad
IF(SUM([Profit]) > 0 AND SUM(Quantity) > 25000) THEN 'Good' ELSE 'Bad' END
Let me add this AND function calculated field to the table (by dragging field to Rows Shelf). Please refer Create Table Report article to understand the process of creating a table
Tableau OR Function
The Tableau OR function is like either or statement in English. If both the conditions are False then it will return False otherwise, it return True. The syntax of this Tableau OR Function is:
Expression_1 OR Expression_2
Below statement will return Good if either of these two statements are True
IF(SUM([Profit]) > 0 OR SUM(Quantity) > 300000) THEN 'Good' ELSE 'Bad' END
Let me add this field to Rows shelf
Tableau IIF Function
The Tableau IIF function is the simple version of If Else Function. If both the condition is True then it will return First Statement otherwise, second statement. The syntax of this Tableau IIF Function is:
IIF(Expression, True_statement, False_Statement)
Below statement will return Profit if the condition is True otherwise, Loss will be returned.
IIF(SUM([Profit]) > 0, 'Profit', 'Loss')
Now you can see the result
Tableau NOT Function
The Tableau NOT function return the exact opposite. I mean, True will become false and vice versa. The syntax of this Tableau NOT Function is:
Below statement will return Loss if the condition is True otherwise, Profit will be returned.
IF(NOT SUM([Profit]) > 0) THEN 'Profit' ELSE 'Loss' END
You can see the NOT function result by yourself
Tableau ISNULL Function
To demonstrate these functions, we will use below shown table.
Tableau ISNULL function will check whether it is NULL or Not. If it is NULL then it return TRUE otherwise, False will be returned. Syntax of Tableau ISNULL Function is:
Below statement will check nulls in Service Grade 2 column.
ISNULL([Service Grader 2])
As you can see, ISNULL function returned True for the products under Blue Color.
Tableau ZN Function
Tableau ZN function will return the original values of Not Null values, and 0 for Null values. In simple English, it is used to replace the NULL values with 0. Syntax of Tableau ZN Function is:
Below statement will replace NULL with 0.
ZN([Service Grader 2])
From the below screenshot, you can see the result of a ZN function.
Tableau IFNULL Function
Tableau IFNULL function is used to replace the NULL values with your own. Syntax of Tableau IFNULL Function is:
Below statement will replace NULLs with 10.
IFNULL([Service Grader 2], 10)
Let us see the result