Named ranges and statistics

Some­times, we need to make many sta­tis­ti­cal oper­a­tions and use many sta­tis­ti­cal func­tions in Excel. How can we do it?

Named ranges and statistics 

A fast and quick solu­tion is using name ranges. For exam­ple, we have the SUM func­tion that sums up all val­ues from Jan­u­ary (Fig. 1)

SUM function
Fig. 1 SUM function

How can we name ranges and use them for our advan­tage? Let’s start with a sim­ple exam­ple of the SUM func­tion. For the SUM func­tion, we have key­board short­cut which is Alt + =. This way Excel will put the sum for Jan­u­ary, which is a range form B2:B6 (Fig. 2)

=SUM(B2:B6)

Alt + = to put the sum
Fig. 2 Alt + = to put the sum

In the exam­ple above we had num­bers. How­ev­er, let’s go to anoth­er sheet where we have no num­bers. When we want to sum the num­bers from the pre­vi­ous sheet, it’s going to be a very tedious job, as we have to move between those two sheets and write prop­er val­ues. What’s more, the ref­er­ence we have while using val­ues form anoth­er sheet isn’t the eas­i­est to under­stand. What we can do is nam­ing the ranges. We just have to select a range in a col­umn and write a prop­er name in the Name Box (Fig. 3).

Naming the range
Fig. 3 Nam­ing the range

Now, when we are using the Alt + = short­cut under our val­ues, Excel will auto­mat­i­cal­ly refer to the Jan­u­ary range, which is B2:B6 (Fig. 4)

=SUM(Jan­u­ary)

Automatic reference
Fig. 4 Auto­mat­ic reference

As we know, Excel remem­bers names. It means that we can use the name in cal­cu­la­tion on a dif­fer­ent sheet. When I start writ­ing “ja” in anoth­er sheet, Excel will sug­gest the range name (Fig. 5). I can just accept it and sum the val­ues from January. 

Excel suggested the range name
Fig. 5 Excel sug­gest­ed the range name

As we can see, nam­ing ranges makes our work faster. We can also name a big­ger group of columns at one time. Just select the group you want. What’s impor­tant, when you’re select­ing your data, you have to select the top rows, and then the num­bers beneath them. Then, go to the For­mu­las tab, click on the Cre­ate from Selec­tion com­mand and select the Top Row check­box in the win­dow that appeared (Fig. 6)

Creating names from selection
Fig. 6 Cre­at­ing names from selection

Now, after we named our ranges, we can see that the Feb­ru­ary range is named Feb­ru­ary, the March range is named March and so on. When we move to anoth­er sheet, we can use those names (Fig. 7)

Excel is suggesting the new names
Fig. 7 Excel is sug­gest­ing the new names

How­ev­er, we can go one step fur­ther using the ref­er­ence to names. I have Jan­u­ary in cell B1. But, when I refer to cell B1, Excel won’t sim­ply con­vert the text from cell B1 to a range because it just text. It means that our func­tion won’t add any­thing. First of all, we have to lock the rows (B$1), then add the INDIRECT func­tion to our cal­cu­la­tion. Now, the INDIRECT func­tion changes the text into a ref­er­ence to the prop­er range (Fig. 8)

=SUM(INDIRECT(B$1))

INDIRECT function
Fig. 8 INDIRECT function

Now, when I drag the for­mu­la to the right, I will have sums from each indi­vid­ual month (Fig. 9)

=SUM(INDIRECT(F$1))

Sum from individual months
Fig. 9 Sum from indi­vid­ual months

When I copy the for­mu­la one cell low­er, I’m just chang­ing SUM into AVERAGE (Fig. 11)

=AVERAGE(INDIRECT(B$1))

SUM into AVERAGE
Fig. 1 SUM into AVERAGE

After I drag it to the right, I have prop­er results. I do the same with the remain­ing rows (Fig. 12)

=MIN(INDIRECT(B$1))

Changing the remaining names
Fig. 12 Chang­ing the remain­ing names

This way, we can quick­ly add more sta­tis­ti­cal oper­a­tions for many months using prop­er name ranges and the INDIRECT function. 

https://www.youtube.com/watch?v=E9APUsy4y‑E

Sum of top 3 values

How to cal­cu­late a sum or an aver­age of top 3 max­i­mal values?

Sum of top 3 values

In Lega­cy Excel, we can use the LARGE func­tion, in which we need to select the range for our cal­cu­la­tion. In the k argu­ment let’s write 1 (Fig. 1).

=LARGE($C$2:$C$33,1)

 LARGE function
Fig. 1 LARGE function

After enter­ing the func­tion, we have the largest val­ue (Fig. 2). 

The largest value
Fig. 2 The largest value

Now, we can copy the func­tion and add the sec­ond and the third largest val­ue (Fig. 3).

=LARGE($C$2:$C$33,1)+LARGE($C$2:$C$33,2)+LARGE($C$2:$C$33,3)

Adding more values
Fig. 3 Adding more values

This way we have the sum of 3 largest val­ues (Fig. 4). 

The sum of 3 largest values
Fig. 4 The sum of 3 largest values

This solu­tion, how­ev­er, is the least dynam­ic of all solu­tions we have. We can make this for­mu­la short­er by hard­cod­ing our val­ues. We have to write 1, 2 and 3 as an array (Fig. 5).

=LARGE($C$2:$C$33,{1;2;3})

The first, second and third value written as an array
Fig. 5 The first, sec­ond and third val­ue writ­ten as an array

This way Excel will return three largest val­ues. I’m using Dynam­ic Excel, so the results are spilled (Fig. 6).

Spilled results
Fig. 6 Spilled results

Now, that our val­ues have been hard-cod­ed, we can sum them by using the SUM func­tion. We can also use this func­tion to sum up aver­ages (Fig. 7). 

=SUM(LARGE($C$2:$C$33,{1;2;3}))

Summing up the largest values
Fig. 7 Sum­ming up the largest values

Our results (Fig. 8).

Results
Fig. 8 Results

This solu­tion, how­ev­er, is hard to mod­i­fy, so we can make it a bit more dynam­ic. We can use the ROW func­tion and select the first three rows from the sheet. This way our solu­tion is more dynam­ic (Fig. 9). 

=SUM(LARGE,($C$2:$C$33,ROW(A1:A3)))

A more dynamic solution
Fig. 9 A more dynam­ic solution

Results (Fig. 10). 

Results
Fig. 10 Results

But, in Lega­cy Excel, we should use the SUMPRODUCT func­tion instead of the SUM func­tion or use the Ctrl + Shift + Enter short­cut to put the for­mu­la into the cell (Fig. 11).

=SUMPRODUCT(LARGE,($C$2:$C$33,ROW(A1:A3)))

SUMPRODUCT function
Fig. 11 SUMPRODUCT function

And we have the result. In the sec­ond col­umn, in Dynam­ic Array Excel, we still need the SUM and LARGE func­tions, then the array which is the range we will look at. Then, we can use the SEQUENCE func­tion to cre­ate a sequence of prop­er num­bers, let’s take 3 (Fig. 12).

=SUM(LARGE($C$2:$C$33,SEQUENCE(3)))

Adding the SEQUENCE function
Fig. 12 Adding the SEQUENCE function

We have prop­er results (Fig. 13).

Results
Fig. 13 Results

This solu­tion is much more dynam­ic. Let’s change the num­ber into 5 (Fig. 14).

=SUM(LARGE($C$2:$C$33,SEQUENCE(5)))

3 changed into 5
Fig. 14 3 changed into 5

Here is the result (Fig. 15). 

Results
Fig. 15 Results

In Lega­cy Excel, we can also change one num­ber. Let’s change the num­ber of rows in the range from 3 to 5 (Fig. 16).

=SUMPRODUCT(LARGE($C$2:$C$33,ROW(A1:A5)))

A3 into A5
Fig. 16 A3 into A5

And we have the same result (Fig. 17).

Result
Fig. 17 Result

We have to remem­ber which ver­sion of Excel we have and which solu­tion we can use. 

https://www.youtube.com/watch?v=B2f4hvKBadk

Running Total

Some­times you need to cre­ate a run­ning total.

Run­ning Total 

The most ver­sa­tile solu­tion I know is using the SUM func­tion and a dynam­ic range which in our case is $B$2:B2. How­ev­er, the most impor­tant fact is that we have to lock (F4 key) the first part of this range (Fig. 1).

=SUM($B$2:B2)

SUM function
Fig. 1 SUM function

When we copy our for­mu­la down, we can see that the first part of the range will always refer to cell B2, but the sec­ond one will go down as we drag the for­mu­la. This way, we have a range that expands as we go down. It’s the most ver­sa­tile solu­tion (Fig. 2). 

=SUM($B$2:B4)

An expanding range
Fig. 2 An expand­ing range

This solu­tion has prob­lems with Excel tables. When we sum from F2 to F2 cells and press F4 key to lock it, every­thing looks fine. How­ev­er, when we add new data to our table, the last cell will expand because it refers to the whole col­umn July -> August (Fig. 3). 

 Adding a new value
Fig. 3 Adding a new value

In this case, we have to mod­i­fy our range. Instead of cell F2 we can use the table nomen­cla­ture. When we click cell F2, Excel will refer to this table row, where @ means this table row, and Income means the col­umn we are refer­ring to (Fig. 4). 

=SUM($F$2:[@Income])

 Range modification
Fig. 4 Range modification

We have to over­write all cells (Fig. 5). 

Overwriting cells
Fig. 5 Over­writ­ing cells

This way we got prop­er results. We can check if it works prop­er­ly by adding some new rows. After we added two rows, the results are still cor­rect (Fig. 6).

Correct formula operation
Fig. 6 Cor­rect for­mu­la operation

The only draw­back of this for­mu­la is that we won’t see the whole range because Excel won’t select the whole range. The­o­ret­i­cal­ly, the last cell should include the whole col­umn but Excel selects only the first and the last row (Fig. 7).

=SUM($F$2:[@Income])

The formula in the last cell
Fig. 7 The for­mu­la in the last cell

We won’t see it, but Excel will. We have to remem­ber that Excel will cre­ate a prop­er ref­er­ence and we can cre­ate a prop­er run­ning total, even in Excel tables. 

https://www.youtube.com/watch?v=djsC6ybbuWk

SUM and AVERAGE with condition — SUMIF and AVERAGEIF functions

Today, we are going to count sums and aver­ages with con­di­tions. In our first exam­ple, we have a small table where we sell fruit. We want to sum up the whole weight and aver­age weight for a sin­gle cell. To sum the val­ues with con­di­tions we can use the SUMIF func­tion. In the first argu­ment, we write the range where we need to check our con­di­tion and lock it by press­ing the F4 key. In the sec­ond argu­ment, which is cri­te­ria, we just put a ref­er­ence to a sin­gle cell with the prod­uct name. We don’t have to lock the cell because while copy­ing the for­mu­la down, we want the fruit names to change. The third argu­ment of the func­tion is the sum range. It is closed in square brack­ets which means that it is not nec­es­sary write it, how­ev­er we will do it. We want to sum val­ues from the Weight col­umn. Then the F4 key to lock it (Fig. 1).

SUM and AVERAGE with con­di­tion — SUMIF and AVERAGEIF functions

=SUMIF($B$2:$B$10,F2,$C$2:$C$10)

 SUMIF function
Fig. 1 SUMIF function

After all argu­ments have been writ­ten, let’s look at our ranges and try to under­stand how the SUMIF func­tion works. The func­tion will look at each cell in the Prod­uct col­umn and check if they meet the con­di­tions. In the first cell, we have apples, which means that the con­di­tion is met. In such a sit­u­a­tion, the func­tion will keep on look­ing for the third argu­ment placed in the same row. If it finds it, it will add the val­ue. If there is anoth­er cell that meets the same con­di­tion, it will also add the val­ue from the same row (Fig. 2).

Finding conditions
Fig. 2 Find­ing conditions

Our for­mu­la looks as fol­lows (Fig. 3).

=SUMIF($B$2:$B$10,F2,$C$2:$C$10)

SUMIF formula
Fig. 3 SUMIF formula

After enter­ing and copy­ing down, we can see how many apples, pears and oranges we have sold (Fig. 4).

Results
Fig. 4 Results

Now, let’s try to count the aver­age weight of a sin­gle sale. We just have to copy our SUMIF for­mu­la in the edit mode and paste it into the cell in the Aver­age col­umn. The SUMIF and the AVERAGE func­tions have the same argu­ments. We just change the func­tion name. We can see that the name of the third argu­ment changed from sum range into aver­age range, how­ev­er it is still the same Weight col­umn (fig. 5).

=AVERAGEIF($B$2:$B$10, F2,$C$2:$C$10)

Name change
Fig. 5 Name change

After enter­ing and copy­ing it down, we have our results (Fig. 6).

Fig. 6 AVERAGEIF counted

Let’s move on to our sec­ond exam­ple. We want to check here what would hap­pen if we give the AVERAGEIF or SUMIF func­tion just two argu­ments. In this exam­ple, we just want to count the aver­age sale. We are start­ing with a sim­ple AVERAGE func­tion with­out con­di­tions, so with­out IFs, for the Sales col­umn (Fig.7).

=AVERAGE(C2:C11)

Simple AVERAGE function
Fig. 7 Sim­ple AVERAGE function

After enter­ing it, we have the aver­age (Fig. 8). 

Average
Fig. 8 Average

Now, let’s count the aver­age with a con­di­tion. We don’t want to count the val­ue if it equals 0. Let’s write the AVERAGEIF func­tion, select the same range and write the con­di­tion. We open dou­ble quotes and write <>0 and close it. When we don’t write the third argu­ment in the AVERAGEIF or SUMIF func­tions, Excel takes the first range as the range in which we are doing our cal­cu­la­tions. In this sit­u­a­tion Excel does­n’t take emp­ty cells and cells with 0 into cal­cu­lat­ing the aver­age. After enter­ing it, we can see dif­fer­ent results. It’s because our con­di­tion was trig­gered in the AVERAGEIF func­tion, which means that Excel did not count the cells with 0 (Fig. 9). 

Different results
Fig. 9 Dif­fer­ent results

When we delete the val­ue with 0, we can see that the AVERAGE and AVERAGEIF func­tion return the same results. It’s because func­tions such as AVERAGE, SUMS, MIN and MAX do not con­sid­er emp­ty cells (Fig. 10).

Results without empty cells
Fig. 10 Results with­out emp­ty cells

How­ev­er, when we put 0 into the cells, the AVERAGE func­tion will count it which means that the aver­age will go down (Fig. 11).

Fig. 11 Results with zeros

Sum­ming up, a sim­ple AVERAGE func­tion counts cells with 0, and the AVERAGEIF func­tion does­n’t due to the condition. 

In Excel, in most sit­u­a­tions, emp­ty cells are treat­ed as 0. 

https://www.youtube.com/watch?v=5h7hVwUjoWA