Grouping, Pivot Tables and Simpson's Paradox
Summarize data by group with pivot tables, drill down to find what drives a change, and see how combining groups can reverse a conclusion.
Welcome back!
Step 1 of 11Learn
Where did the revenue go?
Back to the manager's question from the first lesson: revenue is down 8% from last year. A single total can't explain it. You need to break it down by group, and this is the most common move in all of data analysis.
Grouping (or aggregating) means splitting data into categories and computing a summary for each: total revenue by region, average order value by channel, number of customers by plan. In SQL it's GROUP BY; in spreadsheets it's a pivot table; in BI tools it's dragging a category onto a chart.
Suppose you group last year's and this year's revenue by channel:
| Channel | Last year | This year | Change |
|---|---|---|---|
| Online | $2.0M | $1.5M | −$0.5M |
| Retail stores | $3.0M | $3.1M | +$0.1M |
| Wholesale | $1.0M | $0.92M | −$0.08M |
| Total | $6.0M | $5.52M | −$0.48M (−8%) |
Now the story sharpens: online revenue fell by $0.5M, which is more than the entire net decline. Retail actually grew a little. The next step is to drill down within online: by product, by customer segment, by month, by traffic source. Maybe the drop started in March, exactly when the ad campaign changed.
This approach, splitting a total change into the contributions of its parts, is sometimes called variance analysis or a driver breakdown. It turns "revenue is down" into "online revenue from new customers fell after March", which someone can act on.
Step 2 of 11Practice · Chart
Revenue by channel, last year vs. this year
Tap the channel that explains most of the decline.
Tap the bar that answers the question, then press Check.
Step 3 of 11Learn
Choosing the right aggregation
A pivot table needs three choices: what to group by (rows, and maybe columns), which value to summarize, and which aggregation to apply. The aggregation matters as much as the grouping.
- Sum: totals, such as revenue, units sold, hours worked. Only meaningful for quantities that add up; summing prices or percentages usually makes no sense.
- Count: how many rows, such as number of orders. Count distinct counts unique values, such as the number of different customers. Mixing these up is a classic error: 5,000 orders might come from only 1,800 customers.
- Average (mean): typical value per row, such as average order value. Remember that the average of group averages isn't the overall average unless groups are equal in size.
- Median, minimum, maximum: useful for skewed data and for spotting extremes.
- Share of total: each group's percentage of the whole, which makes groups of different sizes comparable at a glance.
Also watch for:
- Filters and time windows: comparing a full year with eleven months, or including test orders in one period but not another.
- Small groups: a segment with 12 customers can show dramatic percentages that are mostly noise. Show counts next to percentages.
- Too many groups: a table with 300 product rows hides the story. Sort by the measure, show the top 10 and group the rest as "Other".
A useful habit: after building a pivot, check that the grand total matches the total from the raw data. If it doesn't, a filter or a join has gone wrong.
Step 4 of 11Practice · Match the pairs
Match each question to the aggregation that answers it.
Tap an answer, then tap the place where it belongs (or drag it there).
- How many different customers bought something in May?
- What was total revenue by region?
- What is the typical delivery time when a few orders take weeks?
- How many orders did each sales rep process?
- What share of revenue came from each product line?
Step 5 of 11Diagram
Anatomy of a pivot table
Tap a shape with to see more.
- Pivot table
- Rows
What to group by, such as channel.
- Columns
An optional second grouping, such as year.
- Values
The field to summarize, such as revenue.
- Aggregation
Sum, count, count distinct, average, median.
- Filters
Limit to a period, region or segment.
Step 6 of 11Learn
Simpson's paradox: when the total lies
Here's a real pattern from a well-known medical study comparing two kidney stone treatments, A and B:
| Treatment A | Treatment B | |
|---|---|---|
| Small stones | 81 of 87 succeed (93%) | 234 of 270 succeed (87%) |
| Large stones | 192 of 263 succeed (73%) | 55 of 80 succeed (69%) |
| All patients | 273 of 350 (78%) | 289 of 350 (83%) |
Treatment A has a higher success rate for small stones and for large stones. Yet when you combine the groups, B looks better overall. How?
The explanation is in who got which treatment. Doctors tended to give the more invasive Treatment A to harder cases: 263 of its 350 patients had large stones, which have lower success rates no matter what. Treatment B was mostly used on easier, small-stone cases. The combined numbers mix a treatment comparison with a difference in case difficulty.
This reversal is called Simpson's paradox: a trend that appears within every group reverses or disappears when the groups are combined. It happens when a lurking variable (here, stone size) is related both to which group an observation is in and to the outcome.
It shows up in business too. A company's average conversion rate can fall even as every channel improves, if traffic shifts toward a channel that always converts less. University admission rates, hospital outcomes and sales performance by rep can all flip this way.
The defense: when comparing groups, ask what else differs between them, and break the comparison down by the most important lurking variables before drawing conclusions.
Step 7 of 11Practice · Chart
Kidney stone treatments: success rates
Tap the group where Treatment B comes out ahead.
Tap the bar that answers the question, then press Check.
Step 8 of 11Practice · Work it out
Store chain data: In January, Channel X had 1,000 visits with 50 sales and Channel Y had 1,000 visits with 20 sales. In February, X had 200 visits with 12 sales and Y had 1,800 visits with 40 sales. What was the overall conversion rate in February, in percent? (Round to one decimal.)
Step 9 of 11Note
Step 10 of 11Practice · What would you do?
What would you do?
A hospital's overall patient survival rate is lower than a smaller nearby clinic's. A news report concludes the hospital provides worse care. What should an analyst check first?
Step 11 of 11Practice · Choose one
A pivot table shows 6,000 orders in March. A colleague reports "6,000 customers bought from us in March." What is the most likely error?
Lesson complete!