You are on page 1of 29

Calculating

User-defined Fields

Ramesh Kandela
rkandela@gitam.edu
Create a Simple Calculated Field

• Sometimes data source does not contain a field (or column) that you need for your
analysis.
• For example, your data source might contain fields with values for Sales and Profit,
but not for Profit Ratio. If this is the case, we can create a calculated field for Profit
Ratio using data from the Sales and Profit fields.
Step 1: Create the calculated field
• In a worksheet in Tableau, select Analysis > Create Calculated Field.
• In the Calculation Editor that opens, give the calculated field a name.
• In this example, the calculated field is called Profit Ratio.
Step 2: Enter a formula
Step 2: Enter a formula
1.In the Calculation Editor, enter a formula.
SUM([Profit])/SUM([Sales])
• Formulas use a combination of functions, fields, and operators.
• There are four basic components to calculations in Tableau:
• Functions - Statements used to transform the values or members in a field.
• Fields - Dimensions or measures (columns) from your data source.
• Operators - Symbols that denote an operation.
• Literal expressions - Constant values that are represented “as is”, such as "Profitable" and
"Unprofitable".
• When finished, click OK. The new calculated field is added to the Data pane. If the new field
computes quantitative data, it is added to Measures. If it computes qualitative data, it is
added to Dimensions.
Functions in Tableau

• Tableau supports many functions for use in Tableau calculations. 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
• Aggregate Functions
• Logical Functions
• Type Conversion Functions
Number Functions

These built-in functions in Tableau allow to perform computations on the data values in fields. Number
functions can only be used with fields that contain numerical values.
SQRT
This function returns the square root of the given number.
Syntax
SQRT(number)
SQRT(25) = 5
MAX
• MAX(number, number)
• Returns the maximum of the two arguments, which must be of the same type. Returns Null if either
argument is Null. MAX can also be applied to a single field in an aggregate calculation.
Example
• Max(Sales,Profit)
Max(23,10)=23
ROUND
• ROUND(number, [decimals])
• Rounds numbers to a specified number of digits. The decimals argument specifies
how many decimal points of precision to include in the final result. If decimals is
omitted, number is rounded to the nearest integer.
• Example:
• This example rounds every Sales value to an integer:
• ROUND(Sales)
Aggregate Functions
• These built-in functions in Tableau allow to summarize or change the granularity
of data.
AVG: This function returns the mean of all the values in an expression, ignoring
the NULL values. AVG can be used with numeric fields only.
Syntax
AVG(expression)
CORR: This calculation returns the Pearson correlation coefficient of two
expressions.
Syntax
CORR(expr1, expr2)
COUNT
• Returns the number of items in a group. Null values are not counted.
• COUNT(expression)
COUNTD
• Returns the number of distinct items in a group. Null values are not counted. 
• COUNTD(expression)
STDEV
• Returns the statistical standard deviation of all values in the given expression based on a sample of
the population.
• STDEV(expression)
MAX
• Returns the maximum of an expression across all records. If the expression is a string value, this
function returns the last value where last is defined by alphabetical order.
• MAX(expression)
String Functions

String functions are used to manipulate string data (i.e. data made of text).
LEN
Returns the length of the string.
LEN (string)
Example
LEN("Tableau") = 7
Manipulating Text
• At times, we are required to parse or manipulate text variables to get something meaningful out of those
variables; for example, a Full Name field may contain both the first name and last name of a sales
representative, but our reporting standards may require us to show two different columns for the first and last
names. With Tableau's string operators, we can easily manipulate the text to meet our requirements.
• To extract the customer's last name, right-click on Customer from Dimensions and select Create Calculated
Field.
• In the Name box, enter Customer Last Name.
• In the formula, SPLIT([Customer Name], ' ', 2)
The calculation might look something like this: SPLIT(‘Ramesh Kandela' , ' ', 2) = Kandela'.
UPPER
• UPPER(string)
• Returns string, with all characters uppercase.
• Example:
• UPPER(“Ramesh Kandela") = “RAMESH KANDELA"
 

Date Functions

• Tableau has a variety of date functions to carry out calculations involving


dates.
DATEDIFF: This function returns the difference between both the dates
expressed in units of the date part. The start of the week can be adjusted to
the day a user needs to.
Syntax
DATEDIFF(date_part, date1, date2)
• DATEDIFF('day',[Order Date],[Ship Date])
Type Conversion Functions

• Type conversion functions allow to convert fields from one data type to another.
For example, you can convert numbers to strings, such as age values (numbers) to string
values so that Tableau does not try to aggregate them.
• The calculation for such a task might look similar to the following:
STR([Age])

Type conversion functions available in Tableau:


• The result of any expression in a calculation can be converted to a specific data type. The
conversion functions are STR(), DATE(), DATETIME(), INT(), and FLOAT().
• For example, if you want to cast a floating-point number like 3.14 as an integer, you could
write INT(3.14). The result would be 3, which is an integer.
Logical Functions
Tableau provides various Logical Functions to perform logical operations on data.
AND Function
The Tableau AND function is used to check multiple expressions.
The syntax of the Tableau AND Function :
IF <expr1> AND <expr2> THEN <then> END
• As you see from the above syntax, Tableau AND function accept two arguments. If both the conditions
are True, it returns True. Otherwise, it returns False.
• It will check whether the Profit is greater than 0, and Quantity is greater than 25000. If both these
conditions are true, the Tableau logical AND function will return Good; otherwise, it will return Bad.
IF(SUM([Profit]) > 0 AND SUM(Quantity) > 25000) THEN 'Good' ELSE 'Bad' END
OR Function
The Tableau OR function is like either or statement in English. If both the conditions are False, Tableau or
will return False; otherwise, it returns True. The syntax of this OR Function is:
IF <expr1> OR <expr2> THEN <then> END
IF [Profit] < 0 OR [Profit] = 0 THEN "Needs Improvement" END
Function Syntax Description
ISNULL ISNULL(expression) Returns true if the expression is NULL (does not
contain valid data).
Example:
ISNULL([Profit])

NOT IF NOT <expr> THEN Performs logical negation on an expression.


<then> END Example:
IF NOT [Profit] > 0 THEN "Unprofitable" END
WHEN CASE <expr> WHEN Finds the first <value> that matches <expr> and
<Value1> THEN returns the corresponding <return>.
<return1> ... [ELSE <else>] Example:
END CASE [RomanNumberal] WHEN 'I' THEN 1 WHEN
'II' THEN 2 ELSE 3 END
If-then Logic
• IF <expr> THEN <then> ELSE <else> END
• Tests a series of expressions returning the <then> value for the first true <expr>.
• Example:
• If [Profit] > 0 THEN 'Profitable' ELSE 'Loss' END

END: Must be placed at the end of an expression .


ELSE
The formula is
IF sum([Profit])>7000 THEN 'HighProfit’ ELSE 'LowProfit' END
ELSEIF
• IF <expr> THEN <then> [ELSEIF <expr2> THEN <then2>...] [ELSE <else>] END
Example:
• IF [Profit] > 0 THEN 'Profitable' ELSEIF [Profit] = 0 THEN 'Breakeven' ELSE 'Loss' END
Show Totals in Tableau
• Choose Analysis → Totals
from the Tableau main menu
and then select either the
Show Row Grand Totals or
the Show Column Grand
Totals option, depending on
which totals you want to
include.
Show Totals in Tableau

• Although it is useful to show the


breakdown of measures by various
dimensions, readers value seeing grand
totals for rows and columns. If at least
one Columns or Rows value is
present.owing totals
• To show grand totals in a visualization:
1.Click the Analytics pane.
2.In the Analytics pane, under
Summarize, drag Totals into the Add
Totals dialog, and drop it over either
the Row Grand Totals or Column Grand
Totals option.
Showing Percentages in Tableau
To calculate percentages in Tableau :
• Select Analysis > Percentages Of, and then select a percentage option.

Percentage options
• Percent of Table
• Percent of Column
• Percent of Row
• Percent of Pane
• Percent of Row in Pane
• Percent of Column in Pane
• Percent of Cell
Percent of Table

• When you select Percentage


Of > Table from the Analysis menu,
each measure on the worksheet is
expressed as a percentage of the total
for the entire worksheet (table).
• For example, Technology in the East
region accounts for 3.79% of total sales
in 2014. The grand totals for rows show
that 2014 accounts for 31.95% of the
total sales. Summing the grand totals
for rows or for columns yields 100% of
the total.
Percent of Column
• When you select Percentage
of > Column from
the Analysis menu, each
measure on the worksheet
is expressed as a percentage
of the total for the column.
• The values within the red
box add up to 100%.
Percent of Row
• When you select Percentage
of Row, each measure on the
worksheet is expressed as a
percentage of the total for
the row.
• The values within the red
box add up to 100%.
Percent of Pane
• When you select Percentage
of > Pane from
the Analysis menu, each
measure on the worksheet is
expressed as a percentage of the
total for the panes in the view.
This option is equivalent
to Percent of Table when the
table consists of only a single
pane.
• In the following view, the red box
constitutes a single pane; the
values within the red box add up
to 100%
Percent of Row in Pane
• When you select Percentage
of > Row in Pane from
the Analysis menu, each
measure on the worksheet is
expressed as a percentage of
the total for a row within a
pane. This option is equivalent
to as Percent of Row when the
table is only a single pane wide.
• In the following view, the red
box constitutes a row within a
pane; the values within the red
box add up to 100%.
Percent of Column in Pane
• When you select Percentage
of > Column in Pane from
the Analysis menu, each
measure in the worksheet is
expressed as a percentage of the
total for a column within a pane.
This option is equivalent to
as Percent of Column when the
table is only a single pane high.
• In the following view, the red
box constitutes a column within
a pane; the values within the red
box add up to 100%.
Percent of Cell
• When you select Percentage
Of > Cell from the Analysis
menu, each measure on the
worksheet is expressed as a
percentage of the total for
each individual cell in the
view.
• Most views show only one
value per cell, in which case all
cells show a percentage of
100%. But in some cases, as,
for example, when you
disaggregate data, a single cell
can contain multiple values:
Discretizing Data (Create Bins)
• Sometimes we require discretizing (or binning)
of numeric data for pretty labeling or meeting
some format guidelines; for example, you may
need to report the sales amount in thousands,
and thus you will need to create a field that will
put every sales amount in various bins, for
example, 0-1000, 1000-2000, and so on.
Create Bins from a Continuous Measure
1.In the Data pane, right-click (control-click on
Mac) a measure and select Create > Bins.
2.In the Create Bins dialog box, accept the
proposed New field name or specify a different
name for the new field.
3.Either enter a value in the Size of bins field or
have Tableau calculate a value for you.
Create a Histogram from a Binned Dimension

• If you create a binned dimension, you can use it as the


starting point for creating a histogram. Using the Sales
(bin) dimension created according to the instructions
above, use the following steps to create a histogram.
1.Click the Sales (bin) dimension in the Data pane and
choose Convert to continuous.
2.Drag the Sales (bin) dimension from the Data pane
and drop it on the Columns shelf.
3.Drag the original Sales field from the Measures area of
the Data pane and drop it on the Rows shelf.
4.Click SUM(Sales) on Rows and change the aggregation
from Sum to Count.
Happy Visualizing

You might also like