And I love pivot tables.
But what I don't love is that !%$!@#$!$@#!@#$ "Can't group by that selection. (nyah nyah nyah!)" dialogue box.
It most often pops up when I want to group by a date range. You know - aggregate by months or days or somesuch.
The good thing is that there is a way around it.
The bad thing is that I do it so infrequently that I'm constantly forgetting how. And, for some reason, 99.99% of the google results are poorly formatted tutorials that have so many ads intersperced, ya can't read the !$#!$#!@#$ advice.
Today I got sick of it. So now, for Your Reading Pleasure (and my forever future reference) I present:
"The Almost Totally (in)Complete Guide to Getting Past the Can't Group By Pivot table error dialogue in Excel 2003"
"At least for grouping dates."
Step 1:
Make sure you ain't selecting blank lines.
I know, I know. If you've "Imported" your excel data, it's soooo much easier to select the columns when you're creating your pivot table.
Why? Because when you create the pivot table you won't be able to group by ANY FIELD!!
That's right. For some reason yet unknown to man (but probably obvious to those smarties who regularly develop in multi-dimensional database technology) if a field has blank rows, Excel's pivot table engine barfs.
So...how do we make sure we're not getting blanks?
We can do this one of two ways, either select the explicit range of cells (ie, A1:F23135) OR use a named range.
If this is a one-off, I'd use the range of cells. If this is a table that's going to be updated semi-regularly, I'd suggest using a named range as the source for the pivot table.
The super-niftyness of a named range is simply, you can use a formula to determine how big you want it to be. (ok, ok, I think technically it's a "dynamic named range"...)
To wit, assuming you're a normal human being and your data (or, more properly, it's header) starts at cell A1 of a given sheet, here's how you create a named range that only allows non-blank rows:
1) From the Insert menu, choose Name=>Define
2) You'll get something like this. Well, kinda like this. (you won't have any ranges named yet)
3) In the Names in workbook: section, name your range.
(Get it? You give your range a name so now it's a named ra...nevermind.)
Ok, got the name? Good. Now, in the "refers to:" section, type the following:
*********CautionCautionCautionCaution*********
Weird stuff happens in this particular dialogue box.
Arrows don't work...quite...right.
Yes, arrows. You know, those plebeian controls you use to back up when you type "shetname" and correct it
If you use them, whatever the active cell is on the worksheet, it's address gets put in the box.
So use the mouse and the delete keys...but watch out for them arrows.
You have been warned.
*********/endCaution*********
(resuming where we left off...)
in the "refers to:" section, type the following:
=OFFSET(sheetname!$A$1,0,0,COUNTA(sheetname!$A$A),14)
- sheetname = the name of the worksheet your data is on
- $A$1 = the first cell in your range (think, top left corner)
- 0,0 = the horizontal and vertical offset you want from this cell.
- (So if you put in 5, 8...we'd actually start the range 5 cells to the right and 8 cells below A1. Funky. I know.) No human has ever actually had a use for this bit, but if I hear of anything I'll let you know.
- COUNTA(sheetname!$A$A) = This could be a static number (it actually refers to the number of rows we want) but we're using a formula. Counta returns a count of the nonblank rows in a range...so this essentially says "look at column A and give me a count of how many rows are non-blank for this cell"
- The more astute of you have caught the assumption...what if a few rows don't have a value for a? Will they be included? Maybe. Remember...this returns a "count", so if you have 10 rows and 2 of them don't have a value in the A column, you'll only get the first 8 rows. Bummer. So best tag a column that's always populated.
- Where was I? Oh yeah. The last item 14. This is the number of columns you want included in the range. This can also be made dynamic, but in this case I know how many columns. (so there)
That's all there is to it.
Now, when you're defining your hypothetical pivot table, when they ask you to define the range, just type the name of the...err...named range you created above and "POOF". (literally - "your spreadsheet will be, like, smokin dude!")
What is that you say? What if your data contains rows that SOME are blank and some are not? How do you deal with grouping incomplete data?
Yes, it's possible. Maybe I'll write that one up tomorrow. Until then, cheerio!

14 comments:
Awesome!!!
But im getting invalid referrence when i type in the "Range Name"
When you type the "range name" into where?
Into the spot in the pivot table wizard where you specify the data range..?
Yes Aaron,
I have a spreadsheet that derives data from an Access 2003 query.
In the same spreadsheet i have a pivot table that is based on the derived data from Access.
I was able to define the name range and when i enter the same in pivot table data range, it says "reference is not valid"
Can you help me out please?
Regards,
KK
Hmm...
What is the line that you used to define the range?
The bit you typed in to the "refers to" section...in the example above, I used:
=OFFSET(sheetname!$A$1,0,0,COUNTA(sheetname!$A$A),14)
What is yours?
And what is the actual name you used for the range?
=OFFSET(Query Data!$A$1,0,0,COUNTA(Query Data!$A:$A),9)
It did not allow to enter $A$A inside counta(), so i used $A:$A
In the last two columns out of my total columns(9), i use a formula to manipulate on the queried data from Access 2003.
I used "Name" as a name for this dynamic range.
I tried different names also, it didnot work.
Thank you for ur time Aaron.
Please let me know if u need any other information.
Regards,
KK
KK:
I tried creating a demo spreadsheet for a sheet named "Data Range" and I got the same error.
While it _SHOULD_ catch this when entering the range in the "refers to" section, it doesn't seem to.
I surrounded "Data Name" in _single_ quotes in the "refers to" section and it worked.
Like so:
=OFFSET('Query Data'!$A$1,0,0,COUNTA('Query Data'!$A:$A),9)
Let me know if it worked..
It Worked!!!!
But its giving me new problem now.
I am not able to do "Refresh Data" in the spreadsheet so that it can get the latest data from the Access DB.
The "Refresh Data" icon is now disabled.
Can you help me to sort that?
Thank you so much for your assistance.
Regards,
KK
Are you doing a "refresh data" on the pivot table, or the source data?
I know that for my worksheets, I have to first refresh the source data tab and then manually go through and refresh each pivot table or it's related chart.
(there is a way to set all the pivot tables to refresh their info on startup, but I'm a control freak and want to KNOW when it's refreshed...)
OR...
Have you lost the ability to refresh your source data itself..?
If that's it, I can only suggest offering sacrifices at the almighty oracle-of-google...
Yes, I lost the "Refresh Data" funcationality for the source data!! :(
I exactly wanted to refresh the table based on the update from source data.
Now i got to start all the way again, i hope i get(in google search) someone like you to assist with this..
Thanks a zillion for ur help.
-KK
New pivot table question. Has anyone seen Formula1,2,3... columns magically pop up when trying to manually move columns/rows within a table?
Thanks - great help.
I had a problem, and I can't seem to get around it.
I created a named range called "Data" in "Book1.xls". I created a new workbook "Book2.xls", and created a pivottable in Book2. The Pivottable uses the data in the range Book1.xls!Data. The pivottable was created without a problem.
Next, I closed both files. Then I reopened Book1.xls. Then I opened Book2.xls. The pivottable in Book2.xls will no longer refresh and states that the reference to "Book1.xls!Data" is invalid.
Have you encointered this error, and is there a way to get the pivottable to recognize the named range in Book1.xls?
My refresh data functionality is gone as well. It has become a static range rather than dynamically created from microsoft query.
Post a Comment