Working hours and wages. Night shifts | Excel Tips #28

When we want to count the num­ber of work­ing hours in Excel, we have z prob­lem with night shifts. Let’ start with a human approach and day shift. 

Work­ing hours and wages. Night shifts | Excel Tips #28

In a day shift, the end time is larg­er that the start time, so we can just sub­tract one num­ber from anoth­er (Fig. 1). 

=(B2-A2)

Subtracting one number from another
Fig. 1 Sub­tract­ing one num­ber from another

After enter­ing and copy­ing down, we can see we have prop­er results for day shifts. In night shifts, when the end time is small­er that the start time, it does’t show us any time, only hash­tags. It hap­pened so because Excel refused to show any neg­a­tive dates or time. Even if Excel showed us neg­a­tive results, they would be wrong (Fig. 2). 

No negative time
Fig. 2 No neg­a­tive time

First, we have to remem­ber that Excel stores time as num­bers, i.e. the part of the day that has passed. When we look at the first hour, which is 8 am, it means that one-third of the day has passed. When we go to the Time bar, and press Gen­er­al, we can see that the time will be giv­en in dec­i­mal num­bers (Fig. 3), (Fig. 4). 

 General Time
Fig. 3 Gen­er­al Time 
Time in decimal numbers
Fig. 4 Time in dec­i­mal numbers

When we want to con­vert Excel time into human time, we have to mul­ti­ply it by 24 (Fig. 5). 

=(B2-A2)*24

Excel time into human time
Fig. 5 Excel time into human time

Now, we can see that the we can eas­i­ly read the val­ues. Remem­ber to have the Gen­er­al option select­ed. There are neg­a­tive val­ues for the night shift. What’s more, they are wrong, as there are only 8 hours from 8 pm to 4 am in row num­ber 3, not 16. 

Negative and wrong results
Fig. 6 Neg­a­tive and wrong results

It means that this human approach won’t work in Excel, so let’ delete the results. What can work here is a func­tion called MOD, which is also quite short. In the MOD func­tion, we also use the human approach, which means that from the end time we sub­tract the start time. How­ev­er, the MOD func­tion needs a divi­sor, i.e. 1, which means that it is one whole day (Fig. 7)

=MOD(B2-A2,1)

Divisor
Fig. 7 Divisor

Now, we can see that our results in day and night shifts are cor­rect. We can final­ly cal­cu­late the wages by mul­ti­ply­ing the num­ber of work­ing hours by the hourly wage (Fig. 8). 

=C2*$F$2

Calculating wages
Fig. 8 Cal­cu­lat­ing wages

As we can see the results are wrong again. Let’s have a look at row num­ber two. There are 8 hours, but Excel does­n’t mul­ti­ply 8 hours times 40$, but one third of a day times 40$, which gives us around $13 (Fig. 9).

Wrong results
Fig. 9 Wrong results

We want to have prop­er wages, so we need to mul­ti­ply it by 24 hours (Fig. 10).

=C2*$F$2*24

Multiplying by 24
Fig. 10 Mul­ti­ply­ing by 24

Now, we have prop­er wages (Fig. 11). 

Proper wages
Fig. 11 Prop­er wages

If you don’t need pre­ci­sion big­ger than 15 min­utes, just use num­bers instead of Excel time. I’m advis­ing it to you from my own experience. 

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

VLOOKUP all matches and combine them

Some­times, we want to find all the matched val­ues and com­bine them into one text string, i.e., we want o VLOOKUP all matched val­ues and then com­bine them.

VLOOKUP all match­es and com­bine them

I will give you two solu­tions. The first solu­tion will work from Excel 2019, and the sec­ond one will work in Dynam­ic Array Excel. First of all, we have to check if our rep­re­sen­ta­tive comes from the coun­try we are look­ing at. Let’s take a rep­re­sen­ta­tive from Spain. There is only one rep­re­sen­ta­tive of Spain in our exam­ple. We can check it using the IF func­tion, where we have to cre­ate a prop­er log­i­cal test. We have to take into con­sid­er­a­tion the Coun­try col­umn, so let’s write it by select­ing the col­umn and click­ing the F4 key to lock it. Then, we have to check if the val­ues from the col­umn are equal to just one coun­try (Spain) by click­ing the cell with the name of Spain (D4). It means that cell D4 will be com­pared to each cell from the Coun­try col­umn indi­vid­u­al­ly (Fig. 1)

Comparison to each cell
Fig. 1 Com­par­i­son to each cell

The log­i­cal test will return TRUE only in the case of Spain. In the case of oth­er coun­tries, it will return FALSE. If the log­i­cal test is in an array, we can choose prop­er, cor­re­spond­ing result as a return val­ue. We just need to select an array of the same size, which, in our case, is the Rep­re­sen­ta­tive col­umn. Then, we click the F4 key to lock it. When the log­i­cal test returns FALSE, we want to see an emp­ty text string, so we have to write a space in dou­ble quotes. Let’s close the func­tion with a paren­the­sis (Fig. 2)

=IF($A$2:$A$14=D2,$B$2:$B$14,” ”)

IF function
Fig. 2 IF function

Since I’m work­ing in Dynam­ic Array Excel, Excel spills the results to the whole col­umn (Fig. 3).

IF function results
Fig. 3 IF func­tion results

We can see that we have only one rep­re­sen­ta­tive of Spain, and many emp­ty cells. If we want to find the remain­ing rep­re­sen­ta­tives, we have to join the infor­ma­tion we have. To do that, we can use the TEXTJOIN func­tion, which is avail­able from Excel 2019. We have to give the func­tion a delim­iter. Let’s write a com­ma and a space in dou­ble quotes. Then, we have to decide whether we want to ignore emp­ty cells. We def­i­nite­ly do, so let’s write TRUE, or its short­er ver­sion, which is 1. Then, we just have to close the whole for­mu­la once again (Fig. 4).

=TEXTJOIN(“, “,1,IF($A$2:$A$14=D2,$B$2:$B$14,” ”))

TEXTJOIN function
Fig. 4 TEXTJOIN function

After enter­ing the for­mu­la and copy­ing it down, we have our results (Fig. 5). 

TEXTJOIN function results
Fig. 5 TEXTJOIN func­tion results

When we are in Dynam­ic Array Excel, we can use the FILTER func­tion instead of the IF func­tion. This func­tion just fil­ters our data. Let’s write FILTER func­tion. Our data is locat­ed only in one col­umn, which is the Rep­re­sen­ta­tive col­umn, so we have write the range and lock it with the F4 key. Then, we have to cre­ate an array of the same size with log­i­cal val­ues of TRUE or FALSE. It means that we have to cre­ate the same log­i­cal test as we did with the IF func­tion. Let’s add the Coun­try col­umn (A2:A14), F4 key to lock it, then an equal sign to check whether any of the val­ue from the Coun­try col­umn is equal to Spain (D2). After clos­ing it, we have a ready for­mu­la (Fig. 6).

=FILTER($B$2:$B$14,A$2:$A$14,=D2)

FILTER function
Fig. 6 FILTER function

We can see that Spain has only one rep­re­sen­ta­tive, but Poland has got three (Fig. 7). 

FILTER function results
Fig. 7 FILTER func­tion results

That’s why we have to put our FILTER func­tion results into the TEXTJOIN func­tion. And, once again, we have to write a com­ma and a space in dou­ble quotes. Then, we have to decide that we want to ignore emp­ty cells and close the whole for­mu­la (Fig. 8)

=TEXTJOIN(“, “,1,FILTER($B$2:$B$14,A$2:$A$14=D2))

TEXTJOIN with FILTER function
Fig. 8 TEXTJOIN with FILTER function

We can see that the results in the first solu­tion and in the sec­ond solu­tion are the same (Fig. 9)

Results
Fig. 9 Results

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

Count all cells containing certain text | Excel Tips #27

Some­times, we need to count all cells con­tain­ing cer­tain text. In our exam­ple, it will be a prod­uct name. 

Count all cells con­tain­ing cer­tain text | Excel Tips #27

Let’s assume that we want to count all cells con­tain­ing the name of eggs. In the In the bas­ket col­umn, we have cells where the name of eggs appears at the begin­ning, in the mid­dle, or at the end of the text. Some­times, the cell con­tains only eggs (Fig. 1). 

Eggs in many locations
Fig. 1 Eggs in many locations

In Excel, it’s a sim­ple task because we have the COUNTIF func­tion, where we can use wild­cards. Let’s start writ­ing the func­tion. We have to select a range, which is the In the bas­ket col­umn, F4 to lock it, then let’s write cri­te­ria. If we want to count only cells that con­tain only the word eggs, we would write D2 in the cri­te­ria, how­ev­er, we want to count the cells which con­tain eggs and oth­er prod­ucts as well. That’s why we have to add a wild­card, i.e. an aster­isk (*) that replaces any num­ber of char­ac­ters, even an emp­ty text string. So, we’re putting the aster­isk in dou­ble quotes, then we’re writ­ing an amper­sand to com­bine it with eggs, then D2, where eggs are. As we can see, eggs can be placed also at the end, that’s why we have to com­bine it with anoth­er aster­isk in dou­ble quotes. Our cri­te­ria look as fol­lows (Fig. 2). 

=COUNTIF($B$2:$B$20,”*”&D2&”*”

Our criteria
Fig. 2 Our criteria

When we press the F9 key, we can see how our cri­te­ria real­ly look (Fig. 3). 

=COUNTIF($B$2:$B$20,”*Eggs*”

Our criteria after F9
Fig. 3 Our cri­te­ria after F9

By press­ing Ctrl + Z, let’s return to the pre­vi­ous ver­sion, and close our for­mu­la (Fig. 4). 

=COUNTIF($B$2:$B$20,”*”&D2&”*”)

Our whole formula
Fig. 4 Our whole formula

After enter­ing and copy­ing the for­mu­la down, we can see how many cells con­tain cer­tain prod­uct (Fig. 5). 

Results
Fig. 5 Results

https://www.youtube.com/watch?v=Yv7Uiat–cs

How to create a star rating in Excel

When we want to cre­ate a star rat­ing in Excel, there is at least one pos­si­ble solution.

How to cre­ate a star rat­ing in Excel in 3 minutes

We want to use stars from the Unichars, Uni­code. We just need to use the UNICHAR func­tion and a prop­er uni­code (Fig. 1).

Fig. 1 UNICHAR function

In old­er ver­sions of Excel, copy­ing those stars from the Inter­net should also work. We just have to have the signs we want to repeat. As we can see, we already have num­ber rat­ing. The biggest num­ber is five, and the low­est is zero and there is a prop­er num­ber of stars next to them. Let’s delete them how­ev­er, and start cre­at­ing a prop­er for­mu­la from the begin­ning. We start with the REPT func­tion. We write G4 because our text string is there, then press the F4 key to lock it. When we want to add the num­ber of times we want to repeat, we write C2, which is in the Rat­ing col­umn. Then, we close the for­mu­la (Fig. 2).

=REPT($G$4,C2)

Fig. 2 REPT function

We can copy it down and we have our black stars (Fig. 3). 

Fig. 3 Start rating

How­ev­er, we assumed that we always want to have five stars. It means that if the rat­ing is low­er than five, we have to add some white stars. In such a case, we just have to com­bine one REPT func­tion with anoth­er REPT func­tion using an amper­sand. In the sec­ond REPT func­tion, we have to write G5 and press the F4 key to lock it. In the place on the num­ber of repeats, we have to write five minus C2 - a cell from the Rat­ing column. 

=REPT($G$4,C2) &REPT($G$5,5-C2)

Fig. 4 Two REPT func­tions com­bined with an ampersand 

When we tell the REPT func­tion to repeat zero times, it won’t repeat any­thing, i.e. it will return an emp­ty text string. In the case of a 5‑star rat­ing, we have five stars. How­ev­er, then it comes to a cell with zero, the REPT func­tion treats it the same as an emp­ty cell in most sit­u­a­tions (Fig. 5).

Fig. 5 Star rat­ing results

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

Highlight weekends with conditional formatting

Some­times, we want to high­light rows of week­ends. In such a case, we can use con­di­tion­al for­mat­ting but, first of all, we have to cre­ate a for­mu­la which will return TRUE for Sat­ur­day and Sun­day, i.e. week­end days. We can start with the WEEKDAY func­tion which will return the num­ber of days in a week (Fig.1).

High­light week­ends with con­di­tion­al formatting

=WEEKDAY(A2)

WEEKDAY function
Fig. 1 WEEKDAY function

We have our results. How­ev­er, this numer­a­tion assigns num­ber 7 to Sat­ur­day, and num­ber 1 to Sun­day. It’s not prop­er from our per­spec­tive. We have to mod­i­fy it by chang­ing the week day num­ber sequence. The best option we can choose is num­ber 1 for Mon­days and num­ber 7 for Sat­ur­days. That’s why we have to write 2 in the sec­ond WEEKDAY func­tion argu­ment (Fig.2).

=WEEKDAY(A2,2)

Selecting the correct option
Fig. 2 Select­ing the cor­rect option

And now, Sat­ur­day is 6 and Sun­day is 7. We can clear­ly see that week­ends are num­bers big­ger that 5 (Fig. 3). 

Weekend bigger that 5
Fig. 3 Week­end big­ger that 5

Now, we can sim­ply cre­ate a log­i­cal test that defines if the week day num­ber is greater than 5 (Fig. 4).

=WEEKDAY(A2,2)>5

A simple logical test
Fig. 4 A sim­ple log­i­cal test

When we copy our for­mu­la down, we can see that we have TRUE for Sat­ur­day and Sun­day, and FALSE for the rest of the days (Fig 5). 

TRUE and FALSE
Fig. 5 TRUE and FALSE

The results are prop­er, but we have to remem­ber that we want to high­light the whole row. We have to ask our­selves how our ref­er­ence should behave. We know that we will be copy­ing the for­mu­la down and to the right (hor­i­zon­tal­ly). What we have to remem­ber is that we always want to look at col­umn A, which is the Date col­umn. Even if we want to go one or two columns to the right, we always want to look at the cell from col­umn A (Fig. 6). 

A cell from column A
Fig. 6 A cell from col­umn A

That’s why, we have to lock the col­umn by press­ing the F4 key, but not the rows. It means that we have to put only one dol­lar sign (Fig. 7). 

=WEEKDAY($A2,2)>5

Locking the column
Fig. 7 Lock­ing the column

When we copy the for­mu­la down, noth­ing changes. But, when we copy it to the right and down, we can see that TRUEs and FALSEs are in a row (Fig. 8).

Rows
Fig. 8 Rows

Now, we have to copy our for­mu­la and the select the range on which we want to add con­di­tion­al for­mat­ting. The range starts with cell A2, so let’s press this cell, then Shift + , two times to the right and Ctrl + Shift + ↓ to the end of our data. Cell A2 has to stay active and we will start cre­at­ing our for­mu­la from the per­spec­tive of this cell (Fig. 10).

Selected range with an active cell
Fig. 9 Select­ed range with an active cell

Now, we go to the Home tab, then to Con­di­tion­al For­mat­ting, and we select the New Rule option (Fig. 10). 

New Rule option
Fig. 10 New Rule option

In the win­dow that will appear, we have to select the Use a for­mu­la to deter­mine which cells to for­mat bar, then paste our for­mu­la into the Edit the Rule Descrip­tion and press the For­mat but­ton (Fig. 11). 

 New Formatting Rule window
Fig. 11 New For­mat­ting Rule window

In the For­mat Cells win­dow, we press the Fill but­ton, then choose the col­or we like. Let’s choose a shade of grey, then the OK but­ton, and the OK but­ton once again in the New For­mat Rule win­dow (Fig. 12).

Fig. 12 For­mat Cells window

Now, we can see that each day of the week­end is high­light­ed the way we want­ed (Fig. 13). 

Fig. 13 High­light­ed weekends

https://www.youtube.com/watch?v=6jhZhEyk64A