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

Median with condition

If you want to cal­cu­late a medi­an val­ue with a con­di­tion, you can­not use a sim­ple MEDIAN func­tion because it needs only num­bers. We have to use anoth­er func­tion that will reduce the num­ber of wages we look at. 

Medi­an with condition

Typ­i­cal­ly, when we want to add a con­di­tion to anoth­er func­tion we can use the IF func­tion and make a log­i­cal test. In our case, the log­i­cal test will be sim­ple. We just have to check each cell in the Depart­ment col­umn whether it’s equal to the name of the Depart­ment we look at now. Since we are com­par­ing a sin­gle cell to a whole range, Excel will check every sin­gle cell in a giv­en range (Fig. 1). 

Comparing a single cell to a range of cells
Fig. 1 Com­par­ing a sin­gle cell to a range of cells

If a cell in the range is equal to the tar­get cell, we want the func­tion to return prop­er wages, i.e. the wages from the same row. If the log­i­cal test result is FALSE, we want to have some­thing that the MEDIAN func­tion will ignore which, in most cas­es, is an emp­ty text string. How­ev­er, I want to show you that the IF func­tion real­ly returns some­thing so let’s write “no” (Fig. 2).

=IF($B$2:$B$13=E2,$C$2:$C$13,“no”)

IF function
Fig. 2 IF function

We can see that Dynam­ic Array Excel spilled the results and we have wages only for the depart­ments we chose. For oth­er depart­ments we have ‘no’. Now, we can put this func­tion into the MEDIAN func­tion (Fig. 3).

=MEDIAN(IF($B$2:$B$13=E2,$C$2:$C$13,“no”))

MEDIAN function with IF function
Fig. 3 MEDIAN func­tion with IF function

Now, we’ve cal­cu­lat­ed the medi­an val­ue with a con­di­tion for each depart­ment. We can see that for HR it’s $4,000, for IT it’s $8,100 and for Mar­ket­ing it’s $5,900. Those are the mid­dle val­ues for each depart­ment (Fig. 4). 

Median values for each department
Fig. 4 Medi­an val­ues for each department

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

Median vs Average wages

Why do politi­cians lie to us when talk­ing about aver­age wages? Let’s ask Excel.

Medi­an vs Aver­age wages 

Let’s cal­cu­late some aver­age. The AVERAGE func­tion is a sim­ple func­tion that sums up val­ues from all cells in a range and divides it by the num­ber of cells with val­ues (Fig. 1). 

=AVERAGE(D2:D12)

AVERAGE function
Fig. 1 AVERAGE function

In our exam­ple, the aver­age is $2,900. It means that 8 peo­ple earn less, which gives us around 80% of all peo­ple. It’s impor­tant that one per­son at the bot­tom earns much more than the rest and affects our aver­age more than oth­er peo­ple (Fig. 2). 

Average wages
Fig. 2 Aver­age wages

If we want to talk more pre­cise­ly about the dis­tri­b­u­tion of those wages, we can use the MEDIAN func­tion which returns the val­ue in the mid­dle (Fig. 3). 

=MEDIAN (D2:D12)

MEDIAN function
Fig. 3 MEDIAN function

Since our val­ues are sort­ed, we can clear­ly see the mid­dle val­ue, which is $2,000. We can see that 50% of peo­ple earn this val­ue or less and 50% of peo­ple earn this val­ue or more (Fig. 4). 

Median value
Fig. 4 Medi­an value

If we have an even num­ber of val­ues and there are two mid­dle val­ues, the MEDIAN func­tion cal­cu­lates the aver­age of those two val­ues near the mid­dle line (Fig. 5).

=MEDIAN(G2:G11)

MEDIAN function with even number of cells
Fig. 5 MEDIAN func­tion with even num­ber of cells

We can see that it cal­cu­lat­ed the mid­dle val­ue (Fig. 6). 

Median value
Fig. 6 Medi­an value

If we add one zero to cell D12 which belongs only to one per­son, we can see that the aver­age hit $11,900. It means that only one per­son earns more that the aver­age. On the oth­er hand, the medi­an val­ue stayed the same. It’s very impor­tant to know this dif­fer­ence (Fig. 7). 

Higher average
Fig. 7 High­er average

I also want to add one col­umn with the RAND func­tion (Fig. 8).

=RAND()

RAND function
Fig. 8 RAND function

I want to show you that we don’t have to have our data sort­ed to use the MEDIAN func­tion (Fig. 9). 

Random order, median number the same
Fig. 9 Ran­dom order, medi­an num­ber the same

https://www.youtube.com/watch?v=wLV-xl1735s

Count words in a cell 

How can we count the num­ber of words in a cell?

Count words in a cell 

We can count the num­ber of spaces between words. Let’s have a look at our first exam­ple. In cell A2, we have spaces between words and at the end of each line, so the case is sim­ple. We can use the LEN func­tion here that counts the length of text (Fig. 1).

=LEN(A2)

LEN function
Fig. 1 LEN function

Now, that we have the num­ber of signs, we can remove spaces. We can use the SUBSTITUTE func­tion here. This func­tion will change the old text, which in our case is a space into new text. And since we want to remove spaces, our new text will be an emp­ty text string writ­ten in dou­ble quotes (Fig. 2). 

=SUBSTITUTE(A2,” “,“”)

Fig. 2 SUBSTITUTE function

We can see that the func­tion removed all spaces from our text (Fig. 3). 

No spaces
Fig. 3 No spaces

Now, we can count the length of the text with­out spaces using the LEN func­tion (Fig.4).

=LEN(SUBSTITUTE(A2,” “,“”)

Counting the text length
Fig. 4 Count­ing the text length

Now we know the length (Fig. 5).

Text length
Fig. 5 Text length

Since we know how many spaces there are, we can use the for­mu­la to count the words. We just have to sub­tract this for­mu­la from the pre­vi­ous one (Fig. 6). 

=LEN(A2)-LEN(SUBSTITUTE(A2,” “,“”))

 Subtracting formulas
Fig. 6 Sub­tract­ing formulas

And we have the num­ber of spaces (Fig. 7).

Number of spaces
Fig. 7 Num­ber of spaces

We still have to remem­ber that to count the num­ber of words, we have to add 1 because there is only one space between two words (Fig. 8).

=LEN(A2)-LEN(SUBSTITUTE(A2,” “,“”))+1

One space between two words
Fig. 8 One space between two words

Now we know the num­ber of words in each cell. 

Number of words
Fig. 9 Num­ber of words

If we want to count words from all cells, we can use almost the same for­mu­la. The only thing we have to change is the range which now will be from A2 to A4 (Fig. 10). 

=LEN(A2:A4)-LEN(SUBSTITUTE(A2:A4,” “,“”))+1

Adding a new range
Fig. 10 Adding a new range

In Dynam­ic Array Excel, the results will be spilled, how­ev­er we can sum the up (Fig. 11).

Split results
Fig. 11 Split results

In the pre­vi­ous ver­sion of Excel we can use the the SUMPRODUCT func­tion (Fig. 12). 

=SUMPRODUCT(LEN(A2:A4)-LEN(SUBSTITUTE(A2:A4,” “,“”))+1

 SUMPRODUCT function
Fig. 12 SUMPRODUCT function

Final results (Fig. 13).

Final result
Fig. 13 Final result

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

Count cells with text, COUNTIF function and wildcards

What to do if we want to count cells with text?

Count cells with text, COUNTIF func­tion and wildcards 

For me, the sim­plest solu­tion is using the COUNTIF func­tion and wild­cards. We have an aster­isk that rep­re­sents any text string, even an emp­ty one. We also have a ques­tion mark that rep­re­sents just one sin­gle sign (Fig. 1). 

 Wildcards
Fig. 1 Wildcards

Let’s start with count­ing all cells with text. First, we have to select a prop­er range and press the F4 key to lock it. Then, we have to write an aster­isk in dou­ble quotes so that it counts any text string, even an emp­ty one (Fig. 2). 

=COUNTIF($B$2:$B$11,”*”)

Proper range and an asterisk
Fig. 2 Prop­er range and an asterisk

And just like that we have 4 cells with text. In cell B6 I have past­ed an emp­ty text string and even if we see noth­ing, Excel counts it as a cell with an emp­ty text string. But, what can we do if we don’t want to count emp­ty text strings? We can copy our for­mu­la and add a ques­tion mark next to the aster­isk. It does­n’t mat­ter if it’s before or after the sign (Fig. 4).

=COUNTIF($B$2:$B$11,”?*”)

Formula modification
Fig. 4 For­mu­la modification

This way we count­ed all cells that have at least one sign (Fig. 5). 

Results
Fig. 5 Results

If we want to count all not emp­ty cells, we can change our cri­te­ria to the <> signs (Fig. 6). 

=COUNTIF($B$2:$B$11,”<>”)

Adding <> signs
Fig. 6 Adding <> signs

How­ev­er, the sim­plest solu­tion in most cas­es would be using the COUNTA func­tion, where we don’t have to wor­ry about any cri­te­ria (Fig. 7). 

=COUNTA(B2:B11)

COUNTA function
Fig. 7 COUNTA function

https://www.youtube.com/watch?v=8ywd-H7ALc8