Sub topic 8
Manually Grouping Text Items
What manual grouping does
Dates and numbers can be grouped by rule, because Excel can compute buckets from their values. Text has no such rule. Excel cannot know that "Maine" and "Vermont" belong together, so you choose the items yourself. This is manual grouping: you select items in the PivotTable, tell Excel to group them, and give each group a name.
Use it when the categories you want do not exist in the source data. Examples are combining states into regions, products into families, or departments into divisions. The grouping lives in the PivotTable only. The source data does not change.
This chapter assumes you can build a PivotTable with a text field in Rows and a Sum in Values. It also assumes you know that the Grouping dialog is where date and number grouping happens. Text grouping skips that dialog, because there are no units or ranges to set.
A worked example
The examples use a small sales table with the columns Region, Product, and Amount. The Region column holds individual states: Maine, Vermont, Texas, Oklahoma, Oregon, and Washington. The PivotTable has Region in Rows and Sum of Amount in Values.
| Region | Sum of Amount |
|---|---|
| Maine | 12,400 |
| Oklahoma | 9,300 |
| Oregon | 15,100 |
| Texas | 21,800 |
| Vermont | 8,700 |
| Washington | 17,600 |
Six rows is easy to read. With fifty states it would not be, and regional totals are what the report needs.
Creating a group
- Click the first item you want in the group, for example Maine.
- Hold Ctrl (Cmd on a Mac) and click the other items, for example Vermont. You can also drag across a run of adjacent items, or click the first item and Shift+click the last.
- Right-click one of the selected items and choose Group. The ribbon route is PivotTable Analyze, then Group Selection.
Excel creates a group header named Group1 and moves the selected items beneath it. Repeat the steps for the next set of items. Each new group is named Group2, Group3, and so on, in the order you create them.
The selection must be items from a single field, and they must be visible in the PivotTable. Selecting cells from two different fields, or selecting a value cell, makes the Group command unavailable or produces an error.
What Excel adds to your layout
Grouping does not rewrite the original field. Excel adds a second field to the PivotTable field list, named after the original with a number appended, such as Region2. This new field holds the group names and is placed in Rows as the outer level, above the original field.
The structure looks like this:
In the PivotTable, the group header row shows the subtotal for its members. With the Compact layout, the original items are indented below it. With the Tabular layout, Region2 and Region appear in separate columns.
This two-field design explains most of the behavior in the rest of the chapter. The original field is never replaced, so you can always ungroup, and you can always choose which of the two fields to show.
Renaming groups
Group1 is not a useful label. To rename a group, click its header cell, type the new name, and press Enter. You can also edit it in the formula bar. Rename every group before sharing the report.
After renaming, the table might look like this:
| Region2 | Region | Sum of Amount |
|---|---|---|
| Northeast | Maine | 12,400 |
| Northeast | Vermont | 8,700 |
| South | Oklahoma | 9,300 |
| South | Texas | 21,800 |
| West | Oregon | 15,100 |
| West | Washington | 17,600 |
The field header Region2 can also be renamed the same way. Click the header cell and type, for example, Sales Area. Excel accepts any name that is not already used by another field in the PivotTable.
Showing only the groups
Often you want only the group totals. You have two options.
- Collapse the groups. Click the minus button next to a group header, or right-click and choose Expand/Collapse, then Collapse Entire Field. The member rows are hidden but still in the PivotTable.
- Remove the original field from the layout. Drag
Regionout of Rows, or clear its check box in the field list. TheRegion2field stays, and you see one row per group. The grouping is still stored, so puttingRegionback restores the detail.
Collapsing is better when readers may want to drill down. Removing the field gives a simpler layout when the detail should not appear at all.
Items you did not select
If you group only some of the items, the others are not lost. Each ungrouped item appears in the new outer field under its own name. Suppose you group Maine and Vermont into Northeast and leave the other four states alone. The outer field then shows Northeast, Oklahoma, Oregon, Texas, and Washington. Each of the four states appears as its own one-item row.
This is useful for a partial summary. It can also be a surprise. A report that you thought had three regions has five rows because two items were never selected. After grouping, check that the outer field contains only the names you expect.
Ungrouping and editing groups
To dissolve one group, right-click its header cell and choose Ungroup. The member items return to the outer field as individual rows. The other groups are unchanged. When no groups remain, the extra field disappears from the field list.
To move an item from one group to another, select the item and use Group. You can also drag the item's cell onto a different group's rows. To change which items are in a group, ungroup it and regroup, or move the single item.
Ungrouping a group header is safe. It does not delete any source data or any other PivotTable setting.
What happens when the data changes
Manual groups are stored by item name. The group Northeast records the strings "Maine" and "Vermont". After you refresh, these rules apply:
- Changed values. If the amounts change, the group subtotals update. Group membership is the same.
- New items. A new state, such as "New Hampshire", appears in the outer field as its own row. It is not added to Northeast automatically. You must select it and add it to a group.
- Renamed items. If someone changes "Maine" to "ME" in the source, the old name may stay in the group as a stale item, and "ME" appears as a new ungrouped row. The same effect appears with a typo, a trailing space, or different capitalization in the source.
- Removed items. If no rows use a name any more, the group may continue to hold that stale item until the PivotTable cache is cleared, depending on your Retain items setting. The group's subtotal does not include it, because no rows match it.
For a PivotTable that is refreshed often with changing categories, this maintenance is a good reason to consider a helper column, covered below.
Limits to know
- Manual grouping cannot be driven by a rule or a formula. Every item is chosen by hand.
- Groups are scoped to one PivotTable's cache. A second PivotTable built from the same source may share the cache and show the same groups, or may not, depending on how it was created. Do not assume that a copy of the report carries your groups.
- In PivotTables built from the Data Model, the Group Selection command is unavailable for text items in most Excel versions. If the command is gray, check how the PivotTable was created.
- Slicers and filters work on the original field, not on the group names. Filtering the new outer field works from its own filter button.
- The group names are labels in the PivotTable. A formula such as GETPIVOTDATA can refer to them, but a rename breaks it.
A helper column as an alternative
When the mapping is stable or large, put it in the source data. Create a small mapping table on another sheet, then add a column to the data that looks up each item's category. For a Table named Sales and a mapping Table named RegionMap with columns State and Area, the helper column formula is:
=XLOOKUP([@Region], RegionMap[State], RegionMap[Area], "Unmapped")The fourth argument labels any state missing from the map. After a refresh, an Unmapped row is a clear sign that the map needs a new entry. Add the helper column to Rows like any other field.
Compare the two approaches:
| Question | Manual grouping | Helper column |
|---|---|---|
| Setup effort | Seconds | Minutes to build the map |
| New items after refresh | Appear ungrouped | Appear as Unmapped, or mapped if listed |
| Works with Data Model | Often not | Yes, if the column is in the model |
| Same grouping in other reports | Not reliably | Yes, because it is in the data |
| Source data unchanged | Yes | No, a column is added |
Choose manual grouping for quick, one-off summaries. Choose a helper column when others will reuse the grouping or when new items arrive regularly.
Common problems
- Group is grayed out. You selected a value cell, cells from two fields, or the PivotTable comes from the Data Model. Select only row or column labels from one field.
- A group shows the wrong total. An item was missed or a name changed in the source. Look for stray ungrouped rows in the outer field.
- Group names reset to
Group1. This can happen if you ungroup and regroup. Rename again, or use a helper column to keep names stable. - Field name is
Region2and you did not expect it. This is normal. The number is Excel's way of keeping field names unique.
Exercise
A PivotTable has Region in Rows with the items Maine, Vermont, Texas, and Oklahoma. You group Maine and Vermont and rename the group Northeast. You leave Texas and Oklahoma alone. What does the new outer field contain, and what changes if you remove the original Region field from Rows?
Show answer
The outer field (Region2) contains three rows: Northeast, Texas, and Oklahoma. The ungrouped items appear under their own names. If you remove the original Region field from Rows, the PivotTable shows only those three rows, with no member rows beneath Northeast. The group is still stored, and returning Region to Rows restores the detail.
Exercise
You refresh a PivotTable that groups states into Northeast, South, and West. A new state, New Hampshire, appears in the source data. Where does it show up, and name two ways to deal with it permanently?
Show answer
New Hampshire appears as its own row in the outer field, not inside Northeast. To fix it once, select New Hampshire and one Northeast item, then use Group, or drag it into the Northeast group. To handle it permanently, add the state to a mapping table and use a helper column such as =XLOOKUP([@Region], RegionMap[State], RegionMap[Area], "Unmapped") in the source data, so new states are mapped by a lookup instead of by hand.