Is this Time between Working Hours

How can we check whether the giv­en time is between the work­ing hours or not?

Is this Time between Work­ing Hours 

In our case, work starts at 9:30 AM, and ends at 5:30 PM. Our giv­en time, which is 6:47 isn’t between the work­ing ours. When we change it, we can see that the col­or changed into red and ‘No’ turned into ‘Yes’, which means that now the time is between the work­ing ours (Fig. 1)

Our given time is between the working hours
Fig. 1 Our giv­en time is between the work­ing hours

How­ev­er, when we go to ear­li­er hours we can see that the details changed (Fig. 2)

 Our given time isn't between the working hours
Fig. 2 Our giv­en time isn’t between the work­ing hours

How can we check our time? The sim­plest solu­tion is using the MEDIAN func­tion that returns the val­ue in the mid­dle. Since all our times are in one row, we can just select the range (Fig. 3)

=MEDIAN(B2:D2)

Selecting the whole range
Fig. 3 Select­ing the whole range

If our data is in sep­a­rate cells, we can just add those cells one by one (Fig. 4)

=MEDIAN(B2,C2,D2)

Selecting cells one by one
Fig. 4 Select­ing cells one by one

And just like that we receive the val­ue in the mid­dle. If this val­ue is equal to the giv­en time, it means it’s between the work­ing hours. What’s curi­ous about our medi­an is the fact that it won’t cross our thresh­olds, i.e. our start­ing time and fin­ish­ing time. It means that the small­est val­ue our MEDIAN func­tion can show is the start­ing hour, and the largest is the fin­ish­ing hour (Fig. 5)

The smallest value
Fig. 5 The small­est value

Now, we have to check whether our time is between the work­ing hours. We have to com­pare the func­tion to the giv­en time (Fig. 6)

=MEDIAN(B2,C2,D2)=B2

Comparing
Fig. 6 Comparing 

As we can see, our log­i­cal test returned FALSE (Fig. 7)

FALSE
Fig. 7 FALSE

If the log­i­cal test returns TRUE, it means that the giv­en time is between the work­ing ours (Fig. 8)

TRUE
Fig. 8 TRUE

The answers we have are log­i­cal. Let’s make them more human like by adding the IF func­tion to our MEDIAN log­i­cal test and some sim­ple text like ‘Yes’ and ‘No’ (Fig. 9)

=IF(MEDIAN(B2,C2,D2)=B2,“Yes”,“No”)

Adding the IF function
Fig. 9 Adding the IF function

We can see that we can check whether our giv­en time is between the work­ing ours or no (Fig. 10)

Human answer
Fig. 10 Human answer

https://www.youtube.com/watch?v=IVZIY6-78sc

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