Conditional functions: COUNTUNIQUEIF and AVERAGEIF

00Comment iconComment iconComment iconComment icon

Let's continue looking at Excel formulas, now let's try to understand COUNTIFUNIQUEIF and AVERAGEIF

Writer image

翻訳者Editorial

Writer image

改訂者Editorial

Edit Article

COUNTUNIQUEIFS Function

Counts the number of cells with unique content that match a given criterion entered in the function. This function only exists in Google Sheets.

COUNTUNIQUEIFS Function Syntax

=COUNTUNIQUEIFS(count_range; range1; criterion1; [range2; criterion2;...])

Where the range to count is the range of cells that are the target, and the criterion is a logical test (greater than something or less than something).

Example

SalespersonRegionSales
1South87925
2North129887
3Southeast12908
4South99087
5Northeast234987
6North10000
7South105000
8Midwest1876098
9Northeast12387
10South12876
11Southeast123865
12Midwest128543
13North987000

1. How many regions of Brazil are in the table?

2. How many regions of Brazil are in the table and are not from the South or Southeast?

Solution

1. =COUNTUNIQUE(B2:B14 )

2. =COUNTUNIQUEIFS(B2:B14; B2:B14;"<>South"; B2:B14;"<>Southeast" )

How to do it in Excel?

In Excel, we can use two functions to try to produce the same effect:

=COUNTA(UNIQUE(B2:B14))

COUNTA will count the number of cells with values, and the UNIQUE function will return an array with all the unique values that exist where you selected. To answer the second question, it becomes even more difficult; we would have to add yet another layer of function such as FILTER. That is why I do not recommend Excel for answering this type of question. For example:

=COUNTA(UNIQUE(FILTER(B2:B14;(B2:B14<>"South")*(B2:B14<>"Southeast"))))

AVERAGEIF or MEDIASE Function

The AVERAGEIF function is the integration of the AVERAGE function with the IF function. Use the AVERAGEIF function to average the values in a range that meet the criteria you specify.

AVERAGEIF Function Syntax

=AVERAGEIF( source_range; criterion; average_range)

Where source_range and average_range are lists of values and criterion is an expression.

Example

NameAgeSalary
Renato251500
Eduardo231800
Cristina361200
Bárbara381750
Vera221900
Evandro241600
Fátima312100
Carolina271990
Eloisa301300

1. What is the average salary for those over 30 years old?

2. What is the average salary for those under 30 years old?

Solution

1. =AVERAGEIF( B4:B10;">30"; C2:C10) = R$ 1,683.33

2. =AVERAGEIF( B4:B10;"<30"; C2:C10) = R$ 1,681.67

AVERAGEIFS or MEDIASES Function

The AVERAGEIFS function, one of the math and trigonometry functions, adds all its arguments that meet multiple criteria.

AVERAGEIFS Function Syntax

=AVERAGEIFS( average_range; criteria_range1; criterion1; [criteria_range2; criterion2]; ... )

Example

TypePriceCityBedroomsGarage
Cozy Apartment230000Areeiro3No
Holiday Bungalow197000Bela Vista2Yes
Villa345678Bela Vista4Yes
Duplex321900Areeiro2Yes
Mansion450000Bela Vista5Yes
Colonial Mansion395000Bela vista4No

1. What is the average price of a house in Belavista with three or more bedrooms and a garage?

2. What is the average price of a house in Areeiro with three or fewer bedrooms and no garage?

Solution

=AVERAGEIFS(B2:B7 ; C2:C7 ; "=Belavista" ; D2:D7 ; ">2"; E2:E7 ; "=Yes") = R$ 397,839.00

=AVERAGEIFS(B2:B7 ; C2:C7 ; "=Areeiro" ; D2:D7 ; "<4"; E2:E7 ; "=No") = R$ 230,000.00

Exercises

Access here the spreadsheet that will be used for the next exerciseslink outside website

1. What is the average of the grades above 6?

2. How many different grades can be seen?

3. What is the average grade for students who missed more than 2 times? And how many different grades can be seen for students who missed more than 2 times?

4. And the average grade for students who did not miss the first two classes? And how many different grades can be seen for students who did not miss the first two classes?

Use the following table for the next exercises:

OrderCustomerDishAmount (R$)Payment MethodCity
101JoãoPizza50.00CardSão Paulo
102MariaHamburger30.00CashRio de Janeiro
103CarlosSushi80.00CardBelo Horizonte
104AnaPizza55.00PixCuritiba
105RobertoHamburger32.00CashSão Paulo
106FernandaSushi85.00CardSão Paulo
107RicardoPizza52.00PixBelo Horizonte
108CamilaHamburger35.00CardCuritiba
109LuizSushi90.00CashRio de Janeiro

5. How many different cities have registered orders?

6. How many customers paid with Pix or Cash?

7. What is the average amount of orders paid by Card?

8. What is the average amount of Pizza or Sushi orders made in São Paulo?

Use the following table for the next exercises:

CodeProductCategoryStockPrice (R$)Supplier
P001NotebookElectronics123500.00Tech Store
P002SmartphoneElectronics202500.00DigitalShop
P003PrinterOffice8800.00Office Pro
P004DeskFurniture51200.00MoveisPlus
P005ChairFurniture10600.00MoveisPlus
P006TV 55"Electronics152800.00DigitalShop
P007KeyboardAccessories30200.00Tech Store
P008MouseAccessories25150.00Tech Store
P009MonitorOffice7900.00Office Pro

9. How many different product categories exist in stock?

10. What is the average price of products in the Electronics category?

11. How many products have stock below 10 units?

12. What is the average price of products supplied by MoveisPlus?