Grouping Dates in Pivot Tables
Do you know you can group dates in pivot tables to show the report by week, month or quarter? I have learned this trick while doing analysis on a pivot table today. In this online lesson on pivot tables, I will teach you how to group dates in pivot tables to analyze the data by month, week, quarter or hour of day.
Let us say you have a sales transaction database like this:

To show “Total units sold by salesperson in each month” in pivot table
- First make a pivot table [video tutorial] with Sales person as column header, Transaction date as row header and “total units” as pivot values.
- Now select any transaction date, right click and select “Group”
- In the group dialog box, select “Months” for group by option. Press ok.
- Presto! your data is grouped by month. No more than 3 seconds and you are ready analyze the data by month and find trends.
I have made a small screen-cast to show how this can be done. See it here:

Special cases for grouping dates in pivot tables
While the above example is simple, there are various things you can do when you are grouping data in pivot reports. Here are some special cases and how to get the grouping in pivot tables.
Group by Quarter & Month:

To group the pivot tables by Quarter and Month,
- Select “group” option.
- Select both “month” and “quarter” in the “group by” option, Click ok.
Group by Week:

To group the pivot table dates by week,
- Select “group” option
- Select “day” in the “group by” option. When you do this, the “number of days” box will be enabled. Enter the number of days as “7″ to group your pivot report by week.
Group by Hour of the day:

Assuming your data has dates along with the actual transaction time, you might want to analyze the sales by hour of day, to find out say “if one product is selling more than other during certain hours”.
To group the pivot table dates by hour of the day:
- Select “group” option
- Select “hour” in the “group by” option.
Collapsing & Expanding Pivot Table Groups:
When you group pivot reports by more than one “group by” you will see a little icon with + or – sign to expand or collapse the groups. Using this feature, you can easily deep dive in to a particular group to do further analysis. For eg. you can collapse all quarters and just expand Q2 to understand why the sales went up.
Another useful feature of these collapse / expand buttons in pivot tables is that, when you make a pivot chart, the collapsed groups are collapsed in the pivot chart too. And it is dynamic, ie, if you expand a group in the pivot table, the chart gets updated and shows more details.

Tell us how you use the group / un-group feature in pivot tables?
I am finding numerous possibilities with the group / un-group feature of pivot tables. I will learn new things and share them with you as we go along. Meanwhile, share your tips, experience and ideas using comments. I would love to learn from you.
Related Articles on Pivot Tables and Dates:
I suggest reading beginners guide to excel pivot tables, working with dates & times in excel.
Comments
RSS feed for comments on this post. TrackBack URI
Leave a comment
If you have a question, please ask in the forums


At Pointy Haired Dilbert, I have one goal, "to make you awesome in excel and charting". PHD is started in 2007 and today has 300+ articles and tutorials on using excel, making better charts. 




Great post as usual. This feature works well in Excel 2007. I largely avoid using it in 2003 because it will only let you group in this way if all entries in that field are a date – and in particular that means no blanks. This stops you selecting whole columns for the data source.
I work around this by using the year(), month() and day() functions to give new columns of data that strip out the year month and day from the date and then use these columns as page, row or column fields.
If you are still baffled by all of this pivot table talk, take a look at my free Pivot Table Training Video at (2003 and 2007):
http://www.pivot-tables.biz/FreeVideo.htm
Chandoo:
this is exactly the reason why I suggested to change the layout on the data table for the Touch challenge.
In the way you upload it, you are not able to work with the dates, so you can group it as you mention on this post.
Also, I’ve found that sometimes, if you have blanks or non-date data in a field (even though it looks like data), you are not able to use this feature. to do so, the autofilter on that field comes handy to rapidly detect those non-date inputs. Another trick is to select the entire field and clear its format, so you can easily find those bugs.
Regards,
Martín.
Now this is a trick I didn’t know about. Well done! Not quite as slick as a tool like Tableau, but pretty darn useful.
I get fouled up sometimes by extending the range to include a blank past the last row of data.
Martin/Dale
As far as I can see the problem of grouping dates with blanks no longer exists in 2007. I have just tried it when I read this post.
This problem stopped me using date groupings in 2003 (I have been using year(), month(), etc. to strip the data out of the date instead).
I’ve used that trick in the past, but the blank row thing is a bummer.
I like to be able to import new data and just refresh the pivot table. Even filtering out blank rows doesn’t allow grouping. I know I could write a macro to create the pivot table every time, but it’s a hassle.
Another option is to add a column and use the MONTH, WEEKDAY or HOUR formulas, if that is how you plan to group the data.
Sorry–just read Glen’s post. I’m still using 2003. If it allows you to group with blank rows in 2007, that might be enough to get me to upgrade.
This is a great post. I recently started using Pivot table for data analysis and is working well. My frustration is with some of the easy stuff – say, i have a list of fruits
apple – 1
pear – 1
apple – 1
How many instances of apple, pear do I have. Maybe it is my limited knowledge of excel (which is getting better thanks to this blog) but I don’t see an easy way out.
Any help will be much appreciated.
@Martin… I understand your frustration, but the dates were already grouped at month level in the challenge.
@Glen.. very good inputs, thank you
@Dan… I agree tableau has much more powerful features. I am waitng to see how much MS achieves in the upcoming powerpivots
@Sundeep: Make a pivot with the data, use fruit as row header, as well as values. Excel will show summary by count.
Hi Chandoo,
May I say first thanks, this is a great website and a great resource.
I’ve recently had to group by Year/Qtr using 2003 and refered to this at Datapig http://www.datapigtechnologies.com/flashfiles/pivot5.html
But now I’ve got 2007 I’m glad you did this new post here
One frustration with Excel’s date grouping is that Quarters are for calendar years only. In my world Quarter 1 starts in July.
Here’s a bit of code to add to the ThisWorkbook module. This works by changing the captions of quarterly groups from ‘Qtr 1′ to ‘Q3′. It runs after the pivot table update event, because updating the pivot table will change quarter captions back to the defaults.
‘=================================================
Option Explicit
‘ IsUpdated is a global variable that lets us know
‘ if the Pivot Table Update event has been triggered by the user
‘ or by our code in this module
Private IsUpdated As Boolean
Private Sub Workbook_SheetPivotTableUpdate(ByVal Sh As Object, ByVal Target
As PivotTable)
If Not IsUpdated Then
If SetFiscalQtr(Target) Then
Application.StatusBar = “Fiscal Year Quarters Set”
IsUpdated = False
End If
End If
End Sub
Private Function SetFiscalQtr(pt As PivotTable) As Boolean
‘ Function to set Quarters From Excel default to fiscal year
‘ i.e. Q1 = Jul, Aug, Sep etc.
If FieldExists(pt, “Quarters”) Then
IsUpdated = True
With pt.PivotFields(”Quarters”)
‘ could check to see if we have already
‘ renamed the quarters, or just error and keep
‘ going
On Error Resume Next
.PivotItems(”Qtr3″).Caption = “Q1″
.PivotItems(”Qtr4″).Caption = “Q2″
.PivotItems(”Qtr1″).Caption = “Q3″
.PivotItems(”Qtr2″).Caption = “Q4″
.AutoSort xlAscending, “Quarters”
‘ turn error checking back on
On Error GoTo 0
End With
SetFiscalQtr = True
Else
SetFiscalQtr = False
End If
End Function
Private Function FieldExists(pt As PivotTable, strField As String) As
Boolean
‘ checks if strField is a Pivot Field in the Pivot Table pt
‘ true if field exists
Dim fld As PivotField
FieldExists = False
For Each fld In pt.PivotFields
If fld.Name = strField Then
FieldExists = True
Exit Function
End If
Next
End Function
Cheers,
Ed
Is there a way to group on non-date values? For example, I have expenses from various companies that I need to compare. I have normalized the data, and then I pivot – so that the categories and line items are the rows, and I have a column for each company. However, it’s a terrible waste of real-estate – the categories take up too much room. It would be great if they could be grouped….
Suggestions?
hi chandoo,
this post is just what i need to feedback a bug/problem in PivotTable (Excel 2007).
I’ve created a new sheet for pivottable with date grouped into month/year.
BUT, i needed to create a new pivottable sheet (from the same source table) with NO date grouping. However, this new pivottable will not show the full, ungrouped dates. It stumbled me for some time until i figured out that the 1st pivottable has date grouping!
is there a solution to this?
thanks!
@Ed.. this is exactly why I love to blog. Everyday I meet great hacks and excel users like you. Thanks for sharing the macro with us.
@Marion: You can group non-dates by selecting the values you want to group and selecting “group” option. Excel will create a new group with the selected values. It is a bit painful but better than mentally grouping the values.
@Lohhw3: This seems like a bug (or feature) in pivot tables. Once you make a group in one pivot, the group persists in other pivots. A simpler solution could be to make a duplicate data table and use it in pivot.
thanks chandoo!
am glad it’s not a weird problem that i’m facing
Hi. I am trying to chart expenses per month that range from Sept 08 to October 09. When I group by month in the pivot table it only gives 12 months (Jan- Dec) and groups the Sept 08 and Sept 09 data together. Is there a way to group by month (Sept-08 through Oct-09) that will allow me to use greater than 12 months?
@Pam.. you can select both Month and Year in group criteria to have this corrected.
How do you do this with a fiscal year, say from December to November? It seems to only allow for calendar years.
I need it for 3 years, not just one single year.