Sub topic 2
Why dates and numbers can be grouped
This chapter assumes you can build a PivotTable from a table or contiguous range with one header row. It also assumes you can place fields in Rows, Columns, and Values, and that you know the Sum and Count summaries. It covers why Excel offers grouping for some fields and not others, and how to check your source data before you open the Grouping dialog. The dialog itself is covered in the next chapters.
The core idea: grouping is arithmetic
A PivotTable groups items by turning each value into a bucket label. For text, Excel cannot calculate anything, so it asks you to pick the items yourself. For numbers it can calculate the bucket. It divides the value range into steps and assigns each value to a step.
Dates can be grouped by calculation because Excel stores every date as a number. The date you see in a cell is formatting applied to that number. Because the stored value is numeric, Excel can compute which month, quarter, or year it falls in, or which seven-day period it belongs to.
So the question "can this field be grouped?" comes down to one test: does every cell in the field hold a real number or a real date?
How Excel stores dates
Excel counts days. In the default 1900 date system, the number 1 is 1 January 1900, 2 is 2 January 1900, and so on. The number 45000 is 15 March 2023.
Time of day is the fractional part of the number:
- 45000 is midnight at the start of 15 March 2023.
- 45000.5 is noon on that day.
- 45000.75 is 6:00 PM on that day.
To see the stored number behind a date, select the cell and change its number format to General. A date such as 15-Mar-2023 changes to 45000. Changing the format back restores the display. The value never changed.
What the PivotTable sees
When you build a PivotTable, Excel copies the source data into a hidden store called the pivot cache. Each field in the cache has a type that Excel infers from its contents. The Group command is available for a field whose items are all dates or numbers. The dialog that opens depends on that type:
| Field contents | Dialog you get | What Excel calculates |
|---|---|---|
| All real dates | Grouping with a list of units (Seconds through Years) | Which unit period each date falls in |
| All numbers | Grouping with Starting at, Ending at, By | Which equal-width range each number falls in |
| Text, or a mix | No automatic grouping. You select items manually | Nothing. You choose the items |
A single cell that breaks the pattern changes the result for the whole field. The next sections cover the three kinds of cell that cause it.
Real dates versus text that looks like dates
Dates imported from CSV files, exported from other systems, or pasted from web pages often arrive as text. They look the same, but they are strings of characters, and Excel cannot do arithmetic on them.
The ISNUMBER function tests this directly. A real date returns TRUE. Text that looks like a date returns FALSE.
=ISNUMBER(A2)Fill this formula down a spare column next to your data. Any FALSE in a column that should hold dates marks a cell that blocks grouping.
There are visual hints too, though none is reliable on its own:
- Text is left-aligned by default, and numbers and dates are right-aligned. A column can be formatted to override this.
- A small green triangle in the corner of a cell can mean "number stored as text".
- The Number Format box on the Home tab shows Text for cells formatted as text. Cells formatted as Text can still contain real numbers entered earlier, and General cells can contain text, so the format alone proves nothing.
ISNUMBER gives the actual answer because it tests the stored value, not its appearance.
Counting the problem cells
To see how many cells in a column are not numeric, compare COUNTA, which counts all non-empty cells, with COUNT, which counts only numbers and dates:
=COUNTA(A2:A500)-COUNT(A2:A500)If the result is 0, every non-empty cell is numeric. A positive result is the number of text cells or error values. Blank cells are not counted by either function, so they need a separate check:
=COUNTBLANK(A2:A500)Converting text dates
The right fix depends on how the text is formatted. Three common methods:
- Select the column, then use Data > Text to Columns, choose Delimited, click Next twice, and set the column data format to Date with the order that matches your text (for example MDY or DMY). This converts the cells in place.
- In a helper column, use
=DATEVALUE(A2)for text such as "2023-03-15". The result is a serial number, so format that column as a date. The text must be in a format your regional settings recognize. - In a helper column, use
=VALUE(A2), or=A2*1, to coerce a text number into a real number.
Whichever method you use, run ISNUMBER on the result before rebuilding or refreshing the PivotTable.
The three cell types that break a numeric field
A field is only treated as a date or number field when all its cells are numeric. These cells make it fail:
- Text in the column. This includes numbers stored as text, such as "1,250" imported with a leading apostrophe or a trailing space, and notes like "n/a" or "TBD".
- Blank cells. Empty cells in the field can prevent grouping or can force a blank item into the results, depending on the Excel version and how the PivotTable was created. Fill them with a value, or remove the rows from the source.
- Error values. A cell containing
#N/A,#VALUE!, or any other error stops the field from being treated as numeric.
This is why the same Group command works on one PivotTable and shows "Cannot group that selection" on another. The message does not mean grouping is unavailable for that kind of data. It means that at least one cell in the field is not a number. A later chapter covers the full diagnosis and repair process.
The decision Excel makes
The diagram shows the check that decides which grouping behavior you get.
Dates and numbers are the same kind of data
Date grouping and number grouping share the same foundation. A date field is a numeric field whose values are interpreted as calendar dates.
This has practical consequences:
- Grouping by days uses the whole-number part of the serial. A value of 45000.75 and a value of 45000.25 are both on day 45000. In a date field with times, grouping by Days merges the times of day into one item per calendar day.
- Grouping by Hours, Minutes, or Seconds uses the fractional part. These units are useful only if the source cells contain times.
- Numeric grouping by 7, applied to a date field in a way that treats it as a number, produces seven-day buckets. The date dialog's Days unit, with Number of days set to 7, does the same thing and displays the buckets as dates. A later chapter covers the weekly case.
- Rounding down is how bucketing works for numbers. Excel computes the start of each range, and a value belongs to the range whose start is the largest step at or below it.
You can reproduce numeric bucketing in the source data. This formula rounds a positive number down to the nearest multiple of 10, which gives the same buckets as grouping by 10 in the PivotTable:
=FLOOR(B2,10)A value of 37 becomes 30, and 40 stays 40. If you build the same buckets with a helper column, you can check the PivotTable's results against your own calculation. A later chapter uses this idea for fiscal years and custom buckets.
A worked check before grouping
Suppose a sheet has order dates in column A and order amounts in column B, in rows 2 to 500. You are about to group dates by month and amounts by 100.
- In a spare column, enter
=ISNUMBER(A2)and fill it down. Filter the column for FALSE. If any rows appear, convert those dates and recheck. - Run
=COUNTBLANK(A2:A500)and the same formula for column B. If either result is above 0, fill the gaps or delete the rows. - Run
=COUNTA(B2:B500)-COUNT(B2:B500). If it is above 0, find the text amounts and convert them. - Refresh the PivotTable with Data > Refresh All, so the pivot cache picks up the corrected source.
- Right-click a date in the Rows area. If Group appears and works, the field is now a valid date field.
Step 4 matters. A PivotTable does not re-read the source data until you refresh it. If you fix the source and skip the refresh, the PivotTable still holds the old values, and the old error can persist.
Common mistakes
- Trusting the display. A cell showing 15-Mar-2023 may be text. Always test with
ISNUMBER. - Fixing the source without refreshing. The pivot cache keeps its stored data until you refresh.
- Leaving a stray note in the column. A single "TBD" in 10,000 amounts blocks numeric grouping for the whole field.
- Assuming blank means harmless. Blank cells in a field to be grouped are a frequent cause of the error, so clear them first.
- Converting dates with the wrong regional order. The conversion succeeds with wrong results, which is harder to notice than an error.
Summary
Excel can group dates and numbers because both are stored as numbers, so it can calculate the bucket for each value. It cannot calculate buckets for text, so text needs manual grouping. For grouping to work, every cell in the field must be a real number or date. Blanks, text, and errors prevent it. Test with ISNUMBER, COUNT, COUNTA, and COUNTBLANK, fix the source, and refresh the PivotTable.
Exercise
A column of order dates shows 03-Mar-2024 in every cell, left-aligned. =ISNUMBER(A2) returns FALSE for every row. What does this tell you, and what is the likely result if you try to group this field in a PivotTable?
Show answer
The cells hold text that only looks like dates, so Excel cannot do arithmetic on them. The PivotTable treats the field as text, so the date grouping dialog is not available and you get "Cannot group that selection". Convert the cells to real dates (for example with Text to Columns or =DATEVALUE(A2) in a helper column), confirm ISNUMBER returns TRUE, then refresh the PivotTable.
Exercise
Column B has 2,000 order amounts. =COUNTA(B2:B2001) returns 2000 and =COUNT(B2:B2001) returns 1997. What do the results mean, and how do you find the affected cells?
Show answer
Three non-empty cells are not numbers. They are text or error values, such as numbers stored as text or entries like "n/a". Those three cells block numeric grouping for the whole field. To find them, fill =ISNUMBER(B2) down a spare column and filter for FALSE. Fix or remove those cells, refresh the PivotTable, and group again.
Exercise
A cell contains 45000.75. In which day does Excel place this value when you group a date field by Days, and what does the .75 represent?
Show answer
Excel places it on day 45000, which is 15 March 2023, because grouping by Days uses the whole-number part. The .75 is the time of day, 18:00 (6:00 PM), stored as three quarters of a day. It does not change which day the value is grouped into.