Explanation

In this example, the goal is to calculate an average of the quiz scores in columns C, D, E, and F for each person. However, the result needs to ignore any zeros that appear in the data. This formula can be easily solved with the AVERAGEIF function or the AVERAGEIFS function . It can also be solved with a combination of FILTER and AVERAGE . See below for details.

AVERAGE function

The standard way to calculate an average in Excel with a formula is to use the AVERAGE function . You can see the results from AVERAGE in column H of the worksheet shown. The formula in H5, copied down, is:

=AVERAGE(C5:F5)

However, while AVERAGE will ignore text values and empty cells , it will not ignore zero values like those in cell E6, D11, and F15. An easy solution in this case is to switch to the AVERAGEIF or AVERAGEIFS function instead.

AVERAGEIF function

The AVERAGEIF function calculates the average of the numbers in a range that meet a single condition. To apply criteria, AVERAGEIF supports logical operators (>,<,<>,=) and wildcards (*,?) for partial matching. In this case, we can use AVERAGEIFS to solve this problem by excluding zero values with the criteria “<>0”. The formula in cell I5, copied down, is:

=AVERAGEIF(C5:F5,"<>0")

Note that the average returned by AVERAGEIF is higher than the average calculated by AVERAGE in rows 6, 11, and 16. This is the effect of excluding Quiz scores equal to zero.

AVERAGEIFS function

The AVERAGEIFS function works like AVERAGEIF, except it is designed to apply multiple criteria. We can use AVERAGEIFS to solve this problem by excluding zero values with the criteria “<>0” like this:

=AVERAGEIFS(C5:F5,C5:F5,"<>0")

One difference between AVERAGEIFS and AVERAGEIF is that the average range is always the first argument with AVERAGEIFS, and is required. With AVERAGEIF the average range is the last argument and is optional. For more details on the syntax of AVERAGEIFS with many more examples, see this page .

AVERAGE with FILTER

Another way to solve this problem in the current version of Excel is to use the AVERAGE function together with the FILTER function in a formula like this:

=AVERAGE(FILTER(C5:F5,C5:F5<>0))

This is a newer and more flexible way to handle this problem. The FILTER function is configured to select only quiz scores that are not equal to zero like this:

=FILTER(C5:F5,C5:F5<>0)

The result from FILTER is an array that contains only non-zero quiz scores. This array is returned directly to the AVERAGE function, which calculates the average. Zero values never make it into the AVERAGE function to start with. For more on FILTER, see this page .

Explanation

In this example, the goal is to create a formula that calculates an average salary by department, where data is an Excel Table in the range B5:D16. The solution shown requires three general steps:

  1. Create an Excel Table called data
  2. List unique departments with the UNIQUE function
  3. Calculate averages with the AVERAGEIFS function

Create the Excel Table

One of the key features of an Excel Table is its ability to dynamically resize when rows are added or removed. To create a table, place the cursor in the data and use the keyboard shortcut Control + T . Then name the table “data”.

Video: How to create an Excel table

The table will now automatically expand or contract when new data is added or removed.

List unique departments

The next step is to list unique department names starting in cell F5. For this we use the UNIQUE function like this:

=UNIQUE(data[Department]) // unique departments

The result from UNIQUE is an array with 5 values like this:

{"Marketing";"IT";"Finance";"HR";"Sales"}

This array lands as a spill range in cell F5 and lists all of the unique department names in the Department column of the table. Note the UNIQUE function will continue to return a current list of unique departments when data changes.

Video: Intro to the UNIQUE function

Video: What is an array ?

Calculate average

We now have what we need to calculate the average for each department. To do this, we only need one condition: the department name. A good way to do this is to use the AVERAGEIFS function, which can calculate averages based on one or more criteria. Because we already have unique departments in column F, we can reference the list directly. The formula in G5 is:

=AVERAGEIFS(data[Salary],data[Department],F5#)

The first argument in AVERAGEIFS is average_range . This is the range that contains numbers to average. In this example, this is the Salary column in the table:

=AVERAGEIFS(data[Salary],

The next two arguments specify the criteria. The criteria range is data[Department] , and the criteria is the spill range :

=AVERAGEIFS(data[Salary],data[Department],F5#)

Because the spill range F5# contains 5 values, the AVERAGEIFS function returns 5 averages in an array like this:

{53333.3333333333;68500;71000;43500;59000}

Each number in the array is the average calculated for a given department. These values spill into the range G5:G7.

When data changes

The key advantage to using a Excel table with the UNIQUE function is that the formula responds instantly to changes in the data. If new rows are added that refer to existing departments, the spill range returned by UNIQUE remains unchanged, and AVERAGEIFS calculates a new set of averages. If new rows are added that include new departments, or if existing departments values are changed, these changes are captured by the UNIQUE function, which expands the spill range in F5 as needed. If rows are deleted from the table, the spill range contracts as needed. In all cases, the spill range represents the current list of unique departments, and the AVERAGEIFS function uses this list to calculate averages.

Legacy Excel

In older versions of Excel without the UNIQUE function, the approach needs to be modified slightly. Instead of using the UNIQUE function to automatically extract unique departments, the departments can be entered manually. Then the formula in G5 becomes:

=AVERAGEIFS(data[Salary],data[Department],F5)

Note the criteria is no longer a spill range. This formula can then be copied down to return an average for each department.

Note: there are ways to extract unique values in older versions of Excel, but they are more complicated.

Pivot Table option

A pivot table would also be a good way to solve this problem , and would provide additional features. However, one drawback is that pivot tables need to be refreshed to show the latest data. Formulas, on the other hand, update instantly when data changes.