+ Reply to Thread
Results 1 to 6 of 6

network days formula not handling cases when start date is weekend

Hybrid View

  1. #1
    Registered User
    Join Date
    01-25-2022
    Location
    London, England
    MS-Off Ver
    Microsoft Office Professional Plus 2021; Office 365
    Posts
    85

    network days formula not handling cases when start date is weekend

    Hi Experts,

    I have a formula which calculates the working hours between two dates, considering a specific working schedule and excluding weekends and holidays.

    I have a problem regarding when the start date is in the weekend, example attached.

    Can someone please help?
    Attached Files Attached Files
    Alcohol & calculus don't mix.Never drink & derive.

  2. #2
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,864

    Re: network days formula not handling cases when start date is weekend

    I started the formula from the scratch and ended with such construction:
    Formula: copy to clipboard
    =MAX(0,MIN(FLOOR.MATH(A2)+TIME(16,0,0),B2)-WORKDAY(A2-1,1,$R$2:$R$100)-MAX(A2-FLOOR.MATH(A2),TIME(8,0,0)))+
    MAX(0,NETWORKDAYS(WORKDAY(A2,1,$R$2:$R$100),WORKDAY(B2,-1,$R$2:$R$100),$R$2:$R$100))*TIME(8,0,0)+
    IF(FLOOR.MATH(A2)=FLOOR.MATH(B2),0,MAX(0,MIN(FLOOR.MATH(B2)+TIME(16,0,0),B2)-WORKDAY(B2-1,1,$R$2:$R$100)-TIME(8,0,0)))

    it consists of three rows (three parts):
    - first is for time in starting day (is 0 if it's non-working day or time after 16:00)
    - second is the number of working days in between day after first day and a day before last day, multiplied at the end by 8 hours per day:
    two subcomments here:
    a) *TIME(8,0,0) it is equivalent to end hour minus start hour: *(TIME(16,0,0)-TIME(8,0,0)) - worth noticing if starting and ending hours are written in some cells).
    b) situation of no such days is handled by using MAX(0,dates difference)
    - third is the number of hours on last day, but only if last day is different than first day - IF used. It could be also written as (FLOOR.MATH(A2)<>FLOOR.MATH(B2))*(MAX(0,MIN(FLOOR.MATH(B2)+TIME(16,0,0),B2)-WORKDAY(B2-1,1,$R$2:$R$100)-TIME(8,0,0))) (I originally wrote it that way, but IF is probably easier readable).

    Just to analyze (and correct? if needed) I left columns F/N(P) with partial results I used while creating the formula. To show they are not needed any longer I removed them in rows 4:9
    Delete all them from final version.
    Attached Files Attached Files
    Best Regards,

    Kaper

  3. #3
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: network days formula not handling cases when start date is weekend

    Pl see file.
    In C2
    Formula: copy to clipboard
    =IF(AND($A2>0,$B2>0),IF(INT($B2)=INT($A2),IF(NETWORKDAYS($A2,$A2,$M$2:$M$3)>0,(WEEKDAY($A2,2)<6)*(MEDIAN($F$2,$G$2,ROUND(MOD($B2,1),6))-MEDIAN($F$2,$G$2,ROUND(MOD($A2,1),6))),0),(NETWORKDAYS($A2,$A2,$M$2:$M$3)>0)*($G$2-MAX($F$2,ROUND(MOD($A2,1),6)))+(NETWORKDAYS($B2,$B2,$M$2:$M$3)>0)*(MIN(ROUND(MOD($B2,1),6),$G$2)-$F$2)+IF((INT($B2)-INT($A2))>1,NETWORKDAYS($A2+1,$B2-1)*($G$2-$F$2),0)),"")
    Pl note
    Array formula should be confirmed with Ctrl+Shift+Enter keys together.
    If answere is satisfactory press * to add reputation.

  4. #4
    Forum Guru Kaper's Avatar
    Join Date
    12-14-2013
    Location
    Warsaw, Poland
    MS-Off Ver
    most often: Office 365 in Windows environment
    Posts
    8,864

    Re: network days formula not handling cases when start date is weekend

    @kvsrinivasamurthy:
    I don't think your formula is right.

    Check for instance last dates used in my example in previous post.

    Or looking at coming days, look for instance on:
    10.05.2024 07:48 13.05.2024 07:09

    It's a whole day in Friday then Sat free, Sun free and on Mon finish before 8:00 so no hours worked.
    But your formula returns (for end of working day at 18:00, so 10 hrs/day) 9:09:11, not full 10 hrs worked on Friday.

    To make debugging easier (probably) check also pairs:
    10.05.2024 07:48 09.05.2024 07:09 (shall be 0 as work was finished before start, but result of your formula is again 9:09:11)
    and pair 11.05.2024 07:48 10.05.2024 07:09 - here you obtain negative result, which is displayed as ######

    If we think that such data pairs could be entered into worksheet, may be formula shall be nested in =IF(B2<=A2,"Please correct dates finish shall be later than start", ... current formula ...)



    I like the idea to add checking if both A2 and B2 are >0 so no result is displayed until both dates are entered into worksheet.


    Having this in mind and replacing FLOOR.MATH with INT function in my formula, and adjusting holidays to range you used in your file we end up with:
    Formula: copy to clipboard
    =IF(AND(A2>0,B2>0),
    MAX(0,MIN(INT(A2)+G$2,B2)-WORKDAY(A2-1,1,M$2:M$3)-MAX(A2-INT(A2),F$2))+
    MAX(0,NETWORKDAYS(WORKDAY(A2,1,M$2:M$3),WORKDAY(B2,-1,M$2:M$3),M$2:M$3))*(G$2-F$2)+
    IF(INT(A2)=INT(B2),0,MAX(0,MIN(INT(B2)+G$2,B2)-WORKDAY(B2-1,1,M$2:M$3)-F$2)),
    "")

  5. #5
    Forum Expert
    Join Date
    07-20-2011
    Location
    Mysore, India.
    MS-Off Ver
    Excel 2019
    Posts
    8,710

    Re: network days formula not handling cases when start date is weekend

    Try in C2
    Formula: copy to clipboard
    =IF(AND($A2>0,$B2>0),IF(INT($B2)=INT($A2),IF(NETWORKDAYS($A2,$A2,$M$2:$M$3)>0,(WEEKDAY($A2,2)<6)*(MEDIAN($F$2,$G$2,ROUND(MOD($B2,1),6))-MEDIAN($F$2,$G$2,ROUND(MOD($A2,1),6))),0),(NETWORKDAYS($A2,$A2,$M$2:$M$3)>0)*($G$2-MEDIAN($F$2,$G$2,ROUND(MOD($A2,1),6)))+(NETWORKDAYS($B2,$B2,$M$2:$M$3)>0)*(MEDIAN(ROUND(MOD($B2,1),6),$F$2,$G$2)-$F$2)+IF((INT($B2)-INT($A2))>1,NETWORKDAYS($A2+1,$B2-1)*($G$2-$F$2),0)),"")

  6. #6
    Registered User
    Join Date
    01-25-2022
    Location
    London, England
    MS-Off Ver
    Microsoft Office Professional Plus 2021; Office 365
    Posts
    85

    Re: network days formula not handling cases when start date is weekend

    guys, thank you for all your help. incredible display of skills. adding rep & marking this as solved.

+ 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] Start Date + 8 Days but Can't End on a Weekend/Holiday
    By rebma in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 07-21-2021, 11:11 AM
  2. [SOLVED] Formula to count how many AM and PM sessions within a date range using Network Days
    By hodge1 in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 11-11-2016, 10:03 AM
  3. Replies: 24
    Last Post: 10-08-2014, 10:31 AM
  4. [SOLVED] Find date using start date and number of network days
    By v!ctor in forum Excel General
    Replies: 4
    Last Post: 07-06-2012, 05:27 PM
  5. Replies: 5
    Last Post: 12-15-2011, 11:16 AM
  6. aging formula with network days from start to today
    By eddienole in forum Excel General
    Replies: 2
    Last Post: 08-30-2011, 10:43 AM
  7. Excel running date formula minus weekend days
    By Eliane in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 03-30-2005, 06:06 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