Level of Detail Expressions
Level-of-Detail Calculations
A regional sales dashboard can display one mark per region while answering a question about individual customers. For example, average customer spending requires you to total each customer's purchases before averaging those totals. Level-of-Detail Calculations let you control this intermediate grouping without adding every grouping field to the visible chart.
Identify the calculation grain
The grain is the combination of dimensions that identifies one group for a calculation. With Region alone in a view, ordinary SUM(Sales) gives one total per region. Adding Product Category divides each region into smaller groups, so the ordinary sum now describes each region-category combination.
A level-of-detail calculation explicitly controls the dimensions used for an aggregation. Its calculation grain can differ from the grain of the displayed marks. Before writing one, state the business question as a sequence: identify the entities, calculate a value for each entity, and decide how to summarize those values.
For average customer spending within a region, the governing idea is:
Suppose one region has three order-line sales: customer A purchases CAD 80 and CAD 120, while customer B purchases CAD 400. The customer totals are CAD 200 and CAD 400. Their average is CAD 300 per customer; averaging the three original rows instead gives CAD 200 per order line. Both calculations are valid, but they answer different questions.
Choose the scope
The common syntax places a scope keyword and dimensions before a colon, followed by an aggregation:
{FIXED [Region] : SUM([Sales])}Braces enclose the expression, square brackets identify fields, and SUM specifies what happens within each group. Separate multiple declared dimensions with commas.
Scope | Calculation grain | Typical purpose |
|---|---|---|
FIXED | The declared dimensions, independently of view dimensions | Preserve a named grouping when the view changes |
INCLUDE | View dimensions plus declared dimensions | Calculate finer groups before summarizing them |
EXCLUDE | View dimensions minus declared dimensions | Provide a broader comparison within detailed marks |
For customer spending in a Region-only view, use:
{INCLUDE [Customer ID] : SUM([Sales])}Set this field's display aggregation to Average. INCLUDE first calculates totals at Region plus Customer ID; Average then summarizes those customer totals for each region.
In a Region-and-Product-Category view, the following expression removes the category split while retaining Region:
{EXCLUDE [Product Category] : SUM([Sales])}The regional total can therefore appear beside each category's sales. By contrast, FIXED [Region] explicitly names Region as its calculation grain, even if other dimensions enter the view.
Check aggregation and filter behavior
Inspect both the aggregation inside the braces and the aggregation used to display the result. Summing customer totals produces regional sales; averaging them produces average spending per customer. Changing the outer aggregation changes the business question when finer results are summarized.
When a broader total repeats across detailed marks, repetition does not create additional sales. Do not manually add the repeated regional totals across category rows to obtain a company total.
Filter timing also affects meaning. FIXED is evaluated after extract, data-source, and context filters, but before ordinary dimension filters. INCLUDE and EXCLUDE are evaluated after ordinary dimension filters. Thus, an ordinary category filter can restrict visible sales while a FIXED regional denominator still includes all categories. Making that filter a context filter changes the rows available to FIXED.
What to remember
- State the entity and grouping before choosing a scope.
- Use FIXED for declared dimensions, INCLUDE to add detail, and EXCLUDE to remove view detail.
- Match the display aggregation to the intended business measure.
- Check filter timing before interpreting a comparison or percentage.
Example: Customer spending and regional comparisons
A distributor records the following order-line sales for one reporting month. These are all its records for that month. Each customer belongs to one region, all customer IDs are present, and there are initially no filters. Sales are in Canadian dollars. In Tableau, the numeric sales field is named Sales.
Region | Customer ID | Order-line sales (CAD) |
|---|---|---|
North | N1 | 120 |
North | N1 | 180 |
North | N2 | 500 |
South | S1 | 240 |
South | S2 | 360 |
Give monetary results to two decimal places and percentages to one decimal place.
a) A view contains Region as its only dimension. Write an INCLUDE calculation for average total spending per customer, state its display aggregation, and calculate the result for each region.
First total the order lines for each customer within each region. Create a field named Customer Spending with the expression {INCLUDE [Customer ID] : SUM([Sales])}.
Display Customer Spending using Average. Region comes from the view, while INCLUDE adds Customer ID for the intermediate calculation.
The North customer totals are:
Average the two customer totals:
The South customer totals are CAD ________ and CAD ________:
As a check, averaging North's three original order lines would give approximately 800/3=266.67 CAD per order line. That is a different measure because N1 contributes two rows. The requested results are CAD 400.00 per customer for North and CAD 300.00 per customer for South.
b) Change the view to Region plus Customer ID. Write an EXCLUDE expression for the regional sales total beside each customer and calculate the values shown.
Remove Customer ID from the calculation grain while retaining Region from the view. Use the expression {EXCLUDE [Customer ID] : SUM([Sales])}.
Sum the sales in each remaining regional group:
The displayed regional comparison values are therefore:
- North, customer N1: CAD 800.00.
- North, customer N2: CAD 800.00.
- South, customer S1: CAD 600.00.
- South, customer S2: CAD 600.00.
The values repeat because the customer marks are finer than the calculation grain. Repeated totals are comparisons, not additional sales. The actual combined regional sales are 800+600=1400.00 CAD; manually adding all four displayed comparison values would count each regional total twice.
c) In the Region-and-Customer-ID view, define Regional Sales using {FIXED [Region] : SUM([Sales])}.
Calculate a customer's share using SUM([Sales]) / MIN([Regional Sales]).
Apply an ordinary Customer ID dimension filter that retains only N1. Find N1's displayed share. Then find the share if that same filter becomes a context filter.
Each mark belongs to one region, so MIN retrieves that region's single FIXED comparison value. With an ordinary Customer ID filter, FIXED is evaluated before the filter removes N2. N1's numerator contains its own sales, while the North denominator still contains both North customers:
When Customer ID becomes a context filter, only N1's rows reach FIXED. The North denominator is recalculated from those rows:
The first result compares N1 with the original regional customer population. The second compares N1 with the retained customer population. The difference follows from when the filter acts, not from any change to N1's purchases. N1's share is 37.5% with the ordinary dimension filter and 100.0% with the context filter.
A retail analyst is reviewing the Tableau calculation { FIXED [Store ID] : SUM([Sales]) } before saving it. Match each syntax component to its role in a level of detail expression.
Enclose the entire level of detail calculation.
Lists the fields that specify grouping under the selected rule.
Separates the grouping portion from the calculation performed within each group.
Specifies the summary calculation performed within each group.
Selects the rule governing how grouping relates to the view.
Colon
Scoping keyword
Dimension declaration
Aggregate expression
Curly braces
A retailer has one row per order line, and customers place different numbers of orders. A Tableau view contains Region as its only dimension. The analyst wants average total sales per customer within each region, giving each customer equal weight within that region. Assume no filters or missing customer IDs. Which calculation and display aggregation meet this requirement?