Page 311 - Excel 2007 Bible
P. 311
19_044039 ch14.qxp 11/21/06 11:06 AM Page 268
Part II
Working with Formulas and Functions
Summing the “top n” values
In some situations, you may need to sum the n largest values in a range — for example, the top ten values. If
your data resides in a table, you can use autofiltering to hide all but the top n rows and then display the
sum of the visible data in the table’s total row.
Another approach is to sort the range in descending order and then use the SUM function with an argument
consisting of the first n values in the sorted range.
A better solution — which doesn’t require a table or sorting — uses an array formula like this one:
{=SUM(LARGE(Data,{1,2,3,4,5,6,7,8,9,10}))}
This formula sums the ten largest values in a range named Data. To sum the ten smallest values, use the
SMALL function instead of the LARGE function:
{=SUM(SMALL(Data,{1,2,3,4,5,6,7,8,9,10}))}
These formulas use an array constant comprised of the arguments for the LARGE or SMALL function. If the
value of n for your top-n calculation is large, you may prefer to use the following variation. This formula
returns the sum of the top 30 values in the Data range. You can, of course, substitute a different value for 30.
{=SUM(LARGE(Data,ROW(INDIRECT(“1:30”))))}
CROSS-REF
CROSS-REF See Chapter 17 for more information about using array constants.
Conditional Sums Using a Single Criterion
Often, you need to calculate a conditional sum. With a conditional sum, values in a range that meet one or
more conditions are included in the sum. This section presents examples of conditional summing by using
a single criterion.
The SUMIF function is very useful for single-criterion sum formulas. The SUMIF function takes three
arguments:
n range: The range containing the values that determine whether to include a particular cell in the
sum.
n criteria: An expression that determines whether to include a particular cell in the sum.
n sum_range: Optional. The range that contains the cells you want to sum. If you omit this argu-
ment, the function uses the range specified in the first argument.
The examples that follow demonstrate the use of the SUMIF function. These formulas are based on the
worksheet shown in Figure 14.13, set up to track invoices. Column F contains a formula that subtracts the
date in column E from the date in column D. A negative number in column F indicates a past-due payment.
The worksheet uses named ranges that correspond to the labels in row 1.
All the examples in this section also appear on the companion CD-ROM. The file is named
ON the CD-ROM conditional sum.xlsx.
ON the CD-ROM
268