0

I have google docs list: https://docs.google.com/spreadsheets/d/1gB-t47BKusiwVM53lZi1dGjaDcscILP4yCMtEdtUTao/edit?usp=sharing

Where I get SMS texts from phone to the table automaticly. I've already asked there before, and got great help, how to solve some issues, but unfortunatelly I'm stucked there again with another fomulas.

I have couple of issues:

The formulas can't be in the cells themself, but has to be in the "Arrayformula" Because otherwise the automatic SMS push the formulas down when recieved, therefore I get there the advice of this + Index and count function.

So now to my Issues: I would need there in column M to be hours worked trough night. Night work starts at: 22:00 and end at 06:00. Bellow is the table and bellow the table I'll put the formulas that I have sofar.

|    | D               | F              |       G      |      I     |   J               |   M        |    
|----+-----------------+----------------+--------------+------------+-------------------+------------+
| 1> | Start work date | Start of work  |End work date |End of work | Total hours worked| Night shift|
| 2> | 11.09.2020      | 16:00          | 12.09.2020   | 04:00      |                   |            |
| 3> | 10.09.2020      | 07:00          | 12.09.2020   | 07:00      |                   |            |

|    |       N            |  
|----+--------------------+
| 1> |Worked over weekend |
| 2> |                    |
| 3> |                    |

For the hours worked (column J) I have this formula, but the issues with it is that it won't come over 24 hours. The total for J3 should be 48 hours worked.

=ARRAYFORMULA({"Hours worked";--(DATEVALUE(G2:INDEX(G:G,Counta(D:D)))+TIMEVALUE(I2:INDEX(I:I,Counta(I:I))))-(DATEVALUE(D2:INDEX(D:D,Counta(D:D)))+TIMEVALUE(F2:INDEX(F:F,COUNTA(F:F))))})

Then I have this, but it won't count correctly ( column M) Night hours should be 22:00-06:00.

=(J2-(
 If(F2<I2;
  MAX(0;MIN(I2;TIMEVALUE("22:00"))-MAX(F2;TIMEVALUE("06:00")));
  MAX(0;TIMEVALUE("22:00")-F2)+MAX(0;I2-TIMEVALUE("06:00"))
 )
)*24)

M2 should be 06:00. M3 should be 16

I'm not really sure about the formating of celles, ets. It's in the table above linked. And the weekend hours are from Saturday - Sunday. I've tought that I would do it with if conditions, since I have in the table days of weeks if that would be possible? The weekend hours for N2 should be 4 hours.

For any feedback I would be really glad.

TheMaster
  • 45,448
  • 6
  • 62
  • 85
  • Your locale? In file spreadsheet settings? – TheMaster Sep 14 '20 at 12:36
  • Hi - sorry don't understand that. You mean language? I've changed it to US. The formulas I've tried to translate, hopefully correct. – Patrik Gremlica Sep 14 '20 at 12:40
  • Localse. File> Spreadsheet settings > The first oneb location: "Denmark" or "US". Or if you changed it to US, [Edit] your question to provide dates correctly. For eg, `11.09.2020` is not a valid date in US, but it is valid in some European/russian countries. Alternatively, provide date of format yyyy-mm-dd ( It's universally accepted ) – TheMaster Sep 14 '20 at 12:43
  • 1
    the sample sheet is not accessible. – MattKing Sep 14 '20 at 18:58
  • Hi, thank you for the reply. It should be accesible now: https://docs.google.com/spreadsheets/d/1gB-t47BKusiwVM53lZi1dGjaDcscILP4yCMtEdtUTao/edit?usp=sharing – Patrik Gremlica Sep 14 '20 at 19:48
  • @MattKing I've managed to get the "time worked working" I would need to now only check this formula that isn't working right: =ARRAYFORMULA({"Day of week";(J2:INDEX(J:J;COUNTA(J:J)))-IF(F2:INDEX(F:F;COUNTA(F:F))) – Patrik Gremlica Sep 14 '20 at 19:49
  • @TheMaster - I've tried to change it as you wrote, but then it doesn't work since the dates are incorect for the formulas I now have. I will need to keep it in CZE in the future too, so unfortunatelly that is not a option. However thank you for the input, I will try local forums. – Patrik Gremlica Sep 14 '20 at 19:51
  • @PatrikGremlica there is a very simple formula for calculating hours overnight that I can demo if you make your sample sheet editable as opposed to View only. It looks like this ARRAYFORMULA([end time column] - [start time column]+ ([end time column] < [start time column]) You don't need to use the dates at all. – MattKing Sep 14 '20 at 19:57
  • @PatrikGremlica I only asked you to change the date provided in the table above to yyy-mm-dd. That's all. It'll work regardless of the locale you chose after. – TheMaster Sep 14 '20 at 20:12
  • For J: *but the issues with it is that it won't come over 24 hours.* You only need to set format correct: Numberx "Duration" – TheMaster Sep 14 '20 at 20:21
  • @MattKing: It's changed. https://docs.google.com/spreadsheets/d/1gB-t47BKusiwVM53lZi1dGjaDcscILP4yCMtEdtUTao/edit?usp=sharing I've got there total hours worked with dates, if there is over 24 hours worked, and it needs to be done with the ARRAYFORMULAS, or INDEX as TheMaster advice me last time, since then the automatic texts won't come bellow the formulas. This I've wrote above is for the hours worked in 22:00 - 06:00 which is night work. – Patrik Gremlica Sep 14 '20 at 20:59
  • @Themaster I've tried to change it, but since it's from the text, it won't change the dates to the format you've wrote. Also I've tried the format "Duration" which work good, but when I then need to highlight the cells over 24 hours it won't work, since it can't count ( I can't count) with that. So I've made it formated as number and *24 the whole formula and that worked. – Patrik Gremlica Sep 14 '20 at 21:01

2 Answers2

3

M1:

={"Night shift";ARRAY_CONSTRAIN(ARRAYFORMULA(IF(G2:G-D2:D>=1,IF(--F2:F>--"22:00",1-F2:F,"2:00")+IF(--I2:I<--"6:00",I2:I,"6:00")+(G2:G-D2:D-1)*"8:00")),COUNTA(D:D)-1,1)}

Explanation:

={"Night shift";                                             
    ARRAY_CONSTRAIN(                      //constrain output                   
    ARRAYFORMULA(                         //enforce array context                   
      IF(G2:G-D2:D>=1,                    //if end work date > start                   
        IF(--F2:F>--"22:00",1-F2:F,"2:00")//if start time >22, get hours left else 2 hours                   
        +IF(--I2:I<--"6:00",I2:I,"6:00")  //if end time < 6 use hours worked else cap it                   
        +(G2:G-D2:D-1)*"8:00"             //8 hours for full days                   
      )                                                      
    )                                                        
    ,COUNTA(D:D)-1,1)                     //constrain rows to count                    
}                                                            

Notes:

  • INDEX/COUNTA is still preferred but I've used ARRAY_CONSTRAIN instead to limit formula size.
  • You should format the resulting column as "Duration"
  • All date time columns should be formatted and recognised as date and time
TheMaster
  • 45,448
  • 6
  • 62
  • 85
  • Hello, thank you for the answer! However it doesn't work :/ I don't know if it's because of the "duration" or why exactly not. Is there any chance to check this formula that works without the Array, but I think I'm doing something wrong with the flow of the formula, but should work also and it's doing what I would need: – Patrik Gremlica Sep 15 '20 at 07:48
  • @ Themaster =ARRAYFORMULA({"Day of week";(J2:INDEX(J:J;COUNTA(J:J)))-(IF(F2:INDEX(F:F;COUNTA(F:F)))<(I2:INDEX(I:I;COUNTA(I:I)));MAX(0;MIN(I2:INDEX(I:I;COUNTA(I:I))TIMEVALUE("22:00"))-MAX(F2:INDEX(F:F;COUNTA(F:F));TIMEVALUE("06:00")));MAX(0;TIMEVALUE("22:00")-F2:INDEX(F:F;CONUTA(F:F))+MAX(0;I2:INDEX(I:I;COUNTA(I:I))-TIMEVALUE("06:00"))))*24}) – Patrik Gremlica Sep 15 '20 at 07:48
  • The original formula from I've tried to make the Arrayformula and is working: =(J2-( IF(F2 – Patrik Gremlica Sep 15 '20 at 07:54
  • @PatrikGremlica Explain *doesn't work*. What do you mean by it doesn't work? Any error messages? My formula works fine for me. Your formula can't be used in arrayformula due to use of `max`/`min`, which won't support per-row operations – TheMaster Sep 15 '20 at 08:04
  • Thank you, didn't know that. It says translated - error with the analysis of formula. I noticed in the past that I can't make , but have to use ; intead. So I've done that and the it says it can't recognize the >-- so I've tried to do this: ={"Night shift";ARRAY_CONSTRAIN(ARRAYFORMULA(IF(G2:G-D2:D>=1;IF((--F2:F>)--"22:00";1-F2:F;"2:00")+IF((--I2:I<)--"6:00", I2:I ,"6:00")+(G2:G-D2:D-1)*"8:00"));COUNTA(D:D)-1;1)} but that didn't helped either – Patrik Gremlica Sep 15 '20 at 08:20
  • Thank you for the inputs, I've tried and now it's working! :) I've tried this and it worked: ={"Night shift"; ARRAY_CONSTRAIN(ARRAYFORMULA(IF(G2:G-D2:D>=1;IF(F2:F>"22:00";1-F2:F;"2:00")+IF(I2:I<"6:00";I2:I;"6:00")+(G2:G-D2:D-1)*"8:00";0));COUNTA(D:D)-1;1)} Now there is only 1 issue - at the cell "M2" there are the hours counted wrong. I don't know why that is if you'd have a minute could you please check it? [link]https://docs.google.com/spreadsheets/d/1gB-t47BKusiwVM53lZi1dGjaDcscILP4yCMtEdtUTao/edit?usp=sharing – Patrik Gremlica Sep 15 '20 at 08:43
  • @PatrikGremlica `--` before each argument (like `--F2:F`, `--"22:00"`) is essential. – TheMaster Sep 15 '20 at 08:57
  • of course it's working now. Thank you so much for all the help and effort as well as patience with me! :D Would you have paypal or anything I will be glad to send you something for the help, since I deffinitely woudn't make it work without you. Thank you! – Patrik Gremlica Sep 15 '20 at 09:07
  • 1
    Maybe I didn't understand the question, but shouldn't this check for hours worked if start day and end day are the same? That's what I came up with on regards to this: `ARRAYFORMULA(IFS(G2:G-D2:D>=1;IF(--F2:F>--"22:00";1-F2:F;"2:00")+IF(--I2:I<--"6:00";I2:I;"6:00")+(G2:G-D2:D-1)*"8:00";G2:G-D2:D=0;IF(--I2:I>--"22:00";I2:I-"22:00";"00:00")+IF(--F2:F<--"6:00";"6:00"-F2:F;"00:00")))`. – Iamblichus Sep 15 '20 at 09:13
  • @lamblichus You're right. If start and end date are the same, My formula doesn't account for it. I made some fair assumptions that nightshift will continue into next day. I don't think even your modified formula accounts for all out of box cases: I think it'll fail on start at 4am and end at 5am on the same day – TheMaster Sep 15 '20 at 09:41
  • @PatrikGremlica lamblichus showed where this might not work. Is that case a real world scenario? Employees starting and ending on the same day- being considered for "Night shift" hours. – TheMaster Sep 15 '20 at 09:44
  • @lamblichus Thank you for the input! Yes I've tried start of work at 01:00 the same day and ending at 23:00 the same day - which should be 06:00 hours of work trough night, but it came back as 0 hours worked. I don't think it will hapen that often, but since the data are all done by the system it can't be changed by hand in the formula, otherwise it won't then write under it again with the automated SMS. – Patrik Gremlica Sep 15 '20 at 12:28
  • @TheMaster I've tried to remake the formula, but it says that there is "Unary Minus" should be number and therefore it won't count. I've made this formula, since I can't make the <-- because my google sheets can't work with this function and this I've tried before worked with your formula, but won't work with this: – Patrik Gremlica Sep 15 '20 at 12:58
  • ={"Night shift"; ARRAY_CONSTRAIN( ARRAYFORMULA(IFS(--G2:G-D2:D>=1;IF(--F2:F>(--"22:00");1-F2:F;(--"02:00"))+IF(--I2:I<(--"06:00");I2:I;(--"06:00"))+(G2:G-D2:D-1)*(--"08:00");G2:G-D2:D=0;IF(--I2:I>(--"22:00");I2:I-(--"22:00");(--"00:00"))+IF(--F2:F<(--"06:00");(--"06:00")-F2:F;(--"00:00")) ;0)) ;COUNTA(D:D)-1;1) } – Patrik Gremlica Sep 15 '20 at 12:58
  • @Iamblichus Feel free to add another answer. – TheMaster Sep 15 '20 at 17:19
3

The fundamental problem you're having I think is that you're trying to work with text rather than converting immediately into numbers to keep track of time and dates. I made a new tab on your sheet called MK.help and put the following formula in cell M1.

=ARRAYFORMULA({"night shift";IF(C2:C="";;IF((1-F2:F)*(I2:I<F2:F)>2/24;2/24*(I2:I<F2:F);(1-F2:F)*(I2:I<F2:F))+IF(I2:I*(I2:I<F2:F)>6/24;6/24*(I2:I<F2:F);I2:I*(I2:I<F2:F)))})

You'll note that I changed all of the other formulas you were using as well to extract the times and dates from your system. Your formulas were overkill and they seemed to rely on a lot of text, rather than number manipulation.

MattKing
  • 7,373
  • 8
  • 13
  • Wow, that is awesome! Thank you so much for that! :) The only issue now I have is that when there is work at night the same day it won't count the night hours. M2 should be 03:00, but it's 00:00. It was because the old formula was wrong, but it was corrected there above: ARRAYFORMULA(IFS(G2:G-D2:D>=1;IF(--F2:F>--"22:00";1-F2:F;"2:00")+IF(--I2:I<--"6:00";I2:I;"6:00")+(G2:G-D2:D-1)*"8:00";G2:G-D2:D=0;IF(--I2:I>--"22:00";I2:I-"22:00";"00:00")+IF(--F2:F<--"6:00";"6:00"-F2:F;"00:00"))) But I don't think I would be able to put it in the new one you've made right now. I think in a week maybye :D – Patrik Gremlica Sep 15 '20 at 16:59
  • ah, you're right. i forgot to add that. I've just done it and it should be better. – MattKing Sep 15 '20 at 17:15
  • Thank you, thank you so much! :) I don't need the day of week, it was just what I used to make the formula work, but this is definitelly way better. But the night shift - M2 should be 3 hours worked and at M16 should be 4 hours, It's always only the ones that isn't over the midnight. I will try to check the formula – Patrik Gremlica Sep 15 '20 at 17:47
  • Sorry! i keep hoping to catch you on the sheet so i can check it live. Fixed (again!) any better? – MattKing Sep 15 '20 at 18:40
  • Ah, sorry had to go away for a while. That is perfect, thank you so much again! – Patrik Gremlica Sep 15 '20 at 19:55
  • Hi Matt, I'm really sorry to bother you with this. I have there also the weekend hours ( I would ask new question for that, but can't for 2 days now). It's the column O. I've tried with the array formula for that, but can't get the formula working with that, even when I've tried to use WEEKDAY(). If you would have chance to look at that, if not that's OK. Don't want to bother you with that, since the help you've already gave me was more than enough. – Patrik Gremlica Sep 16 '20 at 09:50
  • Hi I don't know if you've recieved the coment or not. – Patrik Gremlica Sep 16 '20 at 21:42