1. Logical Functions Part 1

 
Subtitles Enabled

Formulas in Tableau Part 2

10 lessons , 2 exercises , 1 exam

Start Course
100%

Overview

Logical functions are incredibly useful for building custom categorizations in Tableau. In this lesson, we learn how to use IF/ELSEIF functionality to allocate each customer a Small / Medium / Large designation.

Lesson Notes

IF/ELSE functions

- Unlike Excel, Tableau has the ability to easily create multiple branches to an IF function
- The functionality works as follows:

--- IF is used to create the first condition
--- THEN is used to return the output for each branch
--- ELSEIF is used on each subsequent branch after IF
--- ELSE is capture all remaining cases outside the stated conditions
--- END finishes the function

Transcript

If you have used Excel before you should be familiar with the logical functions such as IF, ELSE, AND and OR These functions test certain conditions and then return a value based on the results of that test.

Let's start with a simple example.

I want to create a variable that categorizes my products by size. If the sum of sales is less than 20,000 then the product will be considered small.

If sales are less than 40,000 the product will be considered medium-sized.

Otherwise, the product will be considered large And to do this, we are going to use IF and ELSE IF Let's right-click and create a calculated field called product size And I am going to start by writing IF And when I write IF, I need to then include a condition or an expression.

And I am going to check if the sum of sales is less than 20,000.

And if it is, then I am going to return small.

ELSE IF the sum of sales is less than 40,000 I'll return medium.

Otherwise, I'll return large.

And then I'll write End, to finish my statement.

As you can see, this function is pretty easy to create.

We write IF and then include our condition and then write then and include our answer.

If we have numerous conditions to check we use ELSE IF, and then our final answer will simply be after ELSE.

If you struggle to create these particular statements you can always look at the example on the right-hand side to help you create these calculations.

Let's now apply this particular calculated field to our chart.

And I'll do it by dragging value on to color And as you can see, we have eight small products three medium products, and then seven large products.

Let's now complicate this a bit further and separate Byresta from the other large products and call it very large.

So, I'll jump back into my calculated field and adjust my formula.

And what I am going to say is, ELSE IF, the product name is equal to Byresta, then very large.

And unfortunately, I get an error And the reason I get an error is because when I include product name without a function before it I have non-aggregate data And so, to fix this, I need to put in attribute.

And the attribute function allows me to convert the product name column into a value.

And now this calculation is valid, I'll press OK and now Byresta has a different color to the other remaining large products.

When using hard coded number in IF ELSE statements you can run into trouble when you apply filters to your visualizations.

Let me give you an example.

Say I apply a date filter.

And I'll show this date filter and reduce the date range considerably.

In this reduced date range, a lot more products have have less than 20,000 in sales.

So, my existing designation of small, medium and large changes. This is not ideal.

What we would like is for a certain condition to stay constant regardless of whether a filter is applied.

To fix this problem, I'll show you a simple solution in the next lesson.