Above, the criteria are fully contained with the formulas. But suppose you enter the value 5 in cell J3. You then could use these formulas:
You also could use array formulas to return the same results:
Notice that the two array formulas above sum ones and zeros rather than using a count function.
Notice that the two formulas above use null strings ("") rather than zero. If you were to use zero, the AVERAGE function would include those zeros in the average.
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 41 | =SUMIF(Prods,"=Hats", Units) |
| 7 | =COUNTIF(Prods,"=Hats") |
| 5.9 | =AVERAGEIF(Prods,"=Hats") |
We could use array formulas to return the same results:
| 41 | {=SUM(IF(Prods="Hats",Units,0))} |
| 7 | {=SUM(IF(Prods="Hats",1,0))} |
| 5.9 | {=AVERAGE(IF(Prods="Hats",Units,""))} |
As before, "Hats" could be in a cell that the formulas could reference.
And again, notice that to count the number of hat sales, we we SUM an array of ones and zeros rather than using a COUNT function. And we also use a null string ("") rather than zeros for the AVERAGE-IF array formula.
So far, the examples have used only one criteria. But frequently, Excel users need to summarize data using multiple criteria. Excel 2007 introduced three new summary functions that allow for multiple criteria:
=SUMIFS(sum_range,criteria_range,criteria,...)
=COUNTIFS(criteria_range,criteria,...)
=AVERAGEIFS(average_range,criteria_range,criteria,...)
These functions work like SUMIF, COUNTIF, and AVERAGEIF, but they allow you to add any number of criteria. Here are some examples:
| 15 | =SUMIFS(Units,Clients,"=Smith",Colors,"=Blue") |
|
Return the total number of blue products sold to Smith. |
|
| 4 | =COUNTIFS(Clients,"=Smith",Colors,"=Blue") |
|
Return the number of times we've sold blue products to Smith. |
|
| 3.8 | =AVERAGEIFS(Units,Clients,"=Smith",Colors,"=Blue") |
|
Return the average number of blue products sold to Smith. |
|
Before you can use arrays successfully, you need to understand how they perform their calculations.
Array-enter the following formula in some cell. Then, in the formula bar, select the highlighted part of the formula:
{=SUM(IF(Prods="Hats",Units,0))}
With this piece of the formula selected, press the F9 key. When you do so, the formula changes to:
=SUM(IF({FALSE;TRUE;TRUE;TRUE;TRUE;TRUE;TRUE;TRUE;FALSE;
FALSE;FALSE;FALSE;FALSE;FALSE;FALSE},Units,0))
(You can use a similar approach to calculate one or more sections of any formula in your formula bar. After checking your formula, press Esc to return to your original formula. If you accidentally press Enter rather than Esc, just click on Undo in QAT.)
Now select Units in your formula bar and press F9 again. After you do so, the formula expands to:
=SUM(IF({FALSE;TRUE;TRUE;TRUE;TRUE;TRUE;TRUE;TRUE;FALSE;
FALSE;FALSE;FALSE;FALSE;FALSE;FALSE},{0;7;5;4;7;4;8;6;1;
1;5;2;5;3;0},0))
Including the gray borders in the table, the database shown above and below has 15 rows of data. As a consequence, the green section of the formula has 15 TRUE and FALSE results, and the brown section has 15 numbers. Each TRUE in the green section marks where it's true that the product equals "Hats".
Press Esc to dismiss your changes in the formula bar. Now select the highlighted part of this formula:
{=SUM(IF(Prods="Hats",Units,0))}
Again, press F9. When you do so your formula bar displays:
=SUM({0;7;5;4;7;4;8;6;0;0;0;0;0;0;0})
Now compare the final orange section with the final green and brown sections above.
Notice that whenever the green section is TRUE, the orange section contains the corresponding value from the brown section; otherwise, the orange section contains zero.
From
another perspective, notice that whenever the product is Hats in this
figure, its number of units appears in the orange section above.
Finally, the SUM function returns the sum of the numbers in the orange section.
In general, this is how all array formulas work. They apply the logic of your array formula to each corresponding value in the multi-cell ranges that your formula references.
Don't worry if this idea isn't too clear yet, the following examples should help to give you a better understanding of how this works.
Let's take a simple example of how array functions can summarize multiple criteria:
| 15 | {=SUM(IF((Clients="Smith")*(Colors="Blue"),Units,0))} |
|
Return the total number of blue products sold to Smith. |
|
| 4 | {=SUM(IF((Clients="Smith")*(Colors="Blue"),1,0))} |
|
Return the number of times we've sold blue products to Smith. |
|
From the previous discussion you already know that the Excel formula converts both Clients="Smith" and Colors="Blue" into a series of TRUE and FALSE values. But the gray section of the formula above multiplies these two arrays. You can see the results of this process if you calculate the gray section in your formula bar.
That is, after you array-enter the formula by pressing Ctrl+Shift+Enter, select the gray section shown above and press F9. When you do so, your formula bar will show this result:
=SUM(IF({0;0;0;0;0;1;0;1;0;0;0;1;0;1;0},Units,0))
Here, the array displays 1 only where the client equals "Smith" and where the color equals "Blue". Otherwise, the formula returns 0. The following display shows how this works.
| (Clients="Smith") |
{FALSE;TRUE;FALSE;TRUE;FALSE;TRUE;FALSE;TRUE; FALSE;TRUE;FALSE;TRUE;FALSE;TRUE;FALSE} |
| (Colors="Blue") |
{FALSE;FALSE;FALSE;FALSE;FALSE;TRUE;TRUE;TRUE; FALSE;FALSE;FALSE;TRUE;TRUE;TRUE;FALSE} |
| (Clients="Smith") *(Colors="Blue") |
{0;0;0;0;0;1;0;1; 0;0;0;1;0;1;0} |
Here, I've highlighted the four results in each array where the TRUEs are in sync. These results correspond with the four values of 1 found in the third row of the table.
Notice in the original array formula that each logical test is enclosed in parentheses. These parentheses are required. That is, within a multi-criteria array formula:
| This works: | (Clients="Smith")*(Colors="Blue") |
| This fails: | Clients="Smith"*Colors="Blue" |
Here are some more examples of array formulas in action:
| 7 | {=SUM(IF((Prods="Coats")*(Colors="White"),Units,0))} |
| Total number of white coats sold | |
| 6 |
=SUM(IF((Prods="Coats")*(Colors="White")* (Clients="Jones"),Units,0)) |
| Total number of white coats sold to Jones. | |
| 6 |
=SUM(IF((Prods="Coats")*(Colors="White")* (Clients="Jones")*(MONTH(Dates)=12),Units,0)) |
| Total number of white coats sold to Jones in any December. | |
Notice in the last example above that you can perform operations on values in a column, and then compare the results to other values. Specifically, the green section of the formula finds the month value of each date and then compares it to 12.
However, this particular date test isn't very practical, because we typically need the month and year in a test like this...not merely the month. Here is a more practical date test:
| 19 |
=SUM(IF((Dates>=DATE(2007,12,1))*(Dates<=DATE(2008,1,0))* (Clients="Jones"),Units,0)) |
| Total number of units sold to Jones in December, 2007. | |
In actual practice, you probably wouldn't include the DATE function in your formula, for two reasons.
First, the formula forces Excel to calculate the same date value for each cell in the array. This slows calculation slightly. Second, you would need to change the formula to change the dates you want to report.
Instead, you probably would enter the date functions in two cells of your spreadsheet. You could name the first cell FirstDate and the second cell LastDate. Your array formula then could be:
| 19 |
=SUM(IF((Dates>=FirstDate)*(Dates<=LastDate)* (Clients="Jones"),Units,0)) |
| Total number of units sold to Jones within the specified dates. | |
So far, I haven't demonstrated the real value of using array formulas with multiple criteria: They allow you to set up standard reports that work much like PivotTables, but with more flexibility in many ways.
Here,
for example, the actual report is in the range A7:D11. The area at the
top of the page contains setup values and side calculations.
The two values highlighted in yellow act like PivotTable page fields. You can change their values to change the report.
Cell B5 checks the report for errors. If the grand total in cell D11 doesn't match the total of all sales for the specified client and month, the Error value turns TRUE, and the report title changes to "ERROR!!".
The key formulas for this report are shown with their cell addresses below. These formulas rely on names assigned to the range B1:B5. To assign these names, select the range A1:B5; choose Formulas, Defined Names, Create from Selection; in the Create Names dialog make sure that only Left Column is checked; then choose OK.
(You also can choose Ctrl+Shift+F3 to launch the Create Names dialog.)
Here are the key values and formulas:
B1: 12/1/2007
B2: Jones
Enter these values as shown. To format the date in cell B1, select that cell; in the Home tab, choose the Number group's dialog launcher; in the Number tab of the Format Cells dialog enter this format string as the Type value: mmm-yy; then choose OK.
(You also can choose Ctrl+1 to launch the Format Cells dialog.)
J3: =DATE(YEAR(ReportMonth),MONTH(ReportMonth),1)
B4: =DATE(YEAR(ReportMonth),MONTH(ReportMonth)+1,0)
The array formulas that follow use the values returned by these standard formulas, as I described above.
B5: {=SUM(IF((Dates>=FirstDate)*(Dates<=LastDate)*
(Clients=Client),Units,0))<>$D$11}
This array formula finds total unit sales for the specified month and client, and then compares that value to the grand total in cell D11. If the values don't match -- if there's an error -- the formula returns TRUE; otherwise, it returns FALSE.
A7: =IF(Error,"ERROR!!","Unit Sales, "&Client)
Normally, this formula displays the current client name with the report title. But if there's an error, this formula returns "ERROR!!".
D7: =TEXT(ReportMonth,"mmmm yyyy")
This formula returns the date specified in cell B1. It uses the TEXT function to format that date as shown in the report. The cell is right-justified.
B9: {=SUM(IF((Dates>=FirstDate)*(Dates<=LastDate)*
(Clients=Client)*(Prods=$A9)*(Colors=B$8),Units,0))}
This array formula uses five criteria to return the correct value to the cell.
When you copy this formula to the rest of the report, you must paste it in two steps. This is because Excel doesn't allow a cell with an array to be copied to a range that includes the copied cell.
Therefore, copy cell B9 and paste it to cell D9. Then copy the range B9:C9 and paste it to the range B10:C10.
D9: =SUM(B9:C9)
B11: =SUM(B9:B10)
These formulas provide the row and column totals. Copy them down or to the right as needed.
In old Excel, array formulas became really useful when you needed to use multiple criteria. But because new Excel offers three multi-criteria summary functions, you have a choice of whether to use the new functions or array functions.
But even if you choose to use the new functions, you should keep the power of arrays in the back of your mind. This is because you can use array functions for other types of calculations. Here are some examples:
| 6 | {=MAX(IF((Clients="Smith")*(Colors="Blue"),Units,""))} |
| 2 | {=MIN(IF((Clients="Smith")*(Colors="Blue"),Units,""))} |
| The largest and the smallest number of blue products sold to Smith. | |
| 5 | {=LARGE(IF((Clients="Jones")*(Colors="Blue"),Units,""),2)} |
| The second largest number of blue products sold to Jones. | |
| 7 | {=SMALL(IF((Clients="Jones")*(Prods="Hats"),Units,""),2)} |
| The second smallest number of hats sold to Jones. | |
| 6 | {=MEDIAN(IF((Colors="White")*(Prods="Hats"),Units,""))} |
| The median number of white hats sold. | |
Array formulas are very powerful. But they can have several problems:
Even with these potential problems, using arrays to summarize your data can reduce Spreadsheet Hell. This is because much of Spreadsheet Hell involves sorting and summarizing data. By using formulas like the ones shown here to summarize your data, you can begin to reduce the more time-consuming chores associated with Excel reporting.
The sample workbook with the data also includes the report described above. You can download the workbook using this link.
Copyright © 2004 - 2012 by Charles W. Kyd, all rights reserved. Content, graphics, and HTML code are protected by US and International Copyright Laws, and may not be copied, reprinted, published, translated, hosted, or otherwise distributed by any means without explicit permission. Terms of Use | Privacy Policy | Earnings Policy.
Excel DashboardsLearn how to create top-quality dashboard reports with Excel. |