Grouping the Items in a Report Filter Problem You added the OrderDate field to the Report Filter area, and a long list of dates is in the down list.. Figure 5-4.Error message about calcu
Trang 1Grouping and Totaling
Pivot Table Data
them into larger units, such as years, quarters, or months This adds flexibility to the pivot
table, and enables you to do things like comparing the results from one year to another, or
creating a running total by quarter
You can also group numeric fields, when you add them to the Row Labels or Column
Labels area of the pivot table By using this feature, you can group the numbers into larger
chunks, such as groups of 10 or 25 If comparing test scores, or results by age, you can look
at larger segments of the population, instead of individual scores or ages
It’s also possible to manually group text items in the Row Labels and Column Labels
areas, so you can group cities into a region, or employees into teams, and summarize the
data for the group
Most of the time, grouping goes smoothly, but you may hit snags, or see error messages
when you try to group the data in your pivot table This chapter addresses those problems,
and provides solutions or workarounds for the issues you might encounter
This chapter also covers problems that may arise when summarizing pivot table results
in subtotals and grand totals, and when summarizing fields in the Values area
Except where noted, the problems in this chapter are based on the Stores.xlsx sample
month, instead of seeing quantities for the individual dates
This seemed like a simple request, but you get an error message, “Cannot group that
selection” (see Figure 5-1), when you right-click a date cell, and then click Group This
prob-lem is based on the GroupError.xlsx sample workbook
101
Trang 2Figure 5-1.This is the error message you see when items in a field can’t be grouped.
Solution
This error occurs if there are cells that contain text, instead of dates, in the source data’sOrderDate field For example, you may have entered text, such as N/A, if a date was not avail-able when the data was being entered To correct the problem, you can do the following:
■ Caution A blank cell does not cause problems with grouping, but use the Delete key to remove the text.Don’t type a space character
lay-out, refresh the pivot table, and then add the date field to the pivot table again
■ Tip In the PivotTable Options dialog box, on the Data tab, if Number of Items to Retain Per Field is set toNone, you should be able to group the date field without following Step 3
5.2 Grouping: Error Message When Grouping Numbers
Problem
The logistics manager has asked for a report on the quantities per order You created a reportwith Quantity in the Row Labels area, Category in the Column Labels area, and OrderDate inthe Values area, as Count of OrderDate To simplify the report, you would like to show thequantities in groups of ten, such as 1–10, and 11–20, and show the number of orders in eachgrouping
However, you get an error message that says, “Cannot group that selection,” when you try
to group the numbers You checked the source data, and no text entries are in the Quantityfield This problem is based on the GroupErrorNum.xlsx sample workbook
Trang 3Sometimes, entries that look like numbers are not recognized as real numbers by Excel The
numbers may have been copied or imported from another program, and they are text values
instead of real numbers
A quick way to test if the numbers are being recognized is to select two or more cells that
contain numbers Then, look in the status bar, at the bottom of the Excel window, to see if the
numbers show a Sum (see Figure 5-2) If only the Count is displayed in the status bar, the
numbers are stored as text
Figure 5-2.The sum of selected cells shows in the status bar.
To correct the problem, you can do the following:
in the following “How It Works” section
numbers in the pivot table
table layout, refresh the pivot table, and then add the number field to the pivot table
again
■ Tip In the PivotTable Options dialog box, on the Data tab, if Number of Items to Retain Per Field is set to
None, you should be able to group the numbers without following Step 3
How It Works
You can convert text “numbers” to real numbers in several ways For example, you can use the
Paste Special command
Trang 43. On the Ribbon, click the Home tab, and in the Clipboard group, click the lower section
of the Paste command, and then click Paste Special (see Figure 5-3)
Figure 5-3.Paste Special command on the Ribbon
Another method for converting text numbers to real numbers is to use the Text to
Columns feature
5.3 Grouping the Items in a Report Filter
Problem
You added the OrderDate field to the Report Filter area, and a long list of dates is in the down list The sales manager frequently asks for a report for a specific month, so you wouldlike to group the dates by month, to make it easier to filter the report
drop-However, when you select the cell that contains the OrderDate drop-down list, the GroupField command on the Ribbon’s Options tab isn’t available This problem is based on the
Workaround
You can’t group the items in a field located in the Report Filter area Temporarily move theOrderDate field to the Row Labels or Column Labels area and group the dates, and then movethe OrderDate field back to the Report Filter area
Trang 55.4 Grouping: Error Message About Calculated Items
Problem
You’re trying to group the dates in the OrderDate field, and you’re getting an error message
that says, “You cannot add a calculated item to a grouped field” (see Figure 5-4) The
Order-Date field doesn’t have any calculated items
The only field with a calculated item is the Category field, and it’s not in the pivot table
layout This problem is based on the GroupErrorCalc.xlsx sample workbook
Figure 5-4.Error message about calculated items
Workaround
Although the OrderDate field has no calculated items, another field in the pivot table has a
calculated item, and that’s preventing the grouping Even though the other field is not visible
in the pivot table, it can still prevent the grouping
■ Tip When troubleshooting pivot table problems, it may help if you know what formulas are in the pivot
table To create a list of calculated items and calculated fields, select a cell in the pivot table, and on the
Ribbon’s Options tab, click Formulas, and then click List Formulas
You can use one of the following workarounds:
• Delete the calculated item in the Category field, and then group the dates in the
Order-Date field
• Add a new field in the source data, with a formula to do the calculation, instead of using
a calculated item in the pivot table Then, add the new field to the pivot table, and
delete the calculated item
• Add a new field with formulas in the source data to create date groups For example,
add a new column with the heading “YearMonth” and use the following formula to
show the year and month, for a date in cell A2:
=TEXT(A2,"yyyy-mm")
Then, refresh the pivot table, and add the YearMonth field to the Row Labels area,
instead of using the OrderDate field
Trang 65.5 Grouping Text Items
Problem
Three new stores opened last year and the sales manager asked you for a report that comparesquantities sold in the new stores to sales in the older stores You added the Store field to theRow Labels area of your pivot table, and you’d like to divide the stores into Old and New groups.The three lowest numbers are the older stores, and the three highest are the newer stores.However, when you select a label in the Store field, the Group Field command on theRibbon’s Options tab isn’t available, so you aren’t sure how to create the groups This problem
is based on the Stores.xlsx sample workbook
Solution
The Group Field command is only available for date and number fields in the Row Labels orColumn Labels area of the pivot table Because the store numbers are entered as text, andthat’s how you want them to remain, you can use the following method to group the stores
hold the Ctrl key, and then click the store numbers In the sample file, select stores
3000, 3036, and 3062
Figure 5-5.Group Selection command
This creates a group named Group1, with the selected stores listed under that heading(see Figure 5-6) For each of the remaining stores, a heading is created, with its storenumber
Figure 5-6.Group1 created in the Row Labels area
Name the second group as New.
Trang 7■ Tip To select the remaining stores, drag down through the list of store numbers in the Row Labels area.
You can include the store number headings that were created
As an alternative to grouping the stores in the pivot table, you could add a StoreType
field to the source data, and then enter Old or New for each record Add the StoreType field
as the first field in the Row Labels area and the stores will appear under the correct
Store-Type heading
5.6 Grouping Dates by Month
Problem
Your pivot table is based on two years of sales data When you group the order dates in your
pivot table by month, the January data from both years is lumped together You’d like to keep
the data from different years separated, so you can compare the monthly data between years
This problem is based on the GroupMonth.xlsx sample workbook
Solution
When you group the dates, select both Year and Month in the Group By list With this type of
grouping, data from each year is grouped, and within each year, the data for each month is
grouped
A Years field is automatically added to the PivotTable Field List, and you can move it to a
different area of the pivot table For example, you could move Years to the Report Filter area,
and leave the OrderDate field, showing Months, in the Row Labels area
5.7 Grouping Dates Using the Starting Date
Problem
You want to group the order dates into four-week periods that coincide with your sales
calen-dar You successfully group the dates, but the date ranges are a couple of days off, starting
midweek instead of on a Monday This problem is based on the GroupStart.xlsx sample
workbook
Solution
When you group the dates, select to group by days, and set the number of days to 28 As a
starting date, Excel automatically selects the first date in your source data, but you can enter a
different date, to get the starting date you need
For example, if the default starting date is Thursday, January 3, 2008, you could enter
December 31, 2007 as the starting date for your grouping (see Figure 5-7) This forces all the
groupings to start on a Monday, and it matches the company’s sales periods
Trang 8Figure 5-7.Changing the starting date
5.8 Grouping Dates by Fiscal Quarter
Problem
You added the OrderDate field to the Row Labels area of your pivot table, and you were able togroup the dates by year and quarter However, you want to group the dates by your company’sfiscal quarter, which starts in July, instead of using the calendar quarter available in theGrouping dialog box This problem is based on the GroupQtr.xlsx sample workbook
Workaround
There’s no built-in option to group by fiscal quarter In the source data, you can add a umn with a formula that calculates the fiscal quarter, and then add that field to the pivottable For example, if your fiscal year starts in July, use the following formula to calculate thefiscal quarter:
col-=CHOOSE(MONTH(A2),3,3,3,4,4,4,1,1,1,2,2,2)
In the Choose formula, the first argument, Month(A2), returns the month of the date in cell A2
It uses that number to select the appropriate fiscal quarter number from the numbers that low For example, if the month is April, the month number is 4 In the Choose formula, thefourth number is 4, so that is the fiscal quarter in which April falls
fol-In another column, you can also calculate the fiscal year For example, if fiscal year 2009starts in July 2008:
Trang 9In the Grouping dialog box, select Days, and set the Number of Days to seven (see Figure 5-8)
You can also change the starting date, so the first day is a Sunday or Monday, or whatever
weekday you would prefer for your grouping
Figure 5-8.Group dates by week in the Grouping dialog box.
This creates groups in the Row Labels area, with the dates formatted using the short date
setting defined in the Windows Control Panel, Regional and Language Options dialog box (see
Figure 5-9)
Figure 5-9.Row labels grouped by week
■ Note The date format may not be exactly the same as the short date format you defined in the Regional
and Language Options dialog box, but the year, month, and date should be in the same order, and the date
separator you specified should be used However, if the defined short date format includes a text month
name, that is replaced by numbers in the grouped dates
Trang 105.10 Grouping Dates by Months and Weeks
Problem
You wanted to show sales data by week, so you grouped the OrderDate field in seven-dayintervals This successfully grouped the days by week, and the date ranges were displayed inthe pivot table
Later, the sales manager asked you to add month to the date grouping, to summarize thedata by month, and by week When you selected Month in the Grouping dialog box, the daysbecame ungrouped, but you’d like to use months and weeks together when grouping Thisproblem is based on the GroupWeekNumxlsx sample workbook
Workaround
In the pivot table, there’s no way to group by both weeks and months You could create a umn in the source data, and then calculate one of the grouping levels there Then, you couldadd that field to the pivot table
col-For example, add a column in the source data, with the heading WeekNum, and use the
following formula to calculate the week number: =WEEKNUM(A2)
With the OrderDate field in the Row Labels area, group the dates by years and months
In the Row Labels area, add the WeekNum field below the OrderDate field, to summarize thedata by month and week number (see Figure 5-10)
■ Note If a week starts in one month and ends in another, then it appears under each month
Figure 5-10.Row labels grouped by month and week number
5.11 Grouping Dates in One Pivot Table Affects Another
Pivot Table
Problem
You have two pivot tables based on the same Excel Table In one pivot table, the order datesare grouped by month, and in the other, you’d like to group the dates by quarters However,when you change the grouping in one pivot table, the same grouping appears in the other
Trang 11pivot table You’d like to change one pivot table without affecting the other pivot table This
problem is based on the GroupTwoTables.xlsx sample workbook
Solution
Because the two pivot tables use the same pivot cache, the grouped items are the same in bothtables If you need different groups in the pivot tables, you can create a named range based on
the Excel Table, and use that name as the source for the second pivot table
For example, on Sheet1 is an Excel Table named Sales_Data Follow these steps to create a
new range named SalesDataNew
in the Sales_Data table, and offsets zero rows and zero columns from that cell (see
Figure 5-11)
=OFFSET(Sales_Data[[#Headers],[OrderDate]],0,0,
COUNTA(Sales_Data[OrderDate])+2,COUNTA(Sheet1!$1:$1))
The COUNTA functions count the number of rows in the Sales_Data table and the cells with
data in Row 1 on Sheet1 The size of the new range is based on those counts Two extra rows
(+2) are added to the size of the range, to make it slightly different from the original table’s
range
Figure 5-11.Create a new named range.
Data Source
Sales-DataNew, and then click OK
a blank row was intentionally included in the SalesDataNew range, a (blank) item
appears in the list of order dates You can hide that item in the OrderDate filter list