The Boolean data type is the most efficient for Tableau to process. Yet it is very under-used.

It seems many developers do not make proper use of the Boolean data type in Tableau. Perhaps it is misunderstood.

This article will tell you what you need to know about the Boolean in a Tableau context.

On various Tableau forums I see developers, from Junior to Zen Master, using a long-winded formula when a simple Boolean would do the same job faster.

This post is to encourage developers to use more Boolean!

Although this is written for Tableau, the exact same also applies to Excel, should any Excel developers read this article.

words true and false in neon lights

Boolean is either True or False

 

What is the Boolean data type?

In Tableau, Boolean is either True or False. It can only be one of those values. In some software a tri-state data type – True, False, Null – can sometimes be mistakenly referred to as boolean, for example a SQL bit data type.

In terms of performance, Boolean is as good as it gets.

True or False is also represented as 1 or 0, where 1 is True and 0 is False. (Note in some software True is also as -1.)

This data type is the quickest for computers to process. Using Boolean values in calculated fields gives faster Tableau performance compared to numeric or string calculations.

Examples of using Boolean in a Tableau calculation

Perhaps it’s the terminology of True or False that deters people from using it. But another, and perhaps less intimidating way to view it is as a simple Yes or No.

For example, maybe you only want to see profitable stores. Framing this as a question in plain English helps define the Boolean calculation:

“Is profit greater than zero?” – “Yes” or “No”

In Tableau that becomes the boolean calculation:

SUM([Profit])>0

The result being True (or Yes) if it is profitable.

However, what I often see is that written:

IF SUM([Profit])>0 THEN "Profit" ELSE "Loss" END

Which is actually significantly longer and more complex to write…but seems easier for most developers to understand that over a simple Boolean.

Boolean example 2

Another example, create a boolean field called [isProfitable]: (Note you don’t set the data type, Tableau does this automatically.)

[isProfitable]: SUM([Profit])>0

Some would then use this as follows:

IF [isProfitable]=True THEN "Profit" ELSE "Loss" END

The “=True” is not needed. Remove it and the result is the same.

Think about the IF statement:

IF something is true THEN do something ELSE do something else.

The first part of the IF statement is only deriving if something is True or False. Therefore, the =True is unnecessary, it’s implicit in the IF statement anyway.

Boolean example 3

Another example I saw recently:

IF [Field] = "A" THEN True ELSE False END

Again, overly complicated. The simple equivalent is:

[Field] = "A"

It either does equal A, so returns True, or it doesn’t equal A, so returns False.

Boolean example 4

Similarly, I saw this:

IF [task] = 'Finish' THEN 1 ELSE 0 END

Far simpler to write:

INT([task] = 'Finish')

Boolean example 5

Another example (copied directly from a forum):

If CONTAINS([Field],"2-Wheel Drive, Front") = true then 1 end

Which is the same as:

INT(CONTAINS([Field],"2-Wheel Drive, Front"))

(Ok, it’s not quite the same; the initial formula returns NULL if it doesn’t contain “2-Wheel Drive, Front”, while the boolean version returns 0.)

Converting Boolean to a number

In some of the previous examples, notice I wrapped the Boolean output in the INT() function.

The output of a Boolean is True or False. The INT function converts True to 1 and False to 0.

This is very useful. For example, use this to solve the common error message “Cannot use Boolean type in IF expression

Use Boolean for Year on Year, Month on Month, Period vs Period calculations

Another good example is to calculate this year and last year numbers.

Consider a data set such as (deliberately small for the example):

Date Sales Profit
01/01/2018 363 7
01/04/2018 979 6
01/07/2018 364 9
01/10/2018 188 5
01/01/2019 719 6
01/04/2019 416 1
01/07/2019 767 2
01/10/2019 376 2
01/01/2020 142 8
01/04/2020 758 2
01/07/2020 146 8
01/10/2020 667 3

Assume the current year is 2020 and we want to compare this year with last year. That means identifying this year and identifying last year (2019).

We can use Boolean fields for this by creating simple flags, to use in other calculations.

For example the current year (isCY = is Current Year):

[isCY]: YEAR([Date]) = YEAR(Today())

For the prior year:

[isPY]: YEAR([Date]) = YEAR(Today())-1

Which returns:

Date Sales Profit isCY isPY
01/01/2018 363 7 FALSE FALSE
01/04/2018 979 6 FALSE FALSE
01/07/2018 364 9 FALSE FALSE
01/10/2018 188 5 FALSE FALSE
01/01/2019 719 6 FALSE TRUE
01/04/2019 416 1 FALSE TRUE
01/07/2019 767 2 FALSE TRUE
01/10/2019 376 2 FALSE TRUE
01/01/2020 142 8 TRUE FALSE
01/04/2020 758 2 TRUE FALSE
01/07/2020 146 8 TRUE FALSE
01/10/2020 667 3 TRUE FALSE

These flags are the simple to use to calculate sales for this year:

[SalesCY]: [Sales]*INT([isCY])

Or profit for the prior year:

[ProfitPY]: [Profit]*INT([isPY])

How does this work?

Simple – it multiplies all of the values by either 1 or 0. Therefore they either remain as they are ([Sales]*1 = [Sales]) or they are disregarded ([Sales]*0 = 0).

Therefore the formula for aggregated Year on Year is:

SUM([SalesCY])/SUM([SalesPY]) - 1

No need for table calculations or filters, this is all handled within the Boolean calculations.

Use more boolean in your Tableau calculations!

If you want faster and more efficient Tableau workbooks, the advice is simple:

Use more Boolean and improve your Tableau dashboard performance!