# Documentation/Calc Functions/AVERAGEIF

Jump to navigation Jump to search

AVERAGEIF

## Category:

Statistical Analysis

## Summary:

Returns the arithmetic mean of all cells in a range that satisfy a given condition. The AVERAGEIF function sums up all the results that match the logical test and divides this sum by the quantity of selected values.

## Syntax:

AVERAGEIF(Range; Criterion [; Average_Range ])

## Returns:

Returns a real value which is the arithmetic mean of all numbers in the range Range that qualify the criteria "Criterion".

## Arguments:

Range is an array, a name of named range or a label of a column or a row containing numbers for averaging or numbers or text for the condition.

Criterion is a condition in the form of expression or a cell reference with expression that defines what cells should be used to calculate the mean. The expression can contain text, numbers, regular expressions (if enabled in calculation options) or wildcards (if enabled in calculation options).

Average_Range is a range of values for calculating the mean.

• If the Average_Range is not specified, Range is used for both, the calculation of the mean and the search according to the condition. If Average_Range is specified, the Range is used only for the condition test, while Average_Range is used for the average calculation.
• If a cell in a range of values for calculating the mean is empty or contains text, function AVERAGEIF ignores this cell.
• If the whole range is empty, contains only text or all values of the range do not satisfy the condition (or any combination of those), the function returns the division(#DIV/0!) error.
• If no cell qualifies the Criterion then the function returns a division(#DIV/0!) error.
• If the Criterion is not a valid one like passing just ">" sign or "!" exclamation mark then the function returns a division(#DIV/0!) error since no cell qualifies for the operation.

## Additional details:

• The search supports wildcards or regular expressions. With regular expressions enabled, you can enter "all.*", for example to find the first location of "all" followed by any characters. If you want to search for a text that is also a regular expression, you must either precede every character with a "\" character, or enclose the text into \Q...\E. You can switch the automatic evaluation of wildcards or regular expression on and off in Tools > Options > LibreOffice Calc > Calculate. Warning: When using functions where one or more arguments are search criteria strings that represents a regular expression, the first attempt is to convert the string criteria to numbers. For example, ".0" will convert to 0.0 and so on. If successful, the match will not be a regular expression match but a numeric match. However, when switching to a locale where the decimal separator is not the dot makes the regular expression conversion work. To force the evaluation of the regular expression instead of a numeric expression, use some expression that can not be misread as numeric, such as "." or ".\0" or "(?i).0".

## Examples:

Consider the following table

A B C
1 Product Name Sales Revenue
2 pencil 20 65
3 pen 35 85
4 notebook 20 190
5 book 17 180
6 pencil-case not not

In all examples below, ranges for calculation contain the row #6, which is ignored because it contains text.

### Simple usage

Formula Description Returns
=AVERAGEIF(B2:B6;"<35") Calculates the average for values of the range B2:B6 that are less than 35. Here the second row does not participate in the calculation. 19
=AVERAGEIF(B2:B6;"<"&MAX(B2:B6)) Calculates the average for values of the same range that are less than the maximum value of this range. Here the largest value (the second row) does not participate in the calculation. 19
=AVERAGEIF(B2:B6;">"&SMALL(B2:B6;1)) Calculates the average for values of the same range that are greater than the first smallest value of this range. Here the first smallest value (the fourth row) does not participate in the calculation. 25

### Using the Average_Range

Formula Description Returns
=AVERAGEIF(B2:B6;"<35";C2:C6) The function searches what values are less than 35 in the B2:B6 range, and calculates the average of corresponding values from the C2:C6 range. Here the second row does not participate in the calculation. 145
=AVERAGEIF(B2:B6;">"&MIN(B2:B6);C2:C6) The function searches what values from the range B2:B6 are greater than the least value in the B2:B6 range, and calculates the average of corresponding values from the C2:C6 range. Here the fourth row (where there is the least value in the range B2:B6) does not participate in the calculation. 113.3
=AVERAGEIF(B2:B6;"<"&LARGE(B2:B6;2);C2:C6) The function searches what values from the range B2:B6 are less than the second large value in the B2:B6 range, and calculates the average of corresponding values from the C2:C6 range. Here only the fourth row participates in the calculation. 180

### Using regular expressions

Formula Description Returns
=AVERAGEIF(A2:A6;"pen";B2:B6) The function searches what cells from the range A2:A6 contain only the word “pen”, and calculates the average of corresponding values from the B2:B6 range. Here only the second row participates in the calculation. The search is performed in the A2:A6 range, but the values are returned from the B2:B6 range. 35
=AVERAGEIF(A2:A6;"pen.*";B2:B6) The function searches what cells from the range A2:A6 begin with “pen” ending with any quantity of other characters, and calculates the average of corresponding values from the B2:B6 range. Here “pencil” satisfies the condition, and both, first and second rows participate in the calculation. 27.5
=AVERAGEIF(A2:A6;".*book.*";B2:B6) The function searches what cells from the range A2:A6 contain “book” starting and ending with any quantity of other characters, and calculates the average of corresponding values from the B2:B6 range. Here only third and fourth rows participate in the calculation. 18.5

### Using wildcard characters

The wildcard characters question mark (?), asterisk(*), or tilde (~) can be used in criteria. A question mark (?) matches any one character and an asterisk (*) matches zero or more characters of any kind.

Formula Description Returns
=AVERAGEIF(A2:A6;"*pen*";B2:B6) The function searches what cells from the range A2:A6 contain the word “pen” anywhere, and calculates the average of corresponding values from the B2:B6 range. Here the second, third and sixth row participate in the calculation. The search is performed in the A2:A6 range, but the values are returned from the B2:B6 range. 27.5

### Reference to a cell as a criterion

If you need to change a criterion easily, you may want to specify it in a separate cell and use a reference to this cell in the condition of AVERAGEIF function.

Formula Description Returns
=AVERAGEIF(A2:A6;".*"&E2&".*";B2:B6) where E2 = book The function searches what cells from the range A2:A6 contain a combination of characters specified in E2 starting and ending with any quantity of other characters, and calculates the average of corresponding values from the B2:B6 range. 18.5
=AVERAGEIF(B2:B6;"<"&E2;C2:C6) where E2 = 35 The function searches what cells from the range B2:B6 are less than the value specified in E2, and calculates the average of corresponding values from the C2:C6 range. 145

AVERAGEIF