How to Change Date Formatting for Grouped Pivot Table Fields
Bottom line: Learn how to change the date formatting for a grouped field in a pivot table.
Skill level: Intermediate
Changing the Days Field Number Formatting Doesn’t Work
When we group a Date field in a pivot table using the Group feature, the number formatting for the Day field is fixed. It has the following format “Day-Month” or “d-mmm”.
If we try to change the number format of the Day/Date field it does not work. Nothing changes when we go to Field Settings > Number Format, and change the number format to a custom or date format.
The number formatting does not work because the pivot item is actually text, NOT a date.
When we group the fields, the group feature creates a Days item for each day of a single year. It keeps the month name in the Day field names, and this is actually a grouping of day numbers (1-31) for each month.
We can actually see this list of text items in the pivotCacheDefinition.xml file. To see that you can change the file extension of the Excel file to .zip, and navigate to the PivotCache folder.
Since these are text items that represent the days of the year, we won’t be able to change the number formatting of the cells directly in Excel. However, there are a few workarounds.
Solution #1 – Don’t Use Date Groups
The first solution is to create fields (columns) in the source data range with the various groups for Year, Quarter, Month, Days, etc. I explain this in detail in my article on Grouping Dates in a Pivot Table VERSUS Grouping Dates in the Source Data.
Using your own fields from the source data for the different date groups will give you control over the number formatting of the field in the pivot table.
You can also create a Calendar Table with the groupings if you are using Power Pivot.
Automatic Date Field Grouping
If you are using Excel 2016 (Office 365) then the date field is automatically grouped when you add it to the pivot table.
To Ungroup the date field:
- Select a cell inside the pivot table in one of the date fields.
- Press the Ungroup button on the Analyze tab of the ribbon.
The automatic grouping is a default setting that can be changed. See my article on Grouping Dates in a Pivot Table VERSUS Grouping Dates in the Source Data to learn more.
Once the date field is Ungrouped you can change the number formatting of the field.
To change the number formatting on the ungrouped Date field:
- Right-click a cell in the date field of the pivot table.
- Choose Field Settings…
- Click the Number Format button.
- Change the Date formatting in the Format Cells window.
- Press OK and OK.
Again, this only works on fields that are NOT grouped. If you group the field again after changing the formatting, the formatting for the items in the Days field will change back to “1-Jan”.
Solution #2 – Change the Pivot Item Names with VBA
If you really want to use the Group Field feature, then we can use a macro to change the pivot item names. This will make it look like the date formatting has changed, but we are actually changing the text in each pivot item name.
The following macro will loop through all the pivot items of the grouped Days field, and change the number formatting to a custom format. By default I set it to “m/d”, but you can change this to any date format for the month and day. Just remember that the item is NOT going to contain the year, since the item is not an actual date.
Download the File
Download the Excel file that contains the macro.
The Change Days Field Formatting Macro
Sub Change_Days_Field_Formatting() 'Change the number formatting of the Days field 'for Grouped pivot table date field. 'Source: https://www.excelcampus.com/pivot-tables/grouped-date-field-formatting/ Dim pt As PivotTable Dim pi As PivotItem 'IMPORTANT: Change the following to the name of the 'grouped Days field. This is usually Days or the name 'of your date field. Const sDaysField As String = "Days" 'Set reference to the first pivot table on the sheet 'This can be changed to reference a pivot table name 'Set pt = ActiveSheet.PivotTables("PivotTable1") Set pt = ActiveSheet.PivotTables(1) 'Set the names back to their default source name For Each pi In pt.PivotFields(sDaysField).PivotItems 'Bypasses the first and last items "<1/1/2015"... If Left(pi.Name, 1) <> "<" And Left(pi.Name, 1) <> ">" Then pi.Name = pi.SourceName End If Next pi 'Set the names to a custom number format For Each pi In pt.PivotFields(sDaysField).PivotItems If Left(pi.Name, 1) <> "<" And Left(pi.Name, 1) <> ">" Then 'Change the "m/d" format below to a custom number format. 'Year 2020 is used for leap year. pi.Name = Format(DateValue(pi.SourceName & "-2020"), "m/d") End If Next pi End Sub
How the Macro Works
The macro first loops the pivot items in the Days field to restore the pivot item name to it’s default source name. Two different pivot items cannot have the same name. So this should prevent any errors when changing or trying different formats.
The second loop changes each pivot item to the new format. It uses the DateValue function to change the pivot item name “1-Jan” to a date. It then uses the Format function to change the formatting of the date to text. By default it uses the “m/d” format. This can be changed to another format with the month and day. Each item must be unique, so you will want to use both the month and day in the item name.
You could probably separate this macro into two macros, and only run the first reset loop as needed. The macro takes about 15 seconds to run on my computer because of all the looping. But it’s not one you will have to run often.
It’s also important to mention that this is running on the pivot items, not the pivot cache. So the newly named items will only be changed on the pivot table you run the macro on.
I hope you find that helpful. I would suggest going with Solution #1 unless you really want to use the Group feature.
Please leave a comment below with any questions or suggestions on how we can improve this. Thanks so much! 🙂