+ Reply to Thread
Results 1 to 16 of 16

Help me with formula to correctly calculate unsocial hours worked

  1. #1
    Registered User
    Join Date
    11-14-2021
    Location
    Tayport, Scotland
    MS-Off Ver
    18.2106.12410.0
    Posts
    9

    Help me with formula to correctly calculate unsocial hours worked

    Hi there. I've just joined the forum as I'm driving my self dizzy trying to work out the solution to my problem. I'm sure it's an easy solution but I need the help of Excel experts to find it!

    I've created a spreadsheet version of the timesheet used within my organisation. There is no fixed shift pattern, so the sheet needs to calculate hours worked between two hours entered into the relevant cells in columns B ('From') and D ('To') respectively. The total hours worked is calculated in column F. My formulas perhaps aren't the best, but his bit all works ok for me.

    Unsocial hours are classed as any time worked before 8:00am or after 8:00pm, Monday to Friday, and all hours worked on Saturday and/or Sunday.

    Columns C and E are hidden in the working spreadsheet but I've unhidden them so you can see my formulas.

    Column C calculates the unsocial hours worked before 8:00AM, and that works ok as can be seen for the example entry for Monday.

    Column E is supposed to calculate the hours worked after 8:00PM, but this is where the problem lies. If for example the start time is 9:30PM and the finish time is 10:30PM (1 hour worked), my formula calculates the unsocial hours as 2.5 hours (ie. the difference between the trigger time for u/s hours - 8:00PM, and the finish time).

    As I said, I'm sure the solution is an easy one, and probably staring me in the face. Hoping one of the experts here can help.

    Cheers
    Attached Files Attached Files

  2. #2
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Help me with formula to correctly calculate unsocial hours worked

    Hello to Scotland.

    Formula for C3 =IF(B3<>"",MAX(0, 8/24-B3),"")+IF(B3<>"",MAX(0, D3-20/24),"")

    If you want separate formulas
    Formula for C3 =IF(B3<>"",MAX(0, 8/24-B3),"")

    Formula for E3 =IF(B3<>"",MAX(0, D3-20/24),"")

    Formula for F3 =(D3-B3)*24

    That is a starting point

    Formula for C3 =IF(B3<>"",MAX(0, 8/24-B3)+MAX(0, D3-20/24,"")
    Attached Files Attached Files
    Last edited by mehmetcik; 11-14-2021 at 09:20 AM.
    My General Rules if you want my help. Not aimed at any person in particular:

    1. Please Make Requests not demands, none of us get paid here.

    2. Check back on your post regularly. I will not return to a post after 4 days.
    If it is not important to you then it definitely is not important to me.

  3. #3
    Registered User
    Join Date
    11-14-2021
    Location
    Tayport, Scotland
    MS-Off Ver
    18.2106.12410.0
    Posts
    9

    Re: Help me with formula to correctly calculate unsocial hours worked

    Hello from Scotland to London

    Huge thanks mehmetcik for your quick reply. Unfortunately, I still get the same outcome with your formula, e.g. if I enter a 'From' time of 21:30 and a 'To' time of 22:30, it still returns 2:30 (i.e 2.5 hours) in the corresponding cell in column E, instead of 1 hour. So the formula still calculates the number of hours from the trigger time of 8:00PM rather than the total number of hours actually worked where those hours are after 8:00PM. Apologies if I'm not explaining that clearly enough.

    Any further thoughts?

  4. #4
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Help me with formula to correctly calculate unsocial hours worked

    D2 =max(if(c3>20/24,20/24,c3)-if(b3<8/24,8/24,b3),0)

    e2 =if(and(c3<>0,b3<8/24),8/24-b3,0)+ if(c3>20/24,c3-20/24,0)

    f2 =(c3-b3)*24
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    11-14-2021
    Location
    Tayport, Scotland
    MS-Off Ver
    18.2106.12410.0
    Posts
    9

    Re: Help me with formula to correctly calculate unsocial hours worked

    Thanks again, but sorry that still doesn't work - as the result in cell D7 of your worked example shows (still returns 2:30 for a start time of 21:30 and end time of 22:30)

  6. #6
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Help me with formula to correctly calculate unsocial hours worked

    Try D3 =MAX(IF(C3>20/24,20/24,C3)-IF(B3<8/24,8/24,IF(B3>20/7,B3,20/7)),0)

  7. #7
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,207

    Re: Help me with formula to correctly calculate unsocial hours worked

    Are there any constraints on work time - start at 18:oo finish at 02:00 so times over/after midnight?
    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED.

  8. #8
    Registered User
    Join Date
    11-14-2021
    Location
    Tayport, Scotland
    MS-Off Ver
    18.2106.12410.0
    Posts
    9

    Re: Help me with formula to correctly calculate unsocial hours worked

    Sorry mehmetcik,I'm confused! In my original sample spreadsheet, cells in column D are data entry - the time worked 'To'. I've edited my original sample to make it clearer. Its the formulas in the cells in column E of my spreadsheet, that aren't correctly calculating the total number of hours worked after 8:00pm
    Attached Files Attached Files

  9. #9
    Registered User
    Join Date
    11-14-2021
    Location
    Tayport, Scotland
    MS-Off Ver
    18.2106.12410.0
    Posts
    9

    Re: Help me with formula to correctly calculate unsocial hours worked

    Hi John. Good question! Technically, there are no constraints on work time (and in fact that was another issue I'd identified, but I was happy to live with a manual adjustment, on the rare occasion that a finish time goes after midnight).

  10. #10
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Help me with formula to correctly calculate unsocial hours worked

    Let me look at this some more.
    Last edited by mehmetcik; 11-14-2021 at 04:24 PM.

  11. #11
    Forum Expert
    Join Date
    05-05-2015
    Location
    UK
    MS-Off Ver
    Microsoft Excel for Microsoft 365 MSO (Version 2402 Build 16.0.17328.20068) 64-bit
    Posts
    28,207

    Re: Help me with formula to correctly calculate unsocial hours worked

    Try

    in E3

    =IF(D3="","",MAX(0,MAX(0,D3)-MAX(TIME(20,0,0),B3)))

    assumes NO working after midnight!!!

  12. #12
    Registered User
    Join Date
    11-14-2021
    Location
    Tayport, Scotland
    MS-Off Ver
    18.2106.12410.0
    Posts
    9

    Re: Help me with formula to correctly calculate unsocial hours worked

    That's it John, works brilliantly. Thank you so much

  13. #13
    Forum Expert
    Join Date
    12-14-2012
    Location
    London England
    MS-Off Ver
    MS 365 Office Suite.
    Posts
    8,448

    Re: Help me with formula to correctly calculate unsocial hours worked

    Ok I have tried it out with lots of data.

    I think this works.
    Attached Files Attached Files

  14. #14
    Registered User
    Join Date
    11-14-2021
    Location
    Tayport, Scotland
    MS-Off Ver
    18.2106.12410.0
    Posts
    9

    Re: Help me with formula to correctly calculate unsocial hours worked

    Many thanks mehmetcik, very useful alternative formula there. Really appreciate the time and trouble you've put in to my query.

  15. #15
    Registered User
    Join Date
    11-14-2021
    Location
    Tayport, Scotland
    MS-Off Ver
    18.2106.12410.0
    Posts
    9

    Re: Help me with formula to correctly calculate unsocial hours worked

    Hi John. You helped me previously by providing a formula that worked brilliantly - for which again, many thanks.

    I've now found another glitch with the formula in the timesheet I'm using which as well as calculating total worked hours, has to count the number of unsocial hours worked which are classified as any hours before 08:00 and/or after 20:00.

    You gave me a formula for the u/s hours worked after 20:00 (see cell E8, in sample sheet attached), but I now see that the formula I'm using for pre 08:00 is also wrong in that it counts the time difference between the start time and 08:00, irrespective of the total hours worked. So for example, if I start at 06:00 and finish at 07:30, my formula (see cell C9 in sample sheet) counts the unsocial hours as 2 hours, when it should only be 1.5 hours.

    Once again, I'm going round in circles and getting cross-eyed trying to work out the solution, and I reckon you'll spot the solution straight away!

    Fingers crossed!
    Attached Files Attached Files

  16. #16
    Registered User
    Join Date
    11-14-2021
    Location
    Tayport, Scotland
    MS-Off Ver
    18.2106.12410.0
    Posts
    9

    Re: Help me with formula to correctly calculate unsocial hours worked

    Hi John. You helped me previously by providing a formula that worked brilliantly - for which again, many thanks.

    I've now found another glitch with the formula in the timesheet I'm using which as well as calculating total worked hours, has to count the number of unsocial hours worked which are classified as any hours before 08:00 and/or after 20:00.

    You gave me a formula for the u/s hours worked after 20:00 (see cell E8, in sample sheet attached), but I now see that the formula I'm using for pre 08:00 is also wrong in that it counts the time difference between the start time and 08:00, irrespective of the total hours worked. So for example, if I start at 06:00 and finish at 07:30, my formula (see cell C9 in sample sheet) counts the unsocial hours as 2 hours, when it should only be 1.5 hours.

    Once again, I'm going round in circles and getting cross-eyed trying to work out the solution, and I reckon you'll spot the solution straight away!

    Fingers crossed!
    Attached Files Attached Files

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Formula to calculate hours worked w/lunch or w/o + OT column only total after 40 hours
    By blinhart in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 10-31-2023, 11:14 AM
  2. [SOLVED] Time Sheet which works out number of hours worked unsocial
    By Dan107 in forum Excel General
    Replies: 4
    Last Post: 09-08-2015, 12:28 PM
  3. [SOLVED] Formula to calculate hours worked
    By LilMissM in forum Excel Formulas & Functions
    Replies: 14
    Last Post: 05-13-2014, 06:08 PM
  4. [SOLVED] Formula To Calculate Hours Worked In a Day
    By SilverFox in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 03-15-2013, 05:21 AM
  5. Replies: 4
    Last Post: 02-12-2013, 07:01 AM
  6. Replies: 2
    Last Post: 02-11-2013, 02:26 PM
  7. Need formula to calculate hours worked
    By BankC in forum Excel General
    Replies: 12
    Last Post: 01-31-2006, 01:38 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1