Sub topic 6
Grouping Numbers into Equal-Width Ranges
This chapter assumes you can build a PivotTable with fields in Rows, Columns, and Values. It also assumes you can use Sum and Count as value summaries, and that your data sits in a table or contiguous range with one header row. Earlier chapters covered why Excel can group numbers at all. Here you will use that ability to turn a numeric field into buckets of equal width, such as 0-9, 10-19, and 20-29.
What numeric grouping does
A PivotTable normally lists every distinct value of a field in Rows. If the field holds ages, prices, or scores, that can mean hundreds of rows with one or two records each. Grouping replaces those rows with ranges of equal width. Each record falls into exactly one range, and the Values area summarizes all records in that range.
Excel needs three settings to build the ranges:
| Setting | Meaning |
|---|---|
| Starting at | The lower edge of the first range. |
| Ending at | The upper edge of the last range. |
| By | The width of every range. |
By default, Starting at and Ending at show the smallest and largest numbers in the field. You can change them to cleaner values, such as 0 and 100.
A worked example
Suppose a table named Sales has these columns: Customer, Region, Age, and Amount. You want to know how many customers and how much revenue fall into each ten-year age band.
- Select any cell in the table and choose Insert > PivotTable. Place it on a new worksheet.
- Drag
Ageto Rows. - Drag
Amountto Values. Set it to Sum of Amount. - Drag
Customerto Values as well. Set it to Count of Customer.
At this point the PivotTable has one row for every distinct age. Now group it:
- Right-click any age value in the Rows area.
- Choose Group.
- In the Grouping dialog, set Starting at to
20, Ending at to69, and By to10. - Click OK.
The rows now read 20-29, 30-39, 40-49, 50-59, and 60-69. Each row shows the total amount and the customer count for that band.
How the boundaries work
Each range includes its lower edge and excludes its upper edge in the underlying logic. With Starting at 20 and By 10, the first range holds values from 20 up to, but not including, 30. Excel labels it 20-29. That label is accurate for whole numbers. For decimal values it can mislead.
Consider a value of 29.5. It falls into the 20-29 range because it is at least 20 and less than 30, even though 29.5 is greater than 29. If your data has decimals, read the labels as "from the first number up to just under the second."
The same rule explains what happens at the edges:
- A value equal to Ending at is included in the last range. Excel handles the final boundary so the largest value is not dropped.
- A value below Starting at goes into a range labeled
<20. - A value above Ending at goes into a range labeled
>69.
These two extra rows appear only when some records fall outside your chosen limits. They are a quick way to see that your limits cut off data.
Choosing a width
The By value must be a positive number. It does not have to be a whole number. A width of 0.5 works for a field of ratings, and 1000 works for salaries.
Pick a width that matches how people read the data. Ten-year age bands, price steps of 25, and score bands of 10 are all common. Very narrow widths bring back the clutter you were trying to remove. Very wide widths hide differences.
You can also reopen the dialog and try several widths. Because grouping is a setting on the PivotTable and not a change to your data, trying a different width is cheap.
Counting records per range
A frequent use of numeric grouping is a histogram table: how many records fall into each range.
- Drag the numeric field to Rows and group it.
- Drag any field that has no blanks, such as an ID column, to Values.
- Set the summary to Count.
If you drag the numeric field itself into Values, it defaults to Sum. Change it to Count in Value Field Settings, or you will add up the values instead of counting them.
To show each range as a share of the total, open Value Field Settings, choose the Show Values As tab, and pick % of Grand Total.
Grouping a numeric field in Columns
Grouping works the same way in Columns. This is useful for a cross-tab. For example, put Region in Rows, grouped Age in Columns, and Count of Customer in Values. The result shows how many customers of each age band live in each region.
If the same field is in more than one area, grouping it once applies everywhere that field appears. A PivotTable field has one grouping at a time.
Requirements for numeric grouping
Grouping a number field fails if the source column contains values Excel cannot treat as numbers. The common causes are:
- Text that looks like a number, such as
"25"stored with a leading apostrophe or imported as text. - Blank cells in the field. Blank cells in a numeric column can trigger the "Cannot group that selection" error on older workflows, so fill or remove them first.
- Error values such as
#N/Aor#DIV/0!. - Mixed content, where most cells are numbers and a few hold words such as
n/a.
A quick test is to use a formula in a spare column of the source data:
=ISNUMBER(A2)Copy it down. Any FALSE marks a cell that blocks grouping. Fix those cells, then refresh the PivotTable and try again. The next chapters cover diagnosing this error in more detail.
Negative numbers and decimals
Numeric grouping handles negative values. Set Starting at to a negative number, such as -50, and By to 10. The ranges then run -50--41, -40--31, and so on. The double hyphen looks odd, but each label shows the lower and upper number separated by a hyphen.
Decimals work the same way. If the field holds values from 0 to 1, a width of 0.1 gives ten ranges. Labels such as 0-0.1, 0.1-0.2 follow the same rule of including the lower edge and stopping just before the upper one.
When the dialog is not enough
The Grouping dialog can only make equal-width ranges. It cannot make a first band of 0-18, a second of 18-65, and a third of 65 and over. For uneven buckets, build a helper column in the source data. That technique is covered in a later chapter.
For equal-width buckets, you can also create the same result in the source data with a formula. This formula rounds a positive number down to the nearest multiple of 10:
=FLOOR(B2,10)If B2 holds 37, the result is 30, which is the lower edge of the 30-39 band. Put the result in a new column and use that column in Rows. You get the same buckets as grouping by 10, with two differences:
- The labels show only the lower edge, such as
30, not30-39. - The buckets are stored in the data. They do not change if someone edits the PivotTable grouping, and they can be used in other tools.
The helper column costs extra space and must be extended when you add rows. The Grouping dialog needs no extra column. Use the dialog for quick analysis and the helper column when you need the buckets to persist or be shared.
Changing or removing the grouping
To change a numeric grouping, right-click a grouped item and choose Group again. The dialog reopens with your current values. Edit them and click OK.
To remove it, right-click a grouped item and choose Ungroup. The field returns to listing each distinct value.
Behavior after data changes
Grouping is stored with the PivotTable, not with the data. When you add rows to the source and refresh:
- New values that fall inside the Starting at and Ending at limits join the matching ranges.
- New values outside the limits appear in the
<or>rows. - The limits you typed stay as you set them. They do not stretch to include the new minimum or maximum.
If you left Starting at and Ending at at their automatic defaults and later add a lower or higher value, check the dialog again. Excel stores the numbers it showed when you grouped, so you may need to widen the limits yourself.
Common mistakes
| Mistake | Effect | Fix |
|---|---|---|
| Dragging the numeric field to Values and leaving Sum | Totals instead of counts | Change the summary to Count, or count an ID field. |
| Choosing limits that exclude data | Extra `<` or `>` rows | Widen Starting at and Ending at. |
| Reading `20-29` as including 29.9 incorrectly | Wrong interpretation of decimals | Remember the range runs up to just under 30. |
| Grouping a column with text entries | "Cannot group that selection" | Convert or remove the text, then refresh. |
| Expecting uneven bands | Dialog only gives equal width | Use a helper column. |
Exercise
A PivotTable shows Score in Rows with values from 3 to 98. You group it with Starting at 0, Ending at 100, and By 25. How many ranges appear, and in which range does a score of exactly 50 fall?
Show answer
Four ranges appear: 0-24, 25-49, 50-74, and 75-100. A score of 50 falls in 50-74, because each range includes its lower edge and stops just below the next one.
Exercise
You group Price by 10, and the table shows a row labeled >200. What does it mean, and how do you remove it?
Show answer
It holds records priced above the Ending at value of 200. Reopen the Group dialog with a right-click, raise Ending at so it covers the largest price, and click OK.
Exercise
You want a count of orders per Quantity band of 5, but the table shows large sums instead of counts. What did you do wrong?
Show answer
You put the Quantity field itself in Values, where it defaults to Sum. Change the summary to Count in Value Field Settings, or put a field such as Order ID in Values and summarize it with Count.