Exceljet

Quick, clean, and to the point

Sum bottom n values with criteria

Excel formula: Sum bottom n values with criteria
Generic formula 
=SUM(SMALL(FILTER(values,criteria),{1,2,3}))
Summary 

To sum the lowest n values in a range with criteria, you can use a formula based on FILTERSMALL, and SUM. In the example shown, the formula in cell F5 is:

=SUM(SMALL(FILTER(data[Values],data[Group]="A"),{1,2,3}))

where data is an Excel Table in the range B5:C16. The result is the sum of the smallest 3 values in Group A (10,15,20) which is 45.

Note: FILTER is a newer function not available in all versions of Excel. See below for an alternative formula that works in older versions.

Explanation 

In this example, the goal is to sum the smallest n values in a set of data after applying specific criteria. In the worksheet shown, we want to sum the three smallest values, so n is equal to 3. At a high level, this problem breaks down into three separate steps:

  1. Apply criteria to select specific values
  2. Extract the 3 smallest values
  3. Sum the 3 extracted values

This problem can be solved with a formula based on the FILTER function, the SMALL function, and the SUM function. For convenience, the range B5:C16 is an Excel Table named "data". This allows the formula to use structured references.

Note: FILTER is a newer function not available in "Legacy Excel". See below for an alternative formula that works in older versions of Excel.

Example formula

The final formula in cell F5 is:

=SUM(SMALL(FILTER(data[Values],data[Group]="A"),{1,2,3}))

To explain how this formula works, we'll walk through the steps listed above. This means we will be working through the formula from the inside out. This is typical of Excel formulas where one function is nested inside another.

Apply criteria

The first step in the problem is to apply criteria to select values by group. This can be done with the FILTER function. To select values in group "A", we can use FILTER like this:

FILTER(data[Values],data[Group]="A")

In this formula array is provided as the Values column in the table, and the include argument provides logic to select only values in group "A". The result is a subset of the values where the group is "A", which is returned as an array like this:

{10;65;25;45;20;15}

If you are new to the FILTER function, see this video: Basic FILTER function example

Extract 3 smallest values

The next step in the problem is to extract the three smallest values from the array returned by FILTER. For this, we use the SMALL function. The SMALL function is designed to return the nth smallest value in a range. For example:

=SMALL(range,1) // 1st smallest
=SMALL(range,2) // 2nd smallest
=SMALL(range,3) // 3rd smallest

Normally, SMALL returns just one value. However, if you supply an array constant (e.g. a constant in the form {1,2,3}) to SMALL as the second argument, k , SMALL will return an array of results instead of a single result. For example:

=SMALL(range,{1,2,3}) // smallest 3 values

Picking up where we left off, FILTER is used to collect values in group "A". The results returned by FILTER are returned directly to the SMALL function like this:

SMALL({10;65;25;45;20;15},{1,2,3}) // returns {10,15,20}

The SMALL function then returns the 3 smallest values in an array: 10, 15, 20.

Sum smallest values

The final step in problem is to sum the values extracted by the SMALL function. This is done with the SUM function:

=SUM({10,15,20})

SUM returns 45 as a final result, which is the sum of the smallest 3 values in group A.

When n becomes large

As n becomes a larger number, it becomes tedious to enter longer array constants like {1,2,3,4,5,6,7,8,9,10}, etc. In this case, you can use the SEQUENCE function to generate an array of sequential numbers automatically like this:

=SUM(SMALL(FILTER(values,criteria),SEQUENCE(n))

Just replace n with the number of smallest values you want to extract:

=SUM(SMALL(FILTER(values,criteria),SEQUENCE(3)) // smallest 3
=SUM(SMALL(FILTER(values,criteria),SEQUENCE(5)) // smallest 5

Legacy Excel

In older versions of Excel that don't provide the FILTER function, you can use the IF function to "filter" data like this:

=SUM(SMALL(IF(data[Group]="A",data[Values]),{1,2,3}))

The behavior of this formula is much the same as the original formula above. The main difference is that the IF function returns values from group A when the group="A", but it returns FALSE when the group does not match the supplied criteria. So the SMALL function receives an array from IF that looks like this:

{10;FALSE;65;FALSE;25;FALSE;45;FALSE;20;FALSE;15;FALSE}

Unlike the FILTER function, which returned just the six values associated with group A, if returns an array that includes twelve results, one for each value in the original data. However, because SMALL is programmed to automatically ignore the logical values TRUE and FALSE, the result from SMALL, {10,15,20},  is the same as before and the final result is correct:

=SUM({10,15,20}) // returns 45

Note: this an array formula and must be entered with Control + Shift + Enter in Legacy Excel.

Author 
Dave Bruns

Excel Formula Training

Formulas are the key to getting things done in Excel. In this accelerated training, you'll learn how to use formulas to manipulate text, work with dates and times, lookup values with VLOOKUP and INDEX & MATCH, count and sum with criteria, dynamically rank values, and create dynamic ranges. You'll also learn how to troubleshoot, trace errors, and fix problems. Instant access. See details here.

Download 100+ Important Excel Functions

Get over 100 Excel Functions you should know in one handy PDF.