Sub topic 7
Sorting results and controlling totals
This chapter covers how to order the rows and columns of a pivot table and how to control the totals it shows. It assumes you can already build a pivot table with fields in Rows, Columns and Values, and that you know how Summarize by works. It also assumes you are comfortable reading a sum, count and average.
Why sorting and totals need attention
A new pivot table is not always in a useful order. By default, Google Sheets sorts row and column labels alphabetically (or in date order for dates). That is rarely the order you want when you ask "which region sells the most?" or "who are my top five customers?".
Totals have the opposite problem. They appear automatically, and it is easy to trust them without checking what they calculate. A grand total of an average, for example, is not the sum of the averages above it.
The examples below use a Sales sheet with these columns: Date, Region, Product, Rep and Revenue. The pivot table has Region in Rows, Product in Columns, and the sum of Revenue in Values.
Sorting row and column groups
Each field in Rows or Columns has its own sort settings in the pivot table editor. Open the editor, find the field under Rows or Columns, and look for the Order and Sort by drop-downs.
Order
Order sets the direction:
- Ascending runs A to Z, smallest to largest, or oldest to newest.
- Descending runs Z to A, largest to smallest, or newest to oldest.
Sort by
Sort by sets what the order is based on. The choices include:
- The field's own labels, such as the region names.
- Any value field in the pivot table, such as "SUM of Revenue".
To rank regions by revenue, set Region's Order to Descending and Sort by to "SUM of Revenue". The region with the highest total moves to the top. If you leave Sort by on the label, the regions stay in alphabetical order no matter how large the numbers are.
If you have several value fields, such as sum of Revenue and count of Rep, choose the one that matches your question. Sort by uses the value field you pick, not the first one in the list.
Sorting within nested groups
When you put two fields in Rows, each field sorts separately. Suppose Region comes first and Product second. The Region sort orders the regions. The Product sort orders products inside each region. Sorting by "SUM of Revenue" on Product ranks products within each region, not across the whole table.
Sorting by a column field
Column fields can also be sorted by value. If Product is in Columns, sorting it by "SUM of Revenue" orders the product columns by their overall totals. This works best when you want the best-selling product to appear on the left.
A different case is sorting by a single column's values across the rows. The Order and Sort by settings do not rank rows by one specific column. They rank by the total for the whole group. If you need rows ranked by one product's revenue, use a filter or a formula such as QUERY, which has an order by clause.
Controlling totals
Each field in Rows and Columns has a Show totals checkbox. The totals you see are:
- Subtotals for each group of an outer field when there is more than one row (or column) field.
- Grand totals at the bottom of the rows and at the right of the columns.
Show totals is set per field. Turning it off for Region removes the Region subtotals and the grand total for that axis. Leave it on for the field you want totals from.
What a total actually calculates
A total applies the same Summarize by function to the underlying records. It does not add up the numbers displayed above it.
| Summarize by | What the total does |
|---|---|
| SUM | Adds all records in the group. Matches the sum of the visible cells. |
| COUNT, COUNTA | Counts all records in the group. Matches the sum of the visible cells. |
| AVERAGE | Averages all records in the group. Does not equal the average of the displayed averages. |
| MAX, MIN | Gives the largest or smallest record in the group. |
| COUNTUNIQUE | Counts distinct values in the group. Can be less than the sum of the visible cells, because a value can repeat across groups. |
The AVERAGE row is the one that surprises people. Say West has two sales of 100 and 300 in Product A, and one sale of 200 in Product B. The cell averages are 200 and 200, but the West total is the average of all three records, which is 200. Change Product B to 500 and the cell averages become 200 and 500, but the West total is 300, not the 350 you would get by averaging the cells. Each record counts once, so the larger group weighs more.
Totals with Show as
When you use a Show as option, covered in a later chapter, the totals change meaning too. A "% of grand total" view makes the grand total 100%. Check what the total represents before you quote it.
Hiding totals for chart-ready tables
Totals can get in the way when the pivot table feeds a chart or another formula. A grand total row looks like one more category to a chart. Turn Show totals off on the fields you plan to chart so the totals do not appear as extra bars or slices.
Reading totals from outside the pivot
Because totals can change position when you sort, filter or add fields, avoid pointing a formula at a total cell such as Pivot!F12. A later change to the pivot can shift that cell, and the formula will silently read the wrong number. GETPIVOTDATA, covered later, looks up values by field and item name instead of position. Here is the formula for the West region total, using the sample layout above:
=GETPIVOTDATA("Revenue", Pivot!A1, "Region", "West")To cross-check a total, calculate it from the source data:
=SUMIFS(Sales!E:E, Sales!B:B, "West")If the two results differ, a filter on the pivot table is the usual cause. Pivot filters exclude records from totals as well as from visible cells.
Worked example: top regions with clean totals
Follow these steps on the Sales sheet.
- Select the data range and choose Insert > Pivot table. Place it on a new sheet named
Pivot. - Under Rows, add Region. Under Values, add Revenue and keep Summarize by as SUM.
- In the Region settings, set Order to Descending and Sort by to "SUM of Revenue". The regions now rank from highest to lowest revenue.
- Under Columns, add Product. Leave Show totals on. A grand total column appears at the right.
- Set the Product field's Sort by to "SUM of Revenue" and Order to Descending. The best-selling product now appears first.
- Compare the Grand Total row with
=SUM(Sales!E2:E). They should match, unless a filter excludes some records. - Turn off Show totals for Product. The grand total column disappears while the Region ranking stays in place.
Common problems
- Sort does not seem to apply. Check that Sort by is set to a value field and not to the labels. Alphabetical order is the default.
- Blank labels sort to an odd position. Records with an empty Region form a blank group. Fix the source data or filter the blank out. Blanks are covered in the filtering chapter.
- A total looks wrong for an average. The total is computed over records, not over the displayed averages, as the table above explains.
- Numbers stored as text sort alphabetically. If a label such as a month number is text, "10" sorts before "2". Convert the source column to numbers or dates.
- A total changed after you added a filter. Filters apply before totals are calculated, so the totals shrink with the visible data.
Exercise
A pivot table has Region in Rows and the average of Revenue in Values. West shows two product cells averaging 200 and 500, based on two sales of 200 and 500 in the first product and two sales of 500 each in the second... Simplify: West has three sales in total, 100 and 300 in Product A, and 500 in Product B. What are the two cell averages, what does the West total show, and why does it differ from the average of the cell averages?
Show answer
Product A averages 200 (100 and 300) and Product B averages 500. The West total averages all three records: (100 + 300 + 500) / 3 = 300. The average of the cell averages would be (200 + 500) / 2 = 350. They differ because the total weighs each record once, so Product A, which has two records, counts twice as much as Product B.
Exercise
You want regions ranked from highest to lowest total revenue, but the pivot table shows them alphabetically. Which two settings on the Region field do you change?
Show answer
Set Sort by to "SUM of Revenue" and set Order to Descending. Sort by chooses what the ranking is based on, and Order chooses the direction.