0% found this document useful (0 votes)
28 views30 pages

Class25 Calculated Fields-1

The document outlines the use of calculated fields in Tableau for performing various data operations, including simple calculations, date differences, and data validation. It provides examples such as calculating profit, cost, and handling null values by converting them to zeroes. Additionally, it introduces table calculations and their applications in visualizing data trends over time.

Uploaded by

matthew.76adane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views30 pages

Class25 Calculated Fields-1

The document outlines the use of calculated fields in Tableau for performing various data operations, including simple calculations, date differences, and data validation. It provides examples such as calculating profit, cost, and handling null values by converting them to zeroes. Additionally, it introduces table calculations and their applications in visualizing data trends over time.

Uploaded by

matthew.76adane
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 30

Class 25

Calculated Fields
Table of Contents/Agenda

Simple Calculations in Tableau Slide 4


Time between Dates Slide 11
Introduction to Table Calculations Slide 20
Data Validation - Nulls to Zeroes Slide 23
Why Calculated Fields
• A formula that performs some action on one or more fields in your data
source.
•Adding new fields to dataset
•Cost= Sales - Profit
•Time to Ship = DATEDIFF (‘Day’, Order Date, Ship Date)
•Aggregate functions
•[Distinct Count of Orders]= COUNTD([Order ID])
•Improve Chart Readability
•Sequence 1: 2018-10 2018-11 2018-8 2018-9 ??
•Sequence 2: 2018-08 2018-09 2018-10 2018-11
•Simplifying formulas
•Data validation
Simple Calculations in Tableau
Simple Calculations in Tableau

•Start with [Class 25 Global Superstore].twbx

•Let’s create a simple profit viz


•Profit to Columns

•Market and Subcategory to Rows

•Filter Category to just Furniture

•Rename sheet 1 as Profit Analysis

•Add a title as Profit by Market and Sub-Category and center it


Simple Calculations in Tableau
Example 1: IF Logic

•What if we want, for emphasis, to have one distinct color for positive
and one for negative?
•Calculated fields are created by defining a formula:
•Drop-down Analysis…Create Calculated Field
•IF SUM([Profit]) > 0 THEN "positive" ELSE "negative" END
•Remember that Measures need an aggregation level
•We are ignoring if Profits are actually zero (Nested IF)
•Name it as “Sign of Profit”, make a comment in your code using //
•The little equal sign in front of the field indicates it’s a calculated field
•Bring this calculated field to Color card
Example 1: IF Logic
Example 2: Cost

•There might be a field of interest to me that is not in the data.

•For example, maybe I want to visualize Cost, defined as Sales-Profit.

•Add a Calculated field for that => Cost= SUM([Sales]) -


SUM([Profit]).

•Duplicate Profit Analysis sheet and name it Cost Analysis.

•Drag Profit out of view and replace with Cost. Rename the chart title.
Example 2: Cost
Time between Dates
Time between Dates
•A common question could be something like “How much time did
something take?”

•When you have a start and an end date in your data


•A date calculation can handle that.

•Let’s look at the average time it takes for an order to ship


•Right click in the data window and select Create Calculated Field

•Name it “Time to Ship”

•We’ll use a function called DATEDIFF to get the amount of time between
Order Date and Shipping Date.
Time between Dates: Time to Ship

•Search for DATEDIFF; the first argument needed is the date part
•Type ‘day’ and a comma.

•Next, drag out Order Date, another comma, and Ship Date
•DATEDIFF(‘day’, [Order Date], [Ship Date])

•Be sure to check that the calculation is valid, then click OK.
Time between Dates: Time to Ship
Time between Dates: Time to Ship
Comparison
•Coloring countries by using Time to Ship
•Add a new sheet and name as Time to Ship Comparison.

•Select Country and Time to Ship, select Filled Map from Show Me.

•Change the aggregation to Average.

•Use Red-Green Diverging.

•Center the title.


Time between Dates: Time to Ship
Comparison
Sorted Year-Month Field
•Time series analysis

•Using the Year-Month in the X-Axis


•2018-03 not 2018-3

•2018-3 > 2018-10. As a date value, which one is actually greater?

•Keeping the data sorted by months

•Solution => Create a Year-Month calculated field and add 0 for one-
digit months
•IF MONTH([Order Date])<10 then STR(YEAR([Order Date]))+ "-0" +
STR(MONTH([Order Date])) else STR(YEAR([Order Date]))+ "-" +
STR(MONTH([Order Date])) END
Line Chart
•Add a new sheet and name Sales Over Time Across Categories.

•Drag Sales to Rows and Year-Month to Columns.

•Drag Category to Color card. Drag Order Date to Detail card.

•Hide the color legend. Uncheck 2013 and 2014 from the Filters shelf.

•Rotate X-Axis values. Center the title.


Line Chart
Introduction to Table Calculations
Introduction to Table Calculations
•A secondary calculation that is performed on top of a returned result
set.
•Tableau includes a set of pre-defined, commonly used computations
called Quick Table Calculations.
•These include options like Running Total, Percent of Total, and Year over
Year Growth.
•Let’s try one
•New Sheet name as Cumulative Sales Over Time.
•Category to Columns, Sales to Rows. Center the title.
•Order Date to Columns, Make it a Continuous Date, Expand to Quarters.
•Sales to Rows again, drop-down and select the Running Total Quick Table
Calculation.
•Now you have a set of graphs with quarterly sales as well as a running total.
Introduction to Table Calculations
Cumulative Sales Over Time
Data Validation - Nulls to Zeroes
Data Validation - Nulls to Zeroes

•A common issue with real-world datasets is the presence of null values


or blanks in the data.
•This can make data analysis challenging and frustrating.
•With Calculations we can easily remove Nulls from our data
•Open [Sales Quotas].twbx.
•In this dataset, we have sales quotas for some, but not all countries.
•Double-click Country to get a dot everywhere we do business.
•I want to see how we are doing relative to quotas everywhere.
•But I have included an issue in the data that needs to be fixed. Right-click
somewhere in the map and view data.
•There are several nulls for Quotas.
Data Validation - Nulls to Zeroes
Data Validation - Nulls to Zeroes
•Our main goal is to compare the sales to their quotas, but we cannot
compare a number to a null. We need to convert our nulls to zeros so that
we can do a comparison.

•Right-click in the data window and select create calculated field.

•Name it “Corrected Quotas”


•And type ZN(Quotas)

•The ZN function returns a zero wherever there is a null. We can think of


this function as asking “Is there a quota for this country? If not, give it a
zero.”
Data Validation - Nulls to Zeroes
Data Validation - Nulls to Zeroes
•Now we can visualize how different our sales are from our quotas.
Again, we can right-click and create a Calculated Field
•We’ll call this our “QuotasCompare”

•SUM(Sales)-SUM(Corrected Quotas)

•Double-click QuotaCompare to bring it into the view.

•Taiwan, Portugal, and Austria look pretty good. To get more data into
the viz, add Corrected Quotas to the Tooltip. Why do these 3 countries
look so strong?
Data Validation - Nulls to Zeroes

You might also like