Bottom line: Learn how to change the date formatting for a grouped field in a pivot table.

Skill level: Intermediate

Pivot Table Grouped Day Field Number Formatting

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.

Why?

Changing Number Formatting of Grouped Field Day Does Not Work

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.

pivotCache XML for Days Group

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.

Pivot Table Date Groups vs Source Data Date Groups in Excel

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:

  1. Select a cell inside the pivot table in one of the date fields.
  2. Press the Ungroup button on the Analyze tab of the ribbon.

Ungroup the Date Field to Change the Number Formatting

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.

Custom Date Formatting on Ungrouped Pivot Field

To change the number formatting on the ungrouped Date field:

  1. Right-click a cell in the date field of the pivot table.
  2. Choose Field Settings…
  3. Click the Number Format button.
  4. Change the Date formatting in the Format Cells window.
  5. 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.

Change Number Formatting of Days Field in Pivot Table with a Macro VBA

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.

Pivot Table Date Field Group Number Formatting Macro.xlsm (54.2 KB)

 

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.

Change Grouped Items Number Formatting Macro

Natilia asked a great question in the comments below about changing the number formatting for grouped numbers.  This is the same issue as the date groups.  The group names are text, not numbers.  However, we can use a macro to change these as well.

Macro to Change Pivot Group Number Formatting

Here is the macro code.  You will just need to change the the value of the sGroupField constant at the top to the name of your grouped field. You can also change the number format in the sNumberFormat if needed.

Sub Change_Grouped_Field_Number_Formatting()
'Change the number formatting of the grouped number field
'Source: https://www.excelcampus.com/pivot-tables/grouped-date-field-formatting/

Dim pt As PivotTable
Dim pi As PivotItem
Dim sGroup() As String
Dim sGroupName As String


'IMPORTANT: Change the following to the name of the
'grouped number field in the rows or columns area.

Const sGroupedField As String = "Quantity"
Const sNumberFormat As String = "$#,###"

  '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(sGroupedField).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(sGroupedField).PivotItems
  
    If Left(pi.Name, 1) = "<" Or Left(pi.Name, 1) = ">" Then
      sGroupName = "<" & Format(Mid(pi.Name, 2, Len(pi.Name)), sNumberFormat)
    Else
      sGroup = Split(pi.Name, "-")
      sGroupName = Format(sGroup(0), sNumberFormat) & " - " & Format(sGroup(1), sNumberFormat)
    End If
    
    If sGroupName <> "" Then pi.Name = sGroupName
      
  Next pi

End Sub

Final Verdict

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! 🙂

24 comments

Your email address will not be published. Required fields are marked *

  • One thing I discovered today is that Excel does not appear to Support Date Groups in Pivot tables if you have used the Date Model option (normally if you want to add Distinct Counts on items in the data you are pivoting).. so you can either have distinct count with Data Model OR Grouped Dates…but NOT BOTH

  • Hi Jon
    I don’t seem to get the Number Format option in the bottom left of the Field Formatting dialog; I’m sourcing my data from a tabular cube so not sure if this makes a difference.

    My dates look like this: ‘4/5/2018 11:20:00 AM’ and are displayed this way, but I just want to display the date as dd/mm/yyyy (Australian format).

    I’m wondering if I’ll have to have the format specified in the data cube itself.

    Any further thoughts or suggestions gratefully accepted.
    Cheers, Paul

    • UPDATE
      After writing my question, I revisited my data & cube – the field was set to be a smalldatetime field in my SQL table which set it as text for some reason in my cube.
      After changing the SQL type to datetime, my cube then recognised it as a date and the pivot reflected the format as required.
      (side note: the Number Format option in Field Settings still did not appear).
      cheers Paul

  • Great explanations – especially the one using the macro – exactly what I needed to pretty up financial values that were bands of $10m upwards – hard to read without the commas so the macro worked a treat!!!

  • Found this post and I hope you are still monitoring the original question

    I have Excel for Mac – these solutions work on PC however when I move to my Mac – it painfully does not….

    Anyone have any experience with the silly bratty date grouping issue for Pivot tables on Mac.

    The leading zeros are in the raw data and when create the pivot it loses them and moves all of my single digit dates throughout the grouped table. I have a ton of work to do every time I refresh weekly with my years worth of data….

    Any thoughts are welcome! You have come the closest to any other input we have found on google.

    Thanks in advance!

  • Here is a super easy solution:

    – On the original data change the date field formatting to number. This will give the Excel number for that day.
    – Create your pivot table and add the date as a field.
    – Format the date field as a date. Excel will only see the info as a number and format it as a date.

    No macros, reorganizing, or ungrouping needed.

    Hope this works for you.

    • Hi Harry,
      Very interesting solution. Thanks for sharing. I’m not sure how it solves the issue with date groups though. You won’t be able to group the field by time period (year, month, day, etc.) when the source field is a number. Therefore, you would have to create date groups with calculated columns, which is solution #1 in the article.

      The other drawback is that the number format will be displayed in the filter drop-down menu and any slicers for the field.

      Maybe I don’t fully understand your solution though. Thanks again!

      • I created a column that was =INT(date field). This truncated the date/time to only the date. Then used this calculated field in the pivot table. The pivot table allowed me to format this number as a Date. This grouped my data by days (which is what I wanted).

        Thanks for all the other tips.

  • PLEASE YOU SOLVED THAT, WILL EDIT THE PIVOT TABLE, HOW TO CHANGE MAIN SHEET/DATA SHEET,
    IT IS POSSIBLE. PLZ REPLAY ME.

  • Hello, thank you for this post. However, when I am adding same months in my database for different categories, it is still appearing as different lines in my pivot table. What is the way out?

  • Hello Jon, can I format groups and how in Pivot Tables? I created groups and need to add number format “accounting”, so i.e. range 0-10000 will display $0-$10,000. I went to Field Settings to change number format, but my selection is not working. Thank you, Natalia

    • Hi Natalia,
      Great question! Unfortunately it’s the same issue as the date formatting. The number groups are text. You can manually change each cell and type in the $ and commas.

      We can also use a macro to save time with this process. I modified the macro above to work on number groups. In the macro below you will just need to change the value of the sGroupField constant at the top to the name of your grouped field. You can also change the number format if needed.

      Sub Change_Grouped_Field_Number_Formatting()
      'Change the number formatting of the grouped number field
      'Source: https://www.excelcampus.com/pivot-tables/grouped-date-field-formatting/

      Dim pt As PivotTable
      Dim pi As PivotItem
      Dim sGroup() As String
      Dim sGroupName As String


      'IMPORTANT: Change the following to the name of the
      'grouped number field in the rows or columns area.

      Const sGroupedField As String = "Quantity"
      Const sNumberFormat As String = "$#,###"

        '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(sGroupedField).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(sGroupedField).PivotItems
        
          If Left(pi.Name, 1) = "< " Or Left(pi.Name, 1) = ">" Then
            sGroupName = "< " & Format(Mid(pi.Name, 2, Len(pi.Name)), sNumberFormat)
          Else
            sGroup = Split(pi.Name, "-")
            sGroupName = Format(sGroup(0), sNumberFormat) & " - " & Format(sGroup(1), sNumberFormat)
          End If
          
          If sGroupName <> "" Then pi.Name = sGroupName
            
        Next pi

      End Sub

      Try it out and let me know if you have any questions. Thanks!

  • Just FYI- if you have office 365 Excel 2016 and it auto groups your dates, then ungrouping does not let you change format- there is still no number format button. It also won’t sort by date correctly no matter what you do. Idiot proofing gone wrong is so very frustrating! Version 1708 Build 8431.2153.

    • It works on my O365 1809 and 1802 versions (September 2018 and February 2018).

      You can ungroup and immediately upon ungrouping, the date field reverts to the cell format already applied to it. I can also change the cell formatting.

      You’ve lost the previously calculated fields (year/month/day) etc.

  • Hi Jon,
    Thank you for the comprehensive post. I have a question about graphs as well. When I make a bar-line graph where I have two series ( each has a bar and line graph) so I have two lines and two bars. I need the data points of each line to be aligned with the bar of the same series ( it is always aligned in the middle between the two bars. Please tell me how to make such alignment.
    Thank you in advance.
    Noha

  • This is definitely a data pet peeve of mine. I tend to go the first option of adding fields to the source data after going crazy trying to reformat the group dates. At least I have better control of that. Thanks for the options, I’ll explore those when I have data and time.

  • John, could you send the XL file that goes with this article? That way I can try out your suggestions and follow-up if there are issues or questions. Thanks. Mark

Search
Generic filters
Exact matches only
Filter by Custom Post Type

JOIN US & LEARN EXCEL

Learn 10 great Excel techniques that will wow your boss and make your co-workers say, "how did you do that??"
Plus weekly updates to help you learn Excel.

Download the eBook

About Me

Jon Acampora Profile

Hello and welcome! My name is Jon Acampora and I'm here to help you learn Excel.

This blog is updated frequently with Excel and VBA tutorials & tools to help improve your Excel skills and save time with your everyday tasks. Subscribe above to stay updated. More about me...

MVP_Horizontal_BlueOnly