© The Author(s), under exclusive license to APress Media, LLC, part of Springer Nature 2022
A. MurrayAdvanced Excel Formulashttps://doi.org/10.1007/978-1-4842-7125-4_9

9. Next-Level Aggregation Functions

Alan Murray1  
(1)
IPSWICH, UK
 

In the previous chapter, we saw functions such as SUMIFS and COUNTIFS performing aggregations dependent upon criteria. They are incredible functions, but as with everything in life, they have their limitations. You may need to perform aggregations outside of the scope of those functions’ capabilities.

This chapter covers two next-level aggregation functions – SUMPRODUCT and AGGREGATE. These functions are amazing, and each one has specific abilities that make them extremely useful.

We will begin with the SUMPRODUCT function. This function has achieved infamy with many advanced Excel users who label it as their favorite function. It’s ability to handle arrays is possibly its greatest strength, and this makes it very valuable to users of Excel versions prior to Excel 365.

We then move on to the AGGREGATE function. This function is a powerful toolset of 19 different aggregations including sum, large, median, and mode. But it does not stop there. It has additional abilities such as to ignore errors and hidden values in a range when aggregating.

SUMPRODUCT Function

Availability: All versions

File

sumproduct.xlsx

The SUMPRODUCT function returns the sum of the products from one or more arrays. Its syntax is straightforward, as it is simply a list of the arrays you want it to use.
=SUMPRODUCT(array1, [array2], [array3], ...)

Each array can be a range of values or an array returned by a function or a logical expression. Each array must be of the same height or width.

The SUMPRODUCT function multiplies the values of the corresponding arrays and then sums the results.

Let’s start with a simple example of SUMPRODUCT being used in its conventional way. And then explore more powerful examples including handling multiple conditions and working with arrays.

Classic SUMPRODUCT

The conventional use of the SUMPRODUCT function can remove the need for performing intermediary row calculations, therefore reducing calculation time and improving spreadsheet performance.

In Figure 9-1, there is some transactional data, and formulas have been entered in column D to multiply the price by the number sold. These row totals are then summed in cell D8 with a simple SUM function.
=SUM(D2:D6)
Figure 9-1

Summing the row totals

This is fine, and the row totals may be useful. However, if there were thousands of rows in this dataset, this may be an unnecessary volume of calculations and strain on the spreadsheet.

The total could be calculated with one SUMPRODUCT formula. In Figure 9-2, the following formula is used:
=SUMPRODUCT(B2:B6,C2:C6)
SUMPRODUCT multiplies the values in the corresponding arrays of B2:B6 and C2:C6, for example, 48*8, 53*2, 37*12. These intermediary totals are then summed to produce the result 1236.
Figure 9-2

One formula solution with SUMPRODUCT

Only two arrays were used in this example, and SUMPRODUCT can handle up to 255 arrays . Let’s move on and see further examples of the extreme power and versatility of this function.

Sum Values That Meet Multiple Criteria

The SUMPRODUCT function is fantastic for summing and counting values that meet multiple criteria. It can handle both AND and OR logic, so the criteria can be complex.

It is much more efficient at handling different logical tests than the IFS family of functions (SUMIFS, COUNTIFS, etc.). We saw a technique to use OR logic with the SUMIFS function in the previous chapter, as natively it cannot apply OR logic.

Let’s start by using the SUMPRODUCT function to sum values that meet a single condition. We will then increase the complexity of the criteria by adding more conditions and explain how it all works.

For these examples, we will use the data in the table named [tblSales] on the [Data] worksheet. The first few rows of this table are shown in Figure 9-3.
Figure 9-3

Sales table to be used for SUMPRODUCT examples

In Figure 9-4, the following formula sums the [Total] values for all sales of the product entered in cell B3:
=SUMPRODUCT((tblSales[Product Name]=B3)*tblSales[Total])
Figure 9-4

SUMPRODUCT function summing values that meet one condition

This formula is quite different to others that we have seen so far in this book. Two things that stand out are that the condition is enclosed within its own set of brackets and that the results of that condition are multiplied by the values in the [Total] column.

Let’s break down how this all works.

Looking at just the first five rows in Figure 9-3, the logical tests return the following array of values. The first and fifth rows contain the [Product Name] of “Coffee.” So TRUE is returned, while the other rows return FALSE.
{TRUE; FALSE; FALSE; FALSE; TRUE}
Note

A semicolon (;) is used for a break in row, while a comma (,) is used for a break in column.

This array of values is multiplied by the corresponding value from the [Total] column. On performing a mathematical calculation, the TRUE and FALSE values are converted to 1s and 0s. Then the following calculations are performed:
{1; 0; 0; 0; 1}
*
{59.06; 424.80; 207.30; 436.53; 424.30}
These calculations result in the following array. And this array of values is summed by SUMPRODUCT.
{59.06; 0; 0; 0; 424.30}

Now that we understand how this is working, let’s add a second condition to our SUMPRODUCT formula.

In Figure 9-5, the following formula sums the [Total] values for all sales of the product entered in cell B3 and the store entered in cell C3:
=SUMPRODUCT(
(tblSales[Product Name]=B3)*(tblSales[Store]=C3),
tblSales[Total]
)

In this formula, each condition is enclosed within its own brackets. Each condition will return an array of TRUE and FALSE values dependent on the result of that logical test. The asterisk (*) is used to multiply the corresponding values of the two arrays. This applies AND logic between the two conditions.

Looking at the first five rows of the sales table, the following two arrays represent the results of the two conditions:
{TRUE; FALSE; FALSE; FALSE; TRUE}
*
{TRUE; FALSE; TRUE; FALSE; FALSE}
After the values of the two arrays are multiplied together, the following array is returned:
{1; 0; 0; 0; 0}

The asterisk, or multiply symbol, is used for AND logic because you only get a non-zero value if all logical tests return TRUE. For example, 1*1=1 but 1*0=0 and 0*1=0.

Note

All numbers except zero evaluate to TRUE including both negative and positive values, while zero evaluates to FALSE.

The resulting array from the conditions is then multiplied by the corresponding values in the [Total] column. And the resulting values from that are then summed by SUMPRODUCT.
Figure 9-5

Summing values that meet two conditions with AND logic

To perform OR logic between conditions with SUMPRODUCT, the plus symbol (+) is used. Let’s see an example.

In Figure 9-6, the following formula is used to sum the [Total] values for both the products in cells B3 and C3:
=SUMPRODUCT(
(tblSales[Product Name]=B3)+(tblSales[Product Name]=C3),
tblSales[Total]
)
Why is the plus symbol used for OR logic? Let’s dive into the detail.
Figure 9-6

Applying OR logic between conditions with SUMPRODUCT

As always, each condition is enclosed in its own set of brackets. When looking at the first five rows only, the following arrays are returned as the results of the two conditions in this formula:
{TRUE; FALSE; FALSE; FALSE; TRUE}
+
{FALSE; FALSE; FALSE; TRUE; FALSE}
The corresponding values from these arrays are added together. This results in the following array being returned:
{1; 0; 0; 1; 1}

These values are then multiplied by the corresponding value from the [Total] column and summed by SUMPRODUCT.

By adding the corresponding values returned by all conditions, as long as one of the logical tests returns TRUE, then the overall result is TRUE. This is perfect OR logic.

If we tested multiple columns, and more than one column returned TRUE, this is fine. Any non-zero value evaluates to TRUE. So, (TRUE + TRUE) = TRUE.

Let’s see one final example, where we will use a combination of both AND and OR logic in a SUMPRODUCT function. These conditional tests can, of course, get more complex than we will take them in this chapter.

In Figure 9-7, the following formula sums the [Total] values for the products entered in cells B3 and C3 for the store entered in cell D3:
=SUMPRODUCT(
((tblSales[Product Name]=B3)+(tblSales[Product Name]=C3))*
(tblSales[Store]=D3),
tblSales[Total]
)
Figure 9-7

Using both AND and OR logic in SUMPRODUCT

In this formula, the OR segment is enclosed in its own set of brackets. This is to follow the mathematical order of precedence and ensure that the OR segment evaluates before applying the AND logical expression to the selected store.

Let’s break this down.

Looking at the first five rows only, the following arrays are returned by the conditions:
{TRUE; FALSE; FALSE; FALSE; TRUE}
+
{FALSE; FALSE; TRUE; FALSE; FALSE}
*
{TRUE; FALSE; TRUE; FALSE; FALSE}
The OR segment then evaluates to return the following arrays:
{1; 0; 1; 0; 1}
*
{TRUE; FALSE; TRUE; FALSE; FALSE}
The values of the corresponding arrays are then multiplied (AND segment):
{1; 0; 1; 0; 0}
The values in this array are then multiplied by the corresponding values in the [Total] column and summed by SUMPRODUCT. So, for the first five rows (Figure 9-3), the following values are summed:
{59.06; 0; 207.30; 0; 0}
Note

Conditions are entered in the same manner in the FILTER function, discussed in Chapter 13. So, what we are learning here is directly transferable to criteria in the FILTER function.

Count Values That Meet Multiple Criteria

To count values instead of summing them, we simply need to remove the array from the formula that contains the values to be summed.

The following formula is the same formula from the previous example, but without the [Total] column. This is shown in Figure 9-8.
=SUMPRODUCT(
((tblSales[Product Name]=B3)+(tblSales[Product Name]=C3))*
(tblSales[Store]=D3)
)
Because the [Total] column is omitted from the formula, SUMPRODUCT is summing the values returned from the conditions only. So, for the first five rows, it is summing the following values:
{1; 0; 1; 0; 0}
Figure 9-8

Counting values that meet multiple conditions with SUMPRODUCT

All the conditions used in the examples so far have tested text values. So, let’s finish with an example that tests a numeric value.

In Figure 9-9, the following formula counts the number of sales for the store entered in cell B3 where the number of units sold was greater than or equal to the number entered in cell C3:
=SUMPRODUCT(
(tblSales[Store]=B3)*(tblSales[Units Sold]>=C3)
)
Figure 9-9

SUMPRODUCT including a test of a numeric value

Aggregating Multiple Columns

An area that showcases the flexibility of the SUMPRODUCT function well is its ability to handle criteria ranges of different dimensions.

Figure 9-10 shows a matrix of student names and their results to five different questions. “C” stands for correct and “I” for incorrect. A blank cell represents no answer to the question.

We want to return the number of correct questions answered for the student specified in cell B2. Cell B2 has been named [rngStudent].

The following COUNTIFS function fails to produce a result because the IFS functions (SUMIFS, COUNTIFS, etc.) require the criteria ranges to be of the same dimensions. And in this example, the student names’ range is one column wide, while the questions’ range is five columns wide.
Figure 9-10

COUNTIFS function failing to produce a result

=COUNTIFS(A5:A8,rngStudent,B5:F8,"C")

This task is no problem for SUMPRODUCT. It can easily handle criteria ranges of irregular dimensions.

In Figure 9-11, the following SUMPRODUCT formula is used to return the number of correct answers for the student “Jason”:
=SUMPRODUCT((A5:A8=rngStudent)*(B5:F8="C"))
Figure 9-11

SUMPRODUCT function handling irregular criteria ranges

Distinct Count Formula

A function that is unfortunately missing from the library of functions in Excel is one that counts only the distinct items in a range.

Figure 9-12 shows a list of webinars that have been delivered. We would like to return the distinct number of different webinars delivered.
Figure 9-12

Table of a schedule of webinars that were delivered

This list is formatted as a table named [tblWebinars]. We will achieve the distinct count with a formula that combines the SUMPRODUCT and COUNTIFS functions together.

In Figure 9-13, the following formula is entered in cell D6 to return the distinct count of values in the [Webinar] column:
=SUMPRODUCT(1/COUNTIFS(tblWebinars[Webinar],tblWebinars[Webinar]))
Figure 9-13

Distinct count formula with SUMPRODUCT

Let’s break down how this formula works.

The COUNTIFS function counts the number of occurrences of each value in the [Webinar] column and returns the results in an array.
{3; 2; 3; 1; 2; 1; 3; 2; 2}
One is then divided by each of the values in the array returned by COUNTIFS. This returns the following array of results:
{0.33;0.5;0.33;1;0.5;1;0.33;0.5;0.5}

SUMPRODUCT then sums the values in this array.

This is a nice technique to produce a distinct count. It takes advantage of SUMPRODUCT’s ability to work with arrays and the fabulously useful COUNTIFS function.

Note

A distinct count formula is even easier to create with the new UNIQUE function in Excel. We will cover this technique in Chapter 10.

Weighted Average

Excel also does not have a built-in function to calculate a weighted average. Fortunately, SUMPRODUCT with its ability to handle arrays natively makes this task simple.

In Figure 9-14, we have a table named [tblAssessments]. The arithmetic mean has been calculated using the AVERAGE function on the [Score] column and returns 78.2.

However, this is wrong because the assessments are not weighted equally. Some assessments make a greater contribution to the final average than others. This is acknowledged by the [Weight] column. For example, the final exam carries the most weight toward the final average.
Figure 9-14

Arithmetic mean on the assessment scores

The following formula is shown in Figure 9-15. The values in the [Score] column are multiplied by the values in the [Weight] column. This produces an array of values for SUMPRODUCT to sum.
=SUMPRODUCT(tblAssessments[Score]*tblAssessments[Weight])
Figure 9-15

Weighted average with SUMPRODUCT

Sum Every Nth Cell

You may come across a scenario where the values you want to sum are not in consecutive cells. Figure 9-16 shows a range that includes quarterly totals in every fourth column (every quarter).

We want to sum only the quarter values as they are subtotals of the months in that quarter. Of course, Figure 9-16 shows only two quarters, but imagine this is a larger range. In a different dataset, there may be many columns occurring every Nth column that you want to sum.
Figure 9-16

Range with quarterly values in every fourth column

In Figure 9-17, the following formula has been entered in cell B2 and filled down range B3:B7:
=SUMPRODUCT((MOD(COLUMN(C2:J2)-2,4)=0)*(C2:J2))
In this formula, the COLUMN function is used to return the column number of each cell in the range. This formula shows the array returned by the column function:
=SUMPRODUCT((MOD({3,4,5,6,7,8,9,10}-2,4)=0)*(C2:J2))

Two is subtracted from each of these column numbers. This is because the range starts in column C, the third column. So, this offsets columns A and B. If the range used started from column A, nothing would need to be subtracted.

The MOD function divides each number in the array by 4 and returns the remainder. The following formula shows what we have at this point:
=SUMPRODUCT(({1,2,3,0,1,2,3,0}=0)*(C2:J2))
Each value in the array is then tested to see if it evaluated to 0. After dividing by 4, if the MOD function returned the remainder of 0, that would indicate that the value is in the fourth column of the range. The following TRUE and FALSE values are returned:
=SUMPRODUCT({FALSE,FALSE,FALSE,TRUE,FALSE,FALSE,FALSE,TRUE}*(C2:J2))
From here, the TRUE and FALSE (1 and 0) values are multiplied by the values in range C2:J2, and the resulting values are summed by SUMPRODUCT.
Figure 9-17

SUMPRODUCT summing the values in every fourth column

This formula could easily be adapted to sum every Nth row. The main change would be to swap the COLUMN function for the ROW function.

Sum Values for a Specific Weekday

The SUMPRODUCT function is a built-in array formula. Its ability to work with arrays is one of its great strengths. This makes the function incredibly valuable to users of older versions of Excel that cannot natively handle arrays (discussed in Chapter 10).

We have seen examples of SUMPRODUCT working with arrays already in the last two examples. However, this example is here to demonstrate a common scenario that avoids the need for creating helper columns.

In Figure 9-18, we have a table named [tblDailySales] with sales transactions on different dates. We want to sum only the values that occurred at a weekend (Saturday or Sunday).
Figure 9-18

Table with daily transactions

A common solution is to insert a column and use the WEEKDAY function to return the weekday numbers in all cells of that column. Then use a separate function such as SUMIFS to sum the [Total] values using the weekday number column for the criteria range. Functions like SUMIFS cannot handle the WEEKDAY function directly within its criteria.

With SUMPRODUCT, it can store the weekday numbers returned by WEEKDAY in an array. No need to create an additional physical column. This array of weekday numbers is then tested and calculated all in one formula.

In Figure 9-19, the following formula uses the WEEKDAY function to return the weekday numbers for each date. The 2 within the WEEKDAY function specifies that the weekday numbers should begin with Monday as 1 and end with Sunday as 7.

SUMPRODUCT tests if the number is greater than 5 (after Friday), and then the results are multiplied by the [Total] values and summed.
=SUMPRODUCT(
(WEEKDAY(tblDailySales[Date],2)>5)*
tblDailySales[Total]
)
Figure 9-19

SUMPRODUCT summing only values that occurred at a weekend

Without the use of an array function, you would not be able to provide an entire column of values to a function such as WEEKDAY. This also applies to functions such as LEFT, RIGHT, MONTH, etc. By using SUMPRODUCT, we do not have to force an array formula by pressing Ctrl + Shift + Enter.

Note

In Chapter 10, we will discuss dynamic arrays in Excel for Microsoft 365, Excel for the web, and Excel 2021. With this modern development, even the standard SUM function could handle this array task.

Sum the Top Five Values Only

For the final SUMPRODUCT examples, let’s see it being used to sum the top five values in a range only. Once again, this is a showcase of SUMPRODUCT handling arrays.

Figure 9-20 shows the first eight rows of the [tblSales] table that we worked with earlier in this chapter when summing and counting values that met multiple criteria. We will use this table again now and look to sum the top five values only.
Figure 9-20

Table of sales data

To do this, we will combine SUMPRODUCT with the LARGE function. The LARGE function returns the kth largest value in a range, for example, the second, third, or fifth largest value.

This is the syntax of the LARGE function:
=LARGE(array, k)
  • Array: The range, table column, or array from which to return the kth largest value

  • K: The position of the value in the array (from the largest) that you want to return

    Note There is also a SMALL function in Excel to return the kth smallest value in a range.

We want to return the top five values for SUMPRODUCT to sum, not just a single value. So, for the k argument, we will feed the LARGE function an array of constants containing all the values that we want.

The following is the array that we will use. This requests the first, second, third, fourth, and fifth largest values to be returned. As you can imagine, you can enter whatever values you want in this array, for example, top three, top eight, or top ten.
{1, 2, 3, 4, 5}
In Figure 9-21, the following formula sums the top five values only:
=SUMPRODUCT(
LARGE(tblSales[Total],{1,2,3,4,5})
)
Figure 9-21

Summing the top five values with SUMPRODUCT and LARGE

The LARGE function could be replaced by the SMALL function to return the smallest values to be summed.

Let’s now take this a step further and add criteria to the example. Say, we want to sum the top five values for a specific store only.

In Figure 9-22, the following SUMPRODUCT formula sums the top five values for the store entered in cell B3. Notice that the criterion has been entered within the array argument of the LARGE function.
=SUMPRODUCT(
LARGE((tblSales[Store]=B3)*tblSales[Total],
{1,2,3,4,5})
)
This ensures that the array the LARGE function returns values from only includes values where that criterion was met. Additional criteria could be added just as was discussed earlier in this chapter with SUMPRODUCT.
Figure 9-22

Summing the top five values with additional criteria

The importance of SUMPRODUCT in older versions of Excel is indisputable, from their general purpose of calculating the products of arrays followed by a sum to complex criteria sums and counts and finally its ability to natively handle arrays.

AGGREGATE Function

Availability: Excel 2010, Excel 2013, Excel 2016, Excel 2019, Excel 2021, Excel for Microsoft 365, Excel for the Web, Excel 2011 for Mac, Excel 2016 for Mac, Excel 2019 for Mac, Excel 2021 for Mac, Excel for Microsoft 365 for Mac

File

aggregate.xlsx

The AGGREGATE function was introduced in Excel 2010. This terrific function can apply a plethora of different aggregate functions (19 in fact) to a dataset with the option to ignore hidden rows, error values, and/or subtotals.

It has two different syntaxes. There is the array form:
=AGGREGATE(function_num, options, array, [k])
And there is the reference form:
=AGGREGATE(function_num, options, ref1, ref2, ...
Note

The AGGREGATE function is the successor to the SUBTOTAL function in Excel. SUBTOTAL remains in Excel primarily for compatibility reasons. SUBTOTAL is also used for total row calculations with Excel Tables.

Figure 9-23 shows the AGGREGATE function presenting both syntaxes as the function is written in a cell. Do not worry about choosing the correct syntax at this point however, because the AGGREGATE function will automatically apply the correct syntax based on the information that you provide.
Figure 9-23

AGGREGATE has two syntax options

Let’s have a look at the different arguments of the AGGREGATE function:
  • Function num : A number from 1 to 19 that specifies which aggregate function to use. The following list details all 19 functions:
    • 1 – AVERAGE

    • 2 – COUNT

    • 3 – COUNTA

    • 4 – MAX

    • 5 – MIN

    • 6 – PRODUCT

    • 7 – STDEV.S

    • 8 – STDEV.P

    • 9 – SUM

    • 10 – VAR.S

    • 11 – VAR.P

    • 12 – MEDIAN

    • 13 – MODE.SNGL

    • 14 – LARGE

    • 15 – SMALL

    • 16 – PERCENTILE.INC

    • 17 – QUARTILE.INC

    • 18 – PERCENTILE.EXC

    • 19 – QUARTILE.EXC

Fortunately, you do not need to remember these index numbers because AGGREGATE provides you with a list to select the function from (Figure 9-24).
Figure 9-24

List of the different functions in AGGREGATE

  • Options: A number between 0 and 7 that specifies which values to ignore, if any, when performing the aggregation function.

Once again, the AGGREGATE function provides a list making it easy to select the option that you need (Figure 9-25).
Figure 9-25

List of the options to be ignored when performing the aggregate

  • Array, ref1: The array formula, array of values, or range of cells for the function to be applied.

  • Ref2: Additional ranges of values for which to apply the aggregation function. Up to 253 can be added. This is an optional argument.

  • K: The position in the array to return. It is used by the functions in the following list only – kth largest, kth smallest, kth percentile, kth quartile:
    • LARGE

    • SMALL

    • PERCENTILE.INC

    • QUARTILE.INC

    • PERCENTILE.EXC

    • QUARTILE.EXC

Advantages of using the AGGREGATE function include
  • The vast array of aggregate functions it offers

  • Its ability to handle arrays (great for non-dynamic array–enabled versions of Excel)

  • Its special skill at ignoring hidden rows, errors, and other subtotals

It is not all rosy though. There are some disadvantages:
  • It is not easy to understand. As the function and options arguments return the selection as an index number, it is confusing to the user reading the formula.

  • It was designed to aggregate vertical ranges of data. If it is used to aggregate data in rows, aggregate cannot ignore hidden columns. The aggregate result will include the hidden values.

  • Criteria can be added to the AGGREGATE function just like with SUMPRODUCT (an example is shown); however, when adding criteria, the options such as ignoring hidden rows are not applied. This is because once criteria are applied, the range is converted to an array, and arrays cannot have hidden values.

Let’s see some examples of the AGGREGATE function. The following examples will use the table of data named [tblSales].
Figure 9-26

Table of sales data

Ignoring Hidden Rows

When using functions such as SUM and AVERAGE on a range that contains hidden rows, the hidden values are still included by the function.

The AGGREGATE function can be requested to perform calculations on the visible cells only, ignoring any hidden rows, which are probably the result of filtering a list.

In Figure 9-27, the following formula is entered in cell F2 to sum the [Total] values and ignore the hidden rows. The values for the “West” have been hidden by a filter applied to the [Region] column.
=AGGREGATE(9,5,tblSales[Total])
In the formula, the number 9 is the index number for the SUM function, and number 5 specifies the option to ignore hidden rows.
Figure 9-27

AGGREGATE function summing visible values only

Let’s now look at an example where we want to return the second largest value in the [tblSales] table while ignoring hidden rows. When using the LARGE function, we will be required to use the K argument.

In Figure 9-28, the following formula specifies function number 14 – the LARGE function. Position 2 is specified for the K argument.
=AGGREGATE(14,5,tblSales[Total],2)
Figure 9-28

Returning the second largest value while ignoring hidden rows

Figure 9-29

Sum values ignoring errors with AGGREGATE

Figure 9-30

Average ignoring errors and hidden rows

Figure 9-31

Second largest value for the North region only

The “West” values are filtered out of the list. In Figure 9-26, you can see that there is a total value of 436.53 for the “West.” This is the second largest value in the complete list.
Figure 9-32

Lookup table with function names and their index number

Figure 9-33

Drop-down list of functions above the sales table

As the AGGREGATE formula in Figure 9-28 is requested to ignore hidden rows, it returns the correct result of 424.30.

Ignoring Errors in a Range

Functions such as SUM, LARGE, and MEDIAN will not work if a table column or range contains error values. However, the AGGREGATE function has a nifty option to ignore error values.

In Figure 9-29, the following AGGREGATE formula applies the SUM function (number 9) to the [Total] column. Option 6 is specified to ignore error values in the range.
=AGGREGATE(9,6,tblSales[Total])
Note

These examples aggregate a single column of values; however, the AGGREGATE function can handle multiple value ranges and ranges that are multiple columns wide.

The AGGREGATE function provides seven different options. These include the individual options to ignore hidden rows, ignore error values, and ignore other totals. But there are also combined options.

In Figure 9-30, the AVERAGE function (number 1) is applied to the [Total] column. Option 7 has been specified. This option requests the AGGREGATE function to ignore hidden rows and error values.
=AGGREGATE(1,7,tblSales[Total])

A filter is applied to the table to hide the sales of “Hot Chocolate” and “Wine” in the [Product Name] column.

Adding Criteria to AGGREGATE

The AGGREGATE function, like SUMPRODUCT, can process arrays. This makes it a very useful function for those on older versions of Excel, as it negates the requirement to press Ctrl + Shift + Enter when entering an array formula.

Note

In Excel for Microsoft 365, Excel 2021, and Excel for the Web, all formulas can handle arrays (covered in Chapter 10), so this benefit of the AGGREGATE function is not as important.

This is a fantastic functionality; however, options such as ignore hidden rows cannot be used when adding criteria to the AGGREGATE function.

This is a shame as it removes one of the key strengths of AGGREGATE, but it can still be a very useful technique.

In Figure 9-31, the following formula has been used to return the second largest value in the [Total] column for the “North” region only:
=AGGREGATE(14,4,tblSales[Total]*(tblSales[Region]="North"),2)

A criterion has been added within a set of brackets and multiplied by the [Total] column values, just like we did in SUMPRODUCT.

Function number 14 is used to specify the LARGE function, and option 4 is used to ignore nothing. Remember, when adding criteria in an array, options such as hidden rows will not be applied anyhow.

Criteria can only be added like this, for the six functions that can handle arrays, as stated earlier in the chapter.

Function Specified by a Cell Value

For the final examples, let’s use a drop-down list to make it easy for the user to select the function they would like to apply. The user can simply select the aggregate function from a list, instead of typing those awkward index numbers in.

Figure 9-32 shows a table named [tblFunctions]. The [Function] column contains five functions that are being used as the source for a drop-down list. The [Index] column contains the index number associated with each of these functions.

Figure 9-33 shows the drop-down list of functions in cell E2 above [tblSales].

We will use the VLOOKUP function (other lookup functions are covered in Chapter 11) to look up the selected function in cell E3 within [tblFunctions] and return the index number for the AGGREGATE function to use.

In Figure 9-34, the following formula is entered in cell F2. The AVERAGE function has been selected from the drop-down list in cell E2.
=AGGREGATE(
VLOOKUP(E2,tblFunctions,2,0),
7,tblSales2[Total])

The VLOOKUP function is nested within the function num argument of AGGREGATE. It returns the function number from [tblFunctions] for the selected function in E2.

Option 7 is specified to ignore hidden rows and error values. A filter has been applied to the [Product Name] column to exclude “Wine” and also to the [Region] column to exclude “South.”
Figure 9-34

VLOOKUP with AGGREGATE for function based on cell value

With the 19 different functions on offer by AGGREGATE, it makes it simple to create this interactivity and enable a user to choose the function from a nice drop-down list.

Summary

In this chapter, we learned the SUMPRODUCT and AGGREGATE functions in Excel – two powerful functions that offer various benefits over other aggregation functions such as SUMIFS and AVERAGE.

Different examples were shown to demonstrate the full capability of these functions and detail of their limitations.

In the next chapter, we will dive into dynamic array formulas in Excel. One of the greatest updates in Excel history, the ability to handle arrays natively, and have them dynamically update in size, has changed how we write formulas.

We will explain what dynamic arrays are and how to use them effectively with Excel functions and features such as charts. We will also detail, with practical examples, a bunch of dynamic array functions including SORTBY, SEQUENCE, and SUM (OK, that one isn’t very new, but this is SUM 2.0, the dynamic array version).

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.197.11