01 / 20
SUM AVG COUNT COUNTD
\( \Sigma \)

Aggregations in
Tableau Calculations

Master the four essential aggregation functions: \( \text{SUM} \), \( \text{AVG} \), \( \text{COUNT} \), and \( \text{COUNTD} \) and learn how to use them correctly inside calculated fields.

Tableau

What Is an Aggregation?

An aggregation is a function that takes many rows of data and reduces them to a single value. When you drag a measure onto a Tableau view, Tableau automatically wraps it in an aggregation: usually \( \text{SUM} \).

Raw Rows $120 $85 $200 $45 $150 ... 1000 rows SUM( ) Result $72,450

The Big Four Aggregations

\( \text{SUM} \)
Adds all values together. Best for totals like revenue or quantity.
\( \text{AVG} \)
Computes the arithmetic mean. Best for per-unit metrics like average price.
\( \text{COUNT} \)
Counts the number of non-null values, including duplicates.
\( \text{COUNTD} \)
Counts distinct (unique) values only. Great for counting unique customers.

Why Aggregations Matter

Tableau operates at two levels: the row level (each individual record) and the aggregate level (summarized groups). Every visualization you build works at the aggregate level. Understanding this distinction unlocks the ability to write correct calculated fields.

Row Level Each record is individual
\( \text{[Profit]} / \text{[Sales]} \)
No aggregation wrapper Fields appear as "abc" or "#" in the data pane Aggregate Level Records are grouped
\( \text{SUM}(\text{[Profit]}) / \text{SUM}(\text{[Sales]}) \)
Wrapped in AGG function Fields appear with "AGG" prefix in the view

The Golden Rule

// ✕ ERROR: mixing levels
SUM([Sales]) / [Quantity]

// ✓ CORRECT: both aggregated
SUM([Sales]) / SUM([Quantity])

// ✓ CORRECT: neither aggregated (row-level)
[Sales] / [Quantity]

Dimensions Control Granularity

Aggregation results change depending on which dimensions are in the view. Adding a dimension splits the aggregation into smaller groups. This is called the level of detail (LOD).

No Dimension $72,450 SUM of all Sales (grand total) → Add Region Region Dimension East: $25,100 West: $22,350 South: $15,000 Central: $10,000 SUM split per region

\( \text{COUNT} \) vs \( \text{COUNTD} \)

Customer Name Alice Bob Alice Carol Bob Alice Dave (duplicates in amber)
\( \text{COUNT} = 7 \)
Counts every row
\( \text{COUNTD} = 4 \)
Unique names only

\( \text{SUM} \) vs \( \text{AVG} \): When to Use Each

Use \( \text{SUM} \) When...
You want a total: total revenue, total quantity sold, total profit. Each row's value is additive.
Use \( \text{AVG} \) When...
You want a per-unit measure: average order value, average rating, average discount rate.
// Total revenue across all orders
SUM([Sales])

// Average discount per order
AVG([Discount])

How to Write an Aggregated Calculation

1
Identify the goal. Do you need a total, an average, a count, or a unique count?
2
Pick the right function. \( \text{SUM} \) for totals, \( \text{AVG} \) for means, \( \text{COUNT} \) for rows, \( \text{COUNTD} \) for uniques.
3
Wrap every measure. If one field is aggregated, all fields in the formula must be aggregated too.
4
Check granularity. Add the calculation to a view and confirm it returns the correct value for each group.
5
Validate with the data. Compare your result against a known value or a simple filter to spot errors.

Example 1: Profit Ratio

Goal: Calculate the profit ratio (profit as a percentage of sales) for each region.

// Profit Ratio (aggregated)
SUM([Profit]) / SUM([Sales])
Region
\( \text{SUM}(\text{[Profit]}) \)
Profit Ratio East $6,275 25% West $3,350 15% South $1,800 12% Central $900 9%

Example 2: Average Order Value

Goal: Calculate the average revenue per order for each product category.

// Average Order Value
SUM([Sales]) / COUNTD([Order ID])
\( \text{SUM}(\text{[Sales]}) \)
Total revenue across all rows in each category
\( \text{COUNTD}(\text{[Order ID]}) \)
Number of unique orders in that category
Result
Revenue divided by unique orders = true average per order

Example 3: Percent of Total

Goal: Show each category's sales as a percentage of the grand total.

// Percent of Total Sales
SUM([Sales]) / TOTAL(SUM([Sales]))
\( \text{SUM}(\text{[Sales]}) \)
per partition
\( \text{TOTAL}() \)
sums across all partitions Divide = % of Total

Row-Level vs Aggregate Calculations

Row-Level Aggregate
Computed at Each individual row Group of rows
Syntax \( \text{[Field A]} / \text{[Field B]} \) \( \text{SUM}(\text{[A]}) / \text{SUM}(\text{[B]}) \)
When to use Per-record metrics Summary metrics
Example \( \text{[Unit Price]} \times \text{[Quantity]} \) \( \text{SUM}(\text{[Sales]}) / \text{COUNT}(\text{[Orders]}) \)
Result type New data column Aggregate measure

Common Mistakes

✕ Mixing Levels
Writing \( \text{SUM}(\text{[Sales]}) / \text{[Quantity]} \) mixes aggregate and row-level. Wrap both or neither.
✕ \( \text{COUNT} \) Instead of \( \text{COUNTD} \)
Using \( \text{COUNT}(\text{[Customer]}) \) when you want unique customers. \( \text{COUNT} \) includes duplicates.
✕ \( \text{AVG} \) of a Ratio
Using \( \text{AVG}(\text{[Profit]}/\text{[Sales]}) \) gives equal weight to every row. Use \( \text{SUM}(\text{[Profit]})/\text{SUM}(\text{[Sales]}) \) for a weighted result.

Summary of Aggregation Functions

Function Returns Best For Watch Out
\( \text{SUM} \) Total of all values Revenue, Profit, Qty Doesn't work for ratios
\( \text{AVG} \) Arithmetic mean Ratings, Temperatures Unweighted: misleading for ratios
\( \text{COUNT} \) Number of non-null rows Total records, events Includes duplicates
\( \text{COUNTD} \) Number of unique values Unique customers, IDs Slower on large datasets

Question 1 of 5

What will Tableau do if you write \( \text{SUM}(\text{[Sales]}) / \text{[Quantity]} \) in a calculated field?

Question 2 of 5

You have an orders table where each row is a line item. A single order can have multiple line items. Which formula correctly counts the number of unique orders?

Question 3 of 5

You want the profit ratio for each product category. Which calculated field is most appropriate?

Question 4 of 5

Adding the Region dimension to a view that shows \( \text{SUM}(\text{[Sales]}) \) will:

Question 5 of 5

A calculated field uses \( \text{[Unit Price]} \times \text{[Quantity]} \) with no aggregation functions. What type of calculation is this?