Sub topic 15
OR logic: several values for one field
This chapter assumes you can write a basic SUMIFS formula and that you know how criteria pairs work. It uses the sample sales data from earlier chapters: Date in column A, Region in B, Product in C, Amount in D, and Notes in E, with data in rows 2 to 100.
Why OR needs a workaround
Every condition inside one SUMIFS, COUNTIFS, or AVERAGEIFS is combined with AND. A row must satisfy all pairs to be included. If you ask for Region equal to "West" and Region equal to "East" in the same formula, no row can be both, so the result is 0.
To get "West or East", you run one query per value and combine the results. This works because a single cell holds only one region, so the two sets of rows never overlap and nothing is counted twice.
Adding separate results
The most readable approach is to add one SUMIFS per value.
=SUMIFS(D2:D100, B2:B100, "West") + SUMIFS(D2:D100, B2:B100, "East")Each part is easy to check on its own. The cost is length: with five values, the formula has five terms.
Using an array constant
An array constant is a list of values in curly braces. When you pass one as a criterion, the function returns one result per value. SUM then adds them.
=SUM(SUMIFS(D2:D100, B2:B100, {"West","East"}))Commas separate values across a row in the constant. Use the same technique with COUNTIFS to count rows for either region.
=SUM(COUNTIFS(B2:B100, {"West","East"}, C2:C100, "Laptop"))The other criteria pairs still apply to every value in the list. This formula counts Laptop rows in West plus Laptop rows in East.
flowchart TDCommon errors
- Overlapping values. Adding results double counts a row only if it can match more than one value. Wildcard criteria such as "W" and "We" can overlap, so check them before you add.
- Mixing fields. This method fits several values of one field. "West laptops or East monitors" is a different problem. You need one complete SUMIFS for each combination, then add them.
- Averages. Do not add AVERAGEIFS results. The average of two averages is not the average of all matching rows. Instead, divide a summed SUMIFS by a summed COUNTIFS.
=SUM(SUMIFS(D2:D100, B2:B100, {"West","East"})) / SUM(COUNTIFS(B2:B100, {"West","East"}))Exercise
Write a formula that totals Amount for the regions North and East, and another that gives the average Amount across those two regions combined.
Show answer
Total: =SUM(SUMIFS(D2:D100, B2:B100, {"North","East"})). Average: =SUM(SUMIFS(D2:D100, B2:B100, {"North","East"})) / SUM(COUNTIFS(B2:B100, {"North","East"})). The average divides the combined total by the combined count, because averaging two separate averages would give the wrong weight to each region.