You are on page 1of 52

Tableau

Calculations
What is Tableau Calculations
• Table calculations in Tableau are basically transformations you apply
to the values in a visualisation.
Fundamentals of Table Calculations

PARTITIONING FIELDS THE DIMENSIONS THAT DEFINE THE ADDRESSING FIELDS THE REMAINING DIMENSIONS,
SCOPE OF DATA IT IS PERFORMED UPON WHICH THE TABLE
ON ARE CALLED PARTITIONING CALCULATIONS IS PERFORMED, ARE
FIELDS. THE TABLE CALCULATIONS CALLED ADDRESSING FIELDS AND
IS PERFORMED SEPARATELY WITHIN DETERMINE THE DIRECTION OF THE
EACH PARTITION. CALCULATION.
3. Table(aCross)

• The calculation is computed


across rowYEAR(Order Date)
for every row MONTH(Order
Date)
3. Table(down)

• The calculation is computed


Columns MONTH(Order
Date) for every column
year(Order Date)
3. Table(across then
down)
• The calculation is computed across columns
YEAR(Order Date), down a row
MONTH(Order Date), and then across
columns again for the entire table.
3. Pane(down)
3. Pane(across
then down)
• The calculation is computed across
columns YEAR(ORDER Date) for
the length of the pane, down a
row MONTH(Order Date), and
then across columns for the length
of the pane again.
3. Pane(down then across)
3. Cell

• The calculation is computed


within a single cell
3. Specific Dimensions
• In this table, the
dimensions, Month of
Order Date and Quarter
of Order Date, are the
addressing fields
selected, are Year of
Order Date is the
partitioning field since it
is not selected.
Building a
Table
Calculation
in Tableau
Tableau - Operators
• An operator is a symbol that tells the compiler to perform
specific mathematical or logical manipulations. Tableau has a
number of operators used to create calculated fields and
formulas.
Following are the details
of the operators that are
available and the order
(precedence) of
operations.
Tableau -
Operators
Types of Operator
• General Operators
• Arithmetic Operators
• Relational Operators
• Logical Operators
Operator Description Example
General 7+3
Operators Profit + Sales
Adds two numbers. 'abc' + 'def' =
Concatenates two 'abcdef'
+(addition)
strings. Adds days #April 15, 2004# +
to dates. 15 = #April 30,
• Following table shows
the general operators 2004#
supported by Tableau.
These operators act on
numeric, character, and -(7+3) = -10
date data types.
Subtracts two #April 16, 2004# -
–(subtraction) numbers. Subtracts 15 = #April 1,
days from dates. 2004#
Operator Description Example

Arithmetic *(Multiplication) Numeric 23*2 = 46


multiplication
Operators
/(Division) Numeric 45/2 = 22.5
division
• Following table shows
the arithmetic operators
supported by Tableau.
%(modulo) Reminder of 13 % 2 = 1
These operators act only numeric
on numeric data types. division

^(power) Raised to the 2^3 = 8


power
Operator Description Example

[Ship Date] > #April 1, 2012#


Logical Operators If the expressions or Boolean
values present on both sides of
AND [Profit] > 10000

AND AND operator is evaluated to


be TRUE, then the result is
TRUE. Else the result is FALSE.
• Following table shows the
logical operators supported by
Tableau. These operators are [Ship Date] > #April 1, 2012#
used in expressions whose If any one or both of the OR [Profit] > 10000
result is a Boolean giving the expressions or Boolean values
output as TRUE or FALSE. present on both sides of AND
OR
operator is evaluated to be
TRUE, then the result is TRUE.
Else the result is FALSE.

This operator negates the NOT [Ship Date] > #April 1,


NOT Boolean value of the 2012#
expression present after it.
Precedence Operator

Operator 1 –(negate)
Precedence
2 ^(power)
• The following table describes 3 *, /, %
the order in which operators
are evaluated. The top row has
the highest precedence. 4 +, –
Operators on the same row
have the same precedence. If 5 ==, >, <, >=, <=, !=
two operators have the same
precedence, they are
evaluated from left to right in 6 NOT
the formula. Also parentheses
can be used. The inner
parentheses are evaluated 7 AND
before the outer parentheses.
8 OR
Tableau - Functions
• Any data analysis involves a lot of calculations. In Tableau, the
calculation editor is used to apply calculations to the fields
being analyzed. Tableau has a number of inbuilt functions which
help in creating expressions for complex calculations.
• Number Functions
• String Functions
• Date Functions
• Logical Functions
• Aggregate Functions
Function Description Example
Number CEILING (number) Rounds a number CEILING(2.145) = 3
Functions to the nearest
integer of equal or
greater value.

POWER (number, Raises the number POWER(5,3) = 125


• These are the power) to the specified
functions used for power.
numeric calculations.
They only take ROUND (number, Rounds the ROUND(3.14152,2) =
numbers as inputs. [decimals]) numbers to a 3.14
Following are some specified number of
examples of important digits.
number functions.
Function Description Example

Returns the length of


LEN (string) LEN("Tableau") = 7
String the string.

Functions LTRIM (string)


Returns the string
with any leading LTRIM(" Tableau ") = "Tableau"
spaces removed.

Searches the string for


• String Functions are
substring and replaces
used for string REPLACE (string, it with a replacement. REPLACE("GreenBlueGreen",
manipulation. Following substring, "Blue", "Red") =
replacement) If the substring is not "GreenRedGreen"
are some important found, the string is not
string functions with changed.
examples

Returns string, with all UPPER("Tableau") =


UPPER (string) characters uppercase. "TABLEAU"
Function Description Example

Date DATEADD
(date_part,
Returns an increment added to
the date. The type of increment
DATEADD ('month', 3,
#2004-04-15#) =
2004-07-15 12:00:00
Functions increment, date) is specified in date_part. AM

DATENAME Returns date_part of date as a DATENAME('month',


• Tableau has a variety of date string.
functions to carry out (date_part, date, #200404-15#) =
[start_of_week]) The start_of_week parameter is "April"
calculations involving dates. All optional.
the date functions use
the date_part which is a string
indicating the part of the date Returns the day of the given date DAY(#2004-04-12#) =
DAY (date) as an integer. 12
such as - month, day, or year.
Following table lists some
examples of important date Returns the current date and NOW( ) = 2004-04-15
functions. NOW( ) time. 1:08:21 PM
Logical Functions
FUNCTION DESCRIPTION EXAMPLE

• These functions evaluate The IFNULL function returns the


IFNULL (expression1, first expression if the result is not
some single value or the expression2) null, and returns the second IFNULL([Sales], 0) = [Sales]

result of an expression and expression if it is null.

produce a boolean output.


ISDATE("11/05/98") = TRUE
The ISDATE function returns TRUE ISDATE("14/05/98") = FALSE
if the string argument can be
ISDATE (string)
converted to a date, and FALSE if
it cannot.

The MIN function returns the


minimum of an expression across
MIN(expression) all records or the minimum of two
expressions for each record.
Function Description Example
Returns the average of all
the values in the
Aggregate AVG(expression)
expression. AVG can be
used with numeric fields
Functions only. Null values are
ignored.

COUNT (expression) Returns the number of


items in a group. Null
values are not counted.
• These functions MEDIAN (expression) Returns the median of an
evaluate some single expression across all
value or the result of an records. Median can only
expression and produce be used with numeric
a boolean output. fields. Null values are
ignored.
STDEV (expression) Returns the statistical
standard deviation of all
values in the given
expression based on a
sample of the population.
Tableau - Numeric Calculations
• Numeric calculations in Tableau are done using a wide range of
inbuilt functions available in the formula editor.
• In this we will see how to apply calculations to the fields. The
calculations can be as simple as subtracting the values of two
fields or applying an aggregate function to a single field.

• Following are the steps to create a calculation field and use


numeric functions in it.
Create Calculated
Field

• While connected to Sample-


superstore, go to the Analysis menu
and click ‘Create Calculated Field’,
as shown in the following
screenshot.
Calculation Editor

• The above step opens a


calculation editor which
lists all the functions that is
available in Tableau. You
can change the dropdown
value and see only the
functions related to
numbers.
Create a
Formula
• To study the difference
between profit and
discount for different
shipping mode of the
products, create a formula
subtracting the discount
from the profit as shown in
the following screenshot.
Also, name this field
as profit_n_discount.
Using the
Calculated
Field
• The above calculated field
can be used in the view by
dragging it to the Rows
shelf as shown in the
following screenshot. It
produces a bar chart
showing the difference
between profit and
discount for different
shipping modes.
Applying
Aggregate
Calculations
• In a similar manner as
above, you can create a
calculated field using
aggregate function. Here,
create AVG(sales) values
for different ship mode.
Write the formula in the
calculation editor as shown
in the following screenshot.
Applying
Aggregate
Calculations
• On clicking OK and dragging
the Avg_Sales field to the
Rows shelf, you will get the
following view.
• In this you will learn about calculations

Tableau - in Tableau involving Strings. Tableau


has many inbuilt string functions, which
can be used to do string manipulations
String such as - comparing, concatenating,
replacing few characters from a string,

Calculations etc. Following are the steps to create a


calculation field and use string functions
in it.
Create
Calculated
Field
• While connected to
Sample superstore, go to
the Analysis menu and
click ‘Create Calculated
Field’ as shown in the
following screenshot.
Calculation
Editor
• The above step opens a
calculation editor which
lists all the functions that is
available in Tableau. You
can change the dropdown
value and see only the
functions related to strings
Create a
Formula
• Consider you want to find
out the sales in the cities,
which contain the letter “o”.
For this, create the formula
as shown in the following
screenshot.
Using the
Calculated
Field
• Now, to see the created
field in action, you can
drag it to the Rows shelf
and drag the Sales field to
the Columns shelf. The
following screenshot
shows the Sales values.
Using the
Calculated
Field
• Now, to see the created
field in action, you can
drag it to the Rows shelf
and drag the Sales field to
the Columns shelf. The
following screenshot
shows the Sales values.
Tableau - Date Calculations
• Dates are one of the key fields which is extensively used in
most of the data analysis scenarios. Hence, Tableau provides a
large number of inbuilt functions involving dates. You can carry
out simple date manipulations such as adding or subtracting
days from a date. You can also create complex expressions
involving dates.
• Following are the steps to create a calculation field and use
date functions in it.
Create Calculated
Field

•While connected to Sample


superstore, go to the Analysis
menu and click ‘Create
Calculated Field’, as shown in
the following screenshot.
Calculation
Editor
•The above step opens a
calculation editor, which lists all
the functions available in
Tableau. You can change the
dropdown value and see only
the functions related to Date.
Create a
Formula
• Now, find out the sales
volume along with the
difference in the date of
sales in months from
21st March 2009. For this,
create the formula as
shown in the following
screenshot.
Using the
Calculated Field

• Now to see the created


field in action, you can
drag it to the Rows shelf
and drag the Sales field to
the Columns shelf. Also
drag the ship Date with
months. The following
screenshot shows the
Sales values.
Tableau - Table Calculations
• These are the calculations which are applied to the values in
the entire table. For example, for calculating a running total or
running average, we need to apply a single method of
calculation to an entire column. Such calculations cannot be
performed on some selected rows.
• Tableau has a feature called Quick Table Calculation, which is
used to create such calculations. The steps to be applied in
Quick Table calculation are as follows
Tableau - Table Calculations
• Step 1 − Select the measure on which the table calculation has
to be applied and drag it to column shelf.
• Step 2 − Right-click the measure and choose the option Quick
Table Calculation.
• Step 3 − Choose one of the following options to be applied on
the measure.
Tableau - Table Calculations
• Running Total
• Difference
• Percent Difference
• Percent of Total
• Rank
• Percentile
• Moving Average
• Year to Date (YTD) Total
• Compound Growth Rate
• Year over Year Growth
• Year to Date (YTD) Growth
Tableau - LOD Expressions
• Level of Detail (LOD) expressions are used to run complex
queries involving many dimensions at the data source level
instead of bringing all the data to Tableau interface. A simple
example is adding dimension to an already calculated
aggregate value.
Tableau - LOD Expressions
• Types of LOD
• There are three main types of LOD expressions.
• FIXED LOD This expression computes values using the specified
dimensions without reference to any other dimensions in the
view.
• INCLUDE LOD This level of detail expressions compute values
using the specified dimensions in addition to whatever
dimensions are in the view.
• EXCLUDE LOD These levels of detail expressions subtract
dimensions from the view level of detail.
FIXED Level of
Detail
Expressions
• Find the amount of Sales
for each state in each
region. Here, first create
the formula field named
Regional Sales using the
formula as shown in the
following screenshot.
FIXED Level of
Detail Expressions

• Next, drag the Region and State


field to the Rows shelf and the
calculated field to the Text shelf
under the Marks card. Also drag the
Region field to the Color shelf. This
produces the following view, which
shows a fixed value for different
states. That is because we have
fixed the dimension as region for the
calculation of Sales value.
INCLUDE Level
of Detail
Expressions
• INCLUDE level of detail
expressions compute values
using the specified dimensions in
addition to whatever dimensions
are in the view. Calculate the
sum of sales per state for each
sub-category of products.
• For this, drag the Sub-Category
field to the Rows shelf. Then,
write the expression in the
Columns shelf as shown in the
following screenshot. It produces
the following view which includes
both the dimensions in the
calculations.
EXCLUDE
Level of Detail
Expressions
• EXCLUDE level of detail
expressions specify
dimensions to exclude from the
view level of detail. Exclude
Region from Sales figure
calculated for every month.
Create the formula as shown in
the following screenshot.
• On dragging the relevant fields
to the respective shelves, you
will get the final view for the
EXCLUDE LOD as shown in
the following screenshot.

You might also like