You are on page 1of 22

Tableau Buddy TB

Calculations Guide - Part 1


A mini-guide to help you refresh Tableau calculations

Prasann Prem
Tableau Social Ambassador

Founder - Tableau Buddy

Tableau Mentor | Sr. Product Engineer @AmexGBT


Swipe >>
Tableau Buddy TB

In this guide, you will learn about the following types of calculations in Tablea

String Calculation
Date Calculation
Aggregate and Non-Aggregate Calculation
User Function
Table Calculations

Swipe >>
Tableau Buddy TB

String Calculations

𝗖𝗼𝗺𝗺𝗼𝗻𝗹𝘆
𝘂𝘀𝗲𝗱 𝘀𝘁𝗿𝗶𝗻𝗴 𝗳𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝘀 𝗶𝗻 𝗧𝗮𝗯𝗹𝗲𝗮

1. LEFT and RIGHT: These functions enables us to return the characters from a field based on the number of characters
reuired either from left or right respectively.

For example: A field [Country] contains a value equal to "United States of America", then

LEFT([Country],5) = United

RIGHT([Country],7) = America

2. LEN: This function returns the length of the string in the form of integers

LEN("Tableau") = 7

Swipe >>
Tableau Buddy TB

3. UPPER and LOWER: These functions returns the uppercase and lowercase of strings respectively.

UPPER("Prasann") = PRASANN

LOWER("Prasann") = prasann

4. CONTAINS: This function searches for the part of the string as defined in the calculation and returns the value in
the form of true or false.

CONTAINS("United States of America", "United") = TRUE

Here, the contains function searches for the keyword "United" in the string "United States of America" and returns
true if it is found.

Swipe >>
Tableau Buddy TB

5. TRIM: This function returns the string with leading and trailing spaces.

TRIM(" String Functions ") = StringFunctions

6. PROPER: A new addition to the Tableau family and is available with 2022.4 version. This function lets you return the
string in proper case

PROPER(“prasann prem”) = Prasann Prem

Swipe >>
Tableau Buddy TB

Date Calculations

𝗖𝗼𝗺𝗺𝗼𝗻𝗹𝘆
𝘂𝘀𝗲𝗱 𝘀𝘁𝗿𝗶𝗻𝗴 𝗳𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝘀 𝗶𝗻 𝗧𝗮𝗯𝗹𝗲𝗮𝘂

Date functions are the functions that enable us to handle date data types or can help us create date from other other
types.

As Tableau Visionary, Jim Dehner defines it - Date functions are typically divided into four types:

1. 𝗗 Date - The functions in this category are used to mainly create dates.

The functions are DATETRUNC(), DATEPARSE(), DATE and MAKEDATE.

2. 𝗗 Date Reference - The functions in this category are used to get an output in the form of integer or a string while we
reference it with a date data type.

The most-used functions in this category are DATENAME(), DATEPART(), YEAR(), MONTH(), DAY() and WEEK().

Swipe >>
Tableau Buddy TB

3. 𝗗 Date Math𝗲 - As the name suggests, these functions helps us in applying mathematical functions to date
values.

Some of the functions in this category are DATEADD() and DATEDIFF().

4. 𝗦 Special𝗽 - These are special functions typically used to fetch a specific value.

The functions that lies in this category are TODAY() and NOW().

Now, let us take some of these functions and understand them one-by-one

Before that, we will take one date variable [Sample date] and assume its value equal to 21 May 2022𝟭 or
05/21/2022.𝟮𝟮

Swipe >>
Tableau Buddy TB

1. DATETRUNC() - Datetrunc function gives us the output in the form of a date with date value as the first date of that period.

Let us see how it works.

DATETRUNC('quarter', [Sample date]) = 04/01/2022

DATETRUNC('month', [Sample date]) = 05/01/2022

We can see that with reference to the date period mentioned, i.e., quarter or month, we are receiving the first date of the period
as our output.

2. DATENAME() - This function gives the output in the form of a string based on the date period mentioned.

DATENAME('month', [Sample date]) = May

DATENAME('day', [Sample date]) = Tuesday

Swipe >>
Tableau Buddy TB

3. DATEPART() - This function gives the output in the form of an integer based on the date period mentioned.

DATEPART('month', [Sample date]) = 5

DATEPART('day', [Sample date]) = 21

4. DATEADD and DATEDIFF - These functions are used to apply mathematical rules such as addition and substraction
respectively to date values.

Dateadd gives us value in the form of dates whereas datediff will evaluate the difference between two dates and will provide
us output in the form of an integer.

DATEADD('month', 5, [Sample date]) = 10/21/2022

DATEDIFF('month', [Sample date], 10/21/2022) = 5

Swipe >>
Tableau Buddy TB

5. TODAY() and NOW() - TODAY() gives us the value of current or today's date and NOW() provides us date and time of this
particular moment.

TODAY() - 07/10/2022

NOW() - 07/10/2022 09:53:54 AM

Swipe >>
Tableau Buddy TB

Aggregate and Non-Aggregate Calculations

𝗖𝗼𝗺𝗺𝗼𝗻𝗹𝘆
𝘂𝘀𝗲𝗱 𝘀𝘁𝗿𝗶𝗻𝗴 𝗳𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝘀 𝗶𝗻 𝗧𝗮𝗯𝗹𝗲𝗮𝘂

Aggregated level calculations are the calculations where you group the data first and then calculate.

Whereas, non-aggregated or record level calculation takes place at the row level.

For e.g. Let us have a group of students in the class:

Name Height Weight

Aaron 6.2 64

Adrian 5.8 70

Armin 5.5 55

If we have to calculate Height per weight of individual students in Tableau, we will write calculation as [Height]/
[Weight].

Swipe >>
Tableau Buddy TB

This calculation will take place at row level since we are calculating a single data point with a single one.

However, if we would like to calculate the Height per weight of the entire class, then the calculation should be
somewhat different.

Height per weight of entire class = SUM(H1+H2+H3)/SUM(W1+W2+W3)

The above calculation must be used to calculate for the group as the mathematics suggest. Remember Algebra
basics, may be in 6th class

So to imitate such logic, we will write the calculation as

SUM[Height]/SUM[Weight].

You can see the difference. The logic is actually same and it helps when we often come around errors like "can't mix
aggregate and non-aggregate calculations".

Swipe >>
Tableau Buddy TB

User Functions

User functions are those functions which allows us to apply user filters or row level security filters in Tableau.

In general, these functions lets you customize your worksheets based on the user who is using it.

For example: If Prasann is a Manager and is part of a retail store dataset, then user function can let him see only his
data whenever he logins onto the dashboard or Tableau Server/Cloud.

Note: These functions fetches some information from your server, so you will only see it in action once your workbook is
connected to Tableau Server/Cloud.

Swipe >>
Tableau Buddy TB

What are the different commonly used user functions?

1. USERNAME(): Returns the username for the current user.

This is the Tableau Server or Tableau Cloud username when the user is signed in; otherwise it is the local or network
username for the Tableau Desktop user.

2. FULLNAME(): Returns the full name of the current user.

The function fetches the name from the server if connected or will return the local desktop user name.

3. ISMEMBEROF(): Returns true if the person specified falls in a certain group or is a member of a group on server.

This function is specially important for implementing row-level security as we can set different rules/permissions for
different groups or filter the data as per the group.

Swipe >>
Tableau Buddy TB

For example: If we have a group called 'Admins' on Tableau Server, then we can write a boolean calculation as

ISMEMBEROF('Admins')

Once applied as filter, it will return values as True or False - where True will provide output for all the users in the Admins
group on Server.

4. ISFULLNAME(): Returns True if the current user full name matches the full name specified on server.

5. ISUSERNAME(): Similar to the above, this checks and returns true, if the current user name matches the username on
the server.

In my view, FULLNAME(), USERNAME() and ISMEMBEROF() are being frequently used whenever we are implementing
user-defined filters.

Swipe >>
Tableau Buddy TB

Table Calculations

𝗖𝗼𝗺𝗺𝗼𝗻𝗹𝘆
𝘂𝘀𝗲𝗱 𝘀𝘁𝗿𝗶𝗻𝗴 𝗳𝘂𝗻𝗰𝘁𝗶𝗼𝗻𝘀 𝗶𝗻 𝗧𝗮𝗯𝗹𝗲𝗮𝘂

Table Calculations in Tableau are those computations that takes place at the view or worksheet level.

To compute table calculations, we must first understand a concept of addressing and partitioning fields.

While using Table calculations, the fields which lies across the direction in which table calculations occurs are our
addressing fields.

While the fields perpendicular to addressing fields or which break the tabular view in Tableau into different segments is
called Partitioning fields.

Let us take an example of calculating running total in Tableau

If we look at the image on the next slide, the running total is being calculated for Year and Region across Category.

Swipe >>
Tableau Buddy TB

Thus, the scope of the calculation is set to Table across in this case
and thereby - Year of Order Date and Region are our addressing
fields since the values are being calculated in their direction.

Whereas, Category is the field which is perpendicular to it and is


distributing the fields into many segments and therefore, it is a
Partitioning field.

Please Note

Layout affects the table calculation and based on the layout, we


can determine the addressing and partioning fields only.

If the layout changes or we just swap the rows and columns, our
addressing and partioning fields will change too.

Swipe >>
Tableau Buddy TB

Table Calculation 1 : Rank function and its different types

Rank functions allows us to evaluate the rank of a dimension with respect to a measure for a partition.

Syntax: RANK(expression, 'asc'|'desc'), here the default is set to descending.

For e.g. A student has marks such as (75, 88, 88, 90). Applying rank function to this value will return rank as (4, 2, 2, 1)

This shows that normal rank function will assign equivalent ranks to identical values and will skip the number if there is
any repetition.

P.S. While calculating rank functions, null values are not considered.

Now, let us go ahead and learn its different forms or variants.

Swipe >>
Tableau Buddy TB

1. Rank_Dense 𝘀𝗲

This function behaves in the same way as normal rank function but the number sequence is not skipped while
evaluating rank.

For e.g. Applying rank dense to the above example will lead to result as (3, 2, 2, 1)

2. Rank_Modified𝗶𝗲𝗱

This function evaluates the rank in the same way as normal rank function. However, wherever there is repetition - it will
assign the largest number to all the identical values and will skip the normal sequence.

This must be quite confusing but let's understand this with the help of an example.

Let's have Marks =[45, 68, 68, 68, 78, 90, 23], then Rank_Modified([Marks], 'desc') will evaluate the output as

(6, 5, 5, 5, 2, 1, 7)

Swipe >>
Tableau Buddy TB

Here, all the three 68s have been assigned 5 because the ranks 3, 4 and 5 will be assigned to 68 in normal sequence
and 5 being the highest for this number will be assigned to all 68s.

3. Rank_Percentile𝗻𝘁𝗶𝗹𝗲

This function returns the value in the form of a percentile. By percentile we mean - It is a statistical term which shows
the position of value in the whole distribution and ranges from 0 to 1.0.

For e.g. Let's say 1 lakh students appeared for CAT examination and 5 out of them scored 80 out of 100. In this case, 80
being the highest will be equivalent to 100 percentile or 1.0 and accordingly, rest of the values position will be
determined by their percentile value.

Rank_Percentile can be evaluated using the same syntax for rank function and we need to note that - null values are
not counted while calculating rank percentile.

Swipe >>
Tableau Buddy TB

4. Rank_Unique𝗶𝗹𝗲

Rank_Unique function assigns rank uniquely to all members in Tableau. To be specific, we need to mention that
identical values are assigned different ranks.

For e.g. If we apply the rank unique function to the [Marks] measure mentioned above in the rank modified section, we
will get the result as

(6, 3, 4, 5, 2, 1, 7) - The order of numbering all the three 68s might be a question to us but we have an answer - Rank lies
in the table calculation category in Tableau and thus, scope of the calculation will decide the sequence.

Swipe >>
Tableau Buddy TB

I hope you enjoyed reading through the guide!

Part - 2 coming soon!

Thank you!!

Follow Prasann Prem for more content

You might also like