Calculate a project start date from the duration and project due date

Answered

Comments

11 comments

  • Rainer Grabowski (FVM)

    Hi Tieka,

    that's no problem. But it depends on your app setup how the code looks like. Some questions:

    1. Which type is the field where the duration is in? Is it a Podio duration field (like: 10 Days 4 Hours) or is it a number field where someone types in a number (# of weeks? # of days? # of hours?)?

    2. Do you want to calculate with business days (Mon-Fri), without counting Sat and Sun) only so that e.g. DueDate 02/03/2015 minus 5 days = StartDate 01/28/2015? Or do you want to calculate with calendar days so that start date would be 01/30/2015?

    3. Should the StartDate be shown like a DateField (so that you can use it in the Podio Calendar) or as a text string)

    Rainer

    0
    Comment actions Permalink
  • Tieka Mayen

    Hello Rainer,
    I set up the duration field as a calc that assigns the duration based on project details. Eg. a project with 1-2tems items is assigned 10 weekdays, 3-4 items is assigned 15 workdays and so on. I am not sure how it works yet.

    if (Number of Items == '1-2') {Duration Category = '10 weekdays';} else if (Number of Items == '3-4') {Duration Category = '15 weekdays';} else if (Number of Items == '5-6') {Duration Category = '20 weekdays';} else if (Number of Items == '7+') {Duration Category = '30 weekdays';} else if (Number of Items == 'Rush') {Duration Category = '5 weekdays';}

    I want to calculate with workdays and the start date should be a date field. Thanks again.

    0
    Comment actions Permalink
  • Rainer Grabowski (FVM)

    Hi Tieka,

    you have to make sure that your duration field is a number type calculation field. Then you can use the following code for calculating the StartDate. Create a new calculation field in the template editor with:

    var dueDate = @DueDate-FieldName; 
    var dur = @Duration-FieldName;
    
    function subtractWeekdays(date, days) { 
         date = dueDate; 
         while (days > 1) { 
             date = date.subtract(1, 'days'); 
             if (date.isoWeekday() !== 6 && date.isoWeekday() !== 7) { days -= 1;
             } 
         } 
            return date; 
    } 
    var startDate = subtractWeekdays(moment(),dur); 
    moment(starDate).toDate();
    

    Than click onto the field icon (left from the name) and choose the options in the field settings.

    For the calc in your duration field: If that what you have written in your post is the whole code in the field the result is a text string and you can't calculate further like you want to do. You have to use another code for getting the number of duration weekdays. And how to get the number depends on your setup :-)

    Rainer
    rg@delos-consulting.com

    0
    Comment actions Permalink
  • Admin - BK

    may ask a thing Rainer;

     

    First of all, the code is not working for me, i couldnt find the problem.

     

    and a second phase, what if i want to reverse scenario?

    Start Day + Duration must be calculate End day (date) using workdays (starts monday morning and ends Friday afternoon.)

    how can i?

    thanks.

     

     

     

    0
    Comment actions Permalink
  • Rainer Grabowski

    Hi Admin-BK,

    change the line
    date = duedate
    to
    date = moment(dueDate)

    For the reverse scenario just change 
    date.subtract(1, 'days')
    to
    date.add(1, 'days')


    I would also change 
    subtractWeekdays
    to
    addWeekdays - just to make it clear that this is an add-function (it's only naming, doesn't have an impact on the result)

    Rainer

     

    0
    Comment actions Permalink
  • Admin - BK

    thanks :) it worked. :)

    but, a minor reminder: there is a bug, i think, in podio duration area.

    Days colums pretends as a hour. so, in formul i have to use /24.

    0
    Comment actions Permalink
  • Rainer Grabowski

     you're right for the duration field. Didn't know that you use a real duration field :)  In the above example (Tieka) the duration is in a calculation field (as a number), many people use number fields for e.g. Project durations(if duration = Full days) . 

    0
    Comment actions Permalink
  • Shane Jezowski

    Hello there Rainer, 

    I am looking to Chop & Screw your script to help us to calculate how many work days our employees are taking off on their vacation requests while excluding weekend days from the calculation. The issue is that I am a total hack at coding these calculations - DOH!

    Do you mind giving this a look over to let me know if I am just being a savage to your script?!? 

     

    Any help would be greatly appreciated! 

    0
    Comment actions Permalink
  • Shane Jezowski

    Also tried your adjusted note to use Moment(dueDate)

     

    0
    Comment actions Permalink
  • Rainer Grabowski

    Hi Shane,

    it can't work cause that's the wrong code for your use case. The code needs a date = var dueDate and a number of days = var dur; but you have 2 dates. And the code returns a date, but you want to return a number of days.  

    You can try this one:

    var start = @Days/Time out of Office;
    var end = @Return to Office;
    var dur = moment(end).diff(moment(end),"days");
    var workdays = 0;
    for(var i = 0; i < dur; i++){
        var day = moment(start).add(i,"days").isoWeekday();
        if(day < 6){
           workdays += 1;
       }
    };
    workdays

    Try that code in a new calculation field.

    Rainer

    0
    Comment actions Permalink
  • Shane Jezowski

    Hey Rainer,

    Thank you for being willing to review and adjust this. I have replaced the script to match, but I now receive a numerical value of zero and I think this has to do with the " var workdays = 0; " line. Should this instead be my original calculation for my yielded workdays? See below:

    var workdays = 0;
    var workdays = (@Return to Office - @Days/Time Out of Office) / 86400000;



    When I swap the "var workdays" to not be zero it comes up with the 13 days calculation, but it is not removing the weekends from the sum.

    Thank you for your help here! I think after this calculation quest, I will need to get some night courses under my belt for the next round. 

    0
    Comment actions Permalink

Please sign in to leave a comment.

Powered by Zendesk