14.1. Basic Data Grouping
The starting point for simplifying your worksheets is learning how to group data. Grouping data lets you tie related columns or rows into a single unit. Once you've put columns or rows into a group, you can quickly collapse the group, temporarily hiding it and leaving more space for the rest of your data. At its simplest, grouping is just a quick way to help you easily view what you want to see, when you want to see it. It's most handy when you want to show all the key summary information but none of the numbers that you need to crunch in order to arrive at these results.
To see how grouping can simplify complex worksheets, check out the single table of sales report data shown in Figure 14-1. In this example, the information fits easily into the viewable area of the Excel window, but in a real-world company, you could easily end up needing to extend the table in both directions.
|
For example, you might widen the table by dividing the sales into more categories (representing different types of promotions, different discounts from regular prices, or different departments in the store). Or, you might lengthen the table by adding sales quarters from more than one year, or by tracking monthly, weekly, or even daily sales totals. In either case, the table would become fairly unwieldy, forcing you to scroll up and down the worksheet, and from side to side. By grouping columns (or rows) together, you can pop them in and out of view with a single mouse click.
Here are the steps you'd need to follow in order to group several columns together using the sales report example:
Select the columns you want to group.
In most cases, you'll want to group the detail columns that provide fine-grained information (like columns B, C, and D). You shouldn't include in your group any subtotals (like column E) that summarize your information, because you're likely to want to see those subtotals.
To make your selection, drag over the column headers so that you've selected the entire column. This way, Excel can tell right away that you want to group columns. In the sales report example, you would choose columns B, C, and D.
Choose Data Group and Outline Group.
If Excel isn't sure which groups of cells you want to group, it shows a Group dialog box that lets you specify whether you're grouping columns or rows. (For example, if you selected cells B3:D3, Excel would consider it an ambiguous selection because the selection could represent an attempt to group the columns or rows.) If the Group dialog box appears, make your selection and then click OK.
Up To Speed Comparing Grouping and Lists |
Overall, grouping isn't as powerful as Excel's data lists (covered in Chapter 13). Whereas lists include features for sorting, filtering, and searching, groups simply try to make it easier to work with data tables that use one or more levels of subtotals. Of course, there's no reason you can't use both grouping and data lists. For example, you might group one or more columns in a big data list so that you can see just the columns you want (all the columns that track address components , for instance). This approach works well if you're grouping multiple columns, but it doesn't work as well if you're grouping rows. The problem here is that Office 2003 data lists grow dynamically. That means if you add a new row at the bottom of a list (or a new column on either side), the list automatically expands to incorporate the new information. Groups don't have the same behavior. As a result, if your data list grows, some of it might slip out beyond an edge of the group. |
When you group your columns, the worksheet doesn't change, although a new margin area appears at the top of the worksheet, as shown in Figure 14-2. This margin allows you to collapse and expand your groups. To collapse a group, click the minus sign (-), which will then change into a plus sign (+). To expand a collapsed group, click the plus sign (+).
|
To remove a group after you've established it, expand the group, select all the columns, and then choose Data Group and Outline Ungroup. If you select only some of the columns, Excel removes the selected columns from the group, but leaves the rest of the group intactas long as at least two grouped columns remain . (If you remove a column from the middle of a group, you'll actually be left with two groups, one with the columns on the left, and one with the columns on the right.)
|
As you've seen, grouping lets you temporarily shrink the size of a large table by removing specific rows or columns. Grouping becomes even more useful when you create a worksheet that has multiple tables, like the one shown in Figure 14-4.
|
Not only can you create separate groups for different tables, you can also put one group inside another. This approach gives you multiple viewing levels. For example, as shown in Figure 14-5, the person using the worksheet can choose to see all the data in each of the four regional tables, just the summary information (listed in the Total line), or to hide a table completely.
Technically, when you add more than one level of grouping, you're adding collapsible outline views to your worksheet, which let you quickly move back and forth between a bird's-eye view of your data and an up-close glimpse of multiple rows.
Understanding how to quickly collapse and expand tables is all well and good, but before long, you'll want to add up the totals you've collected in these accordion-style tables. In the sales report shown in Figure 14-5, for example, the perfect way to complete this worksheet would be to add a final table that sums up all the Total rows listed in the last row of each division.
|
To create this table, you need to employ just a few more formulas. These formulas will add the separate subtotals (contained in columns B, C, D, and E) to arrive at a final series of grand totals. For example, the following formula would calculate the total sales in all divisions and all quarters for regular merchandise:
=B7+B15+B23+B31
GEM IN THE ROUGH Collapsing and Expanding Multiple Groups at Once |
If you create a worksheet with multiple levels of grouping, you can find yourself doing a lot of clicking to expose all the information you're interested in (or to hide all the details you don't care about). Fortunately, Excel includes a helpful shortcut that lets you expand or collapse multiple groups at once. These are the grouping buttons .
Excel provides two sets of grouping buttons, one for row grouping (just above the row numbers), and one for column grouping (just to the left of the column letters ). Each grouping button is labeled with a number. If you click the number 1, Excel collapses all of the column or row groups. If you click the number 2, Excel collapses all the groups except the top level. If you click the number 3, Excel collapses all the groups except the first two levels, and so on. For example, in the sales worksheet shown in Figure 14-5, you can click 3 to show the whole worksheet, 2 to show just the subtotals, or 1 to hide all the sales tables. Excel displays a different number of grouping buttons, depending on how many levels of grouping there are in your worksheet. The largest numbered button you'll see is 8, because Excel allows a maximum of seven levels of nested groups. |
You'll notice that no matter how you expand or collapse the groups in your worksheet, the result of these formulas is always the same. That's because, whether you're using straight addition (of multiple cells) or the SUM( ) function, Excel takes into account visible and hidden cells. If you're using grouping, however, it might occur to you that it would be handy if you perform a calculation that deals only with the visible cells. That way, you could choose the portions of a worksheet you want to consider, and the formula could recalculate itself automatically to give you the corresponding summary information.
Good news: Excel gives you the power to build this sort of formula. All you need is the awesome SUBTOTAL( ) function. As you recall from Chapter 13, the SUBTOTAL ( ) function is a useful tool for calculating totals in filtered lists. But it's just as helpful when you apply it to outlines because it ignores collapsed rows and columns.
As explained on Section 13.4.3, the first argument (known as the calculation code) you use with the SUBTOTAL( ) function tells Excel the type of math you want to do (summing, averaging, counting, and so on). If you want to ignore the hidden cells in collapsed groups, you have to use the calculation codes above 100. For example, if you want to perform a sum operation, you'll need to use the code 109. (For a full list of the SUBTOTAL( ) calculation codes, see Section 13.4.3.)
Once you've chosen the calculation code, you need to specify the cells or range of cells you want to add. Here's an example that rewrites the earlier formula to sum only visible cells:
=SUBTOTAL(109,B7,B15,B23,B31)
Figure 14-6 shows the difference between using the SUBTOTAL( ) function and the SUM( ) function.
|
Instead of writing your summary formulas by hand, you can generate a summary table automatically that takes advantage of Excel's ability to consolidate data. Consolidation works if you have more than one table with precisely the same layout. For example, you can use consolidation to take the different tables shown in the Figure 14-5 sales report (West Division, East Division, and so on) and calculate summary information. Excel will create a new table that has the same structure, but combines the data from the other tables. You can choose how Excel combines the data, including whether the numbers should be totaled, averaged, multiplied, and so on.
Data consolidation works with any worksheet (with or without grouping); you can even use it to analyze data in identically structured tables from different worksheets.
To consolidate the sales report data, follow these steps:
Move to the location where you want to insert the summary table.
Excel will insert the summary table, starting at the current cell. Make sure you've scrolled down past all your data, so you don't overwrite important information.
Choose Data Consolidate.
The Consolidate dialog box appears, as shown in Figure 14-7.
|
From the Function pop-up menu, choose how you want to combine numbers.
In the sales report, the best choice is Sum to calculate total sales. However, you might want to create separate tables that pick out the best or worst sales using Max and Min.
Click inside the Reference text box. Now, drag to select the first table you want to consolidate in the worksheet.
If Excel's main window isn't already visible, click the icon at the right end of the Reference text box to collapse the Consolidate dialog box. You'll have to click this icon again to restore the window when you're done selecting the cells you want.
Once you've selected the appropriate cells, click the Add button in the Consolidate dialog box.
The range appears in the "All references" list.
Return to step 4 to select the next table you want to consolidate.
Repeat steps 4 and 5 for each table you want to consolidate.
If your selection includes labels (for example, row or column titles), select the "Top row" or "Left column" checkbox to tell Excel where the labels are.
If you don't tell Excel where the labels are, it will ignore these cells and the corresponding cells in the summary table will wind up blank. But if you use these checkboxes to tell Excel where the labels are, Excel simply copies the labels directly to the summary table.
Click OK to generate the summary table (Figure 14-8).
When creating the consolidated data, Excel copies headings and calculates numbers, but doesn't copy any of the source formatting.
The key disadvantage to data consolidation is that it generates a table filled with numbers, rather than formulas. Microsoft's engineers probably designed the consolidation feature this way so that you can consolidate data from multiple files and not worry about losing the information if the source files move or their structure changes.
But this behavior means that if you modify any of the sales figures, the summary table won't update. Instead, you'll need to generate a completely new summary table by using the Data Consolidate command. Fortunately, the second time around should be quite a bit faster, because Excel keeps track of all the ranges you've selected for consolidation, so you won't need to define them again.