# Calculate time interval

Hello,

For context, my app has Tasks that each have 4 different status options:
To Do
In progress
Blocked
Complete

By default the task is created in the To Do status

I’m trying to calculate the time interval between the statuses.
ie: how long was each task in each of it’s respective statuses for?

In my workflow I was attempting the below by using “Start” and “End” attributes for each status, in the Task data type:

Although, I ran out of ideas once I realised that not every task will receive each of the 4 status options throughout its lifecycle, and I couldn’t figure out how to write an ‘Only when’ expression to the affect of:
“only when this task’s status changes FROM X TO Y” (where Y = either of the other 3 statuses.)

Any ideas are welcome. Thanks!

You mean not every task will go from a->b->c->d and they can jump from b->d etc.?

Yes that’s correct

In that case you can add couple of more fields for each state.
“Duration in the state”
“When this state stopped”

Now even if jump happens from b to d, you update values for “end time of b”, “duration of b”, “start time of d”

I currently already have “Start” and “End” attributes for each status - the main part I’m having trouble with is figuring out how to write an ‘Only when’ expression to the affect of:
“only when this task’s status changes FROM X TO Y” (where Y = either of the other 3 statuses.)

Yes I got that. I am saying that when you add these extra fields that I am talking about, you don’t need to add a complex condition in “only when”

You can just have more actions.
`If old state is a, "a's end time = current time"`
`If new state is a, "a's start time = current time"`
`If new state is b, "b's start time = current time"`

Like that.

1 Like

I see what you mean - I’m not exactly sure how to put that into affect though without an ‘only when’ expression?

You need to put “only when”. But the condition within “only when” can be simple.

i.e.

`a's end time = current time`
`"only when old state is a"`

You can have two statements like this for each state. So you don’t have one complex “only when” but it gets broken down in multiple actions.

I see, it makes sense too but I’m still running into the issue of writing an expression identifying what the ‘old state’ was though, or what the ‘new state’ is.

Perhaps if I share my editor with it would be clear?

Thanks

Thanks -

Thanks.

I suggest following:

1. Create a custom event to update the statuses, say “Update Status”
2. In the custom event have “old status”, “new status” and “task” as parameters
3. In your first workflow (“Group task statuse…”) call the custom event Update Status instead of doing the work directly there.
4. In Update Status custom event you would need following actions for whichever status you want to record
a. If old state is “x” . Here you mark “x’s end time = current time”. “x’s duration = current time - x’s start time”
b. If new state is “x” . Here you mark “x’s start time = current time”

Hope this helps.

1 Like

Thanks! I’ll give this a try tomorrow and let you know. Thanks again for your help!

I believe I have it set up as per your instructions - thank you! That’s the solution!

One last extra question - do you have any idea of how I can structure the steps to record total time spent in a state if the Task has moved in and out of one throughout its life?

ie: Task #1 starts off in ‘To Do’ state, it then moves to ‘In Progress’, but then moves back into ‘To Do’ some time later. By the end of the Task’s life (when it’s completed), I’d like to have recorded how long in total it had spent in the To Do state.

I believe if I want to record this I’d need to be careful of where/how I’m using ‘Current date/time’ in the expressions.

If you want to do that, then you can default the “duration” for each state to 0, and then in the actions where you are assigning durations, instead of assigning durations you “add” duration to existing values.

However, in case you need to have more details e.g. How many times did a task move from In-progress to Todo, then you may want to keep a separate data to store the history of changes so that you can query that and get various details.

Thanks!

I’ve given it a play around and trying to it figure out.

Right now I have the following data values:
“State Start” - date
“State End” - date
“State Duration” - date interval

I am not able to set a default on the Duration type which is a date-interval field type - what field type should the ‘Duration’ be in order to both collect and add to existing values?

ie: equate the initial time in the state then add any additional time in that state (if the task moves back to that state)?

I would put duration as number and put either the minutes of difference between two dates or seconds, depending on my use-case.

Thanks - although even a ‘number’ field type doesn’t allow me to ‘add’ to the State Duration data type, only set what State Duration is equal to.

Like I said earlier, I would rather create a different data type if I want to store history of changes.

What you are asking at the moment is more about logically structuring your data and workflows and doesn’t have much about how Bubble works per se. One would need to have detailed discussion on what exactly is needed to arrive at right choice of structuring data and workflows.

I agree, and that’s what I have done - but even a ‘number’ field type doesn’t allow me to ‘add’ to the State Duration data type, only set what State Duration is equal to.