Calculations
Calculations
Applying calculations to your visualization helps further customize, filter, and aggregate data. Calculations can be something as simple as applying an AVG
(average) or any other aggregate function.
Regardless of what type of calculation you apply to your visualization, we consider the result a calculated field.
When a new calculation is created, a new pill will be added to your data pane under the name you provided the calculation. We will cover basic calculations and table calculations below.
Basic Calculations
Basic calculations in Tableau revolve around using arithmetic operators and functions to manipulate and transform your data as you see fit. Calculations can be applied to an individual field or multiple fields, allowing you to create new extracted fields based on your analysis requirements.
Take a look at the example below:
AVG([Transaction Total])
This would provide you a calculated field pill that would store the AVG
of the Transaction Total
field.
Calculation Components
When you are creating a new calculation there are multiple components to familiarize yourself with:
- Fields: You are able to include existing fields from your data inside of a calculation, like in the example above referencing the
Transaction Total
field. - Operators: (
>
,<
,=
,>=
,<=
etc..) - Literal expressions: Specific values (
23
,"String"
,true
,false
, null,#Date
) - Functions: Tableau provides a large number of functions that you can apply within your calculated fields. The functions available include aggregate functions, number functions, logical functions, etc..
Tableau documentation provides an exhaustive list of the available functions you can make use of. You can find a list of all functions here .
While creating a new calculated field, you can select or include a function from a list.
- Click on
Analysis
. - Then click on
Create Calculated Field...
. - Click on the arrow to expand the list of available functions.
Table Calculations
Table calculations in Tableau are a powerful tool for performing calculations on data within a visualization, based on the context of the table or chart. They allow you to create dynamic and informative visualizations which provide insights into your data that would be difficult or impossible to achieve with simple calculations.
Here are a few key concepts to be familiar with in creating table calculations:
- Addressing: Determines the direction in which the calculation is performed. For example, across rows, down columns, or across a specific dimension.
- Partitioning: Defines the groups or subsets of data within which the calculation is applied. This allows you to perform calculations on specific segments of your data.
- Compute Using: Specifies the field or fields that determine the calculation’s scope and direction.
There are several common table calculation functions immediately available within Tableau:
- Running Totals: Accumulate values over time or across categories.
- Differences: Calculate the difference between values in adjacent rows or columns.
- Percentages: Calculate percentages of totals or grand totals.
- Moving Averages: Calculate averages over a specific window of data.
- Rank: Rank values within a group or partition.
- Custom Calculations: Create your own calculations using Tableau’s built-in functions and operators.
To add a table calculation to a visualization within Tableau:
- Build your basic visualization using your desired dimension(s) and measure(s).
- Right-click on the measure you want to calculate and select “Quick Table Calculation” or “Edit Table Calculation.”
- Select the desired calculation type from the options provided.
- Adjust the addressing, partitioning, and compute using settings to customize the calculation.
Filling in the Gaps: PluralSight Content (see warning notice below)
At this moment, please pause your reading of the textbook and complete the two PluralSight learning modules linked below. These modules will help you further understand how to use calculations and quick table functions in Tableau. Once you are finished with these modules, please resume reading this chapter.
PluralSight has gifted LaunchCode licenses for our students to use.
You do not have to pay for your own PluralSight account.
A PluralSight invitation should have been sent to the email you used to register for LaunchCode.
The subject of the email should include LaunchCode has invited you to join Pluralsight!
If you cannot find the email or are having trouble registering using the invite email, please reach out to Course Staff
Partitioning Fields
When working with table calculations, you can optionally specify one or more partitioning fields. Partitioning fields are used to define subsets or partitions of your data, and the table calculation will be performed independently for each partition.
If you have a sales data set with fields like Region, Product, and Sales, you could partition your table calculation by Region to calculate running totals or percentages of totals for each region independently.
This allows you to analyze and compare values within specific partitions of your data.
Addressing Fields
In addition to partitioning fields, table calculations also require you to specify one or more addressing fields. Addressing fields determine the order or direction in which the calculation is performed within each partition.
If you have a time-based data set with a Date
field, you could use Date
as the addressing field to calculate a running total or moving average over time. The addressing field dictates the order in which the calculation is applied to the values within each partition.
By combining partitioning fields and addressing fields, you can create highly customized and insightful table calculations that provide valuable context and enable deeper analysis of your data.
Conditional Statements
You can utilize a few types of conditional statements within calculated fields. The three types of conditional statements we will cover are IF
, IIF
, and CASE
statements.
Each conditional statement has it’s own use cases. When comparing the IIF
to the IF
statement, the IIF
statement allows you to specify how unknown values are treated. Otherwise, these two conditional statements are quite similar.
The CASE
statement is especially useful if you would like to rename values within a given field. Similar to IF
statements, CASE
statements are often used to format and filter data. Let’s look at a few examples below.
IF
statement syntax:
IF some condition
THEN do this
## ELSE statement is optional
ELSE do this
END
IIF
statement syntax
IIF (some condition, do this if true, else this if false, handle unknown)
You have a field named Operating Systems
that contains the three major types of operating systems: Linux
, Windows
, and MacOS
. But, they were represented by numbers within the field instead of the string values. You could create a calculated field like the one below to provide more descriptive value names:
CASE[Operating Systems]
WHEN 1 THEN "Linux"
WHEN 2 THEN "Windows"
WHEN 3 THEN "MacOs"
END
Check Your Understanding
Which of the following is NOT one of the four basic components of a Tableau calculation?
- Functions
- Level of Detail
- Fields
- Operations