How to group durations of timerange per days?

I would like to make some statistical analysis on a time register that I use to log each work tasks that I performed. It is a LibreOffice spreadsheet with 3 columns: the task type, the begin datetime and the end datetime.

I would like to have the working time duration for each days of the year.

For that purpose, I started by adding a 4th column to the register with the date that I started performing the task (it is just the date of the begin datetime). I also added a 5th column with the duration of the task (“end datetime” minus “begin datetime”).

In a new spreadsheet, I listed all the days of the year in one column and I wrote a formula in a second column using the SUMIF function. I sum the duration of the register if it corresponds to the day I am performing the calculation for.

That worked perfectly but this was before I started having tasks that overlap midnight: tasks that do not start and end within the same day and that could spread over several days.

I thought I could sum up the duration part of the tasks that intersect with the day I am performing the calculation for. Sadly, I did not find an “intersection” function.

Do you how I can compute these statistics?

Edit on 2015-05-18: I may have been too vague at first, the tasks duration can not only spread on two consecutive days but they can also spread on several days.

Hallo

Lets assume the Start DateTime Stamp is in Column B and the End DateTime Stamp is in C
the Duration belonging to StartDate: (place it in Column E)

=IF(INT(C2)>B2;INT(C2);C2)-B2

the Duration belonging to EndDate ( if EndDate is later as StartDate ): (put into Column F)

=IF(INT(C2)>B2;MOD(C2;1);"")

set NumberFormatcode in Column E and F to [HH]:MM

Which version of LIbO are you using?

I also observed that there is a problem when calculating a duration over midnight.
https://bugs.documentfoundation.org/show_bug.cgi?id=88285

I was surprised to be told the way it work before was changed to get consistency with Excel.

And advice / workaround I kindly received from raal :

You can press CTRL+; (this insert
DATE) and then on the same cell press
CTRL+SHIFT+; (this insert TIME) →
result is date with time. Example:
CTRL+; (14.01.2015) & CTRL+SHIFT+;
→ result is 14.01.15 13:01

I’ll close this as notabug. Maybe this
can be enhancement “create shortcut
for insert date&time”.

I had not yet enough time to place an related enhancement request. Please feel free to do so.

@ROSt53
the question is not about changed shortcuts inserting DateTime versus Time.
its about “splitting” durations into parts meanwhile days of Startdate and EndDate

Hi

GroupDurations.ods is an example without adding column and for grouping per day.

Note: B14 is a matrix formula. It must be validated by Ctrl+Shift+ Enter.
To copy it down use the fill handle with Ctrl.

Karolus’s formula is shorter. I therefore advise to use it same manner in the consolidation; e.g.:

=SUMPRODUCT(IF(INT($C$2:$C$10)>$B$2:$B$10;INT($C$2:$C$10);$C$2:$C$10)-$B$2:$B$10;INT($B$2:$B$10)=B14)+SUMPRODUCT(IF(INT($C$2:$C$10)>$B$2:$B$10;MOD($C$2:$C$10;1);"");INT($C$2:$C$10)=B14)

I would like to submit an additional approach using more helpers to get a clear structure. This is, of course, not intending to depreciate any suggestion already posted! ask50705DurationPerDay001.ods

(Editing with regard to the changes and completions of the question made by updating.)

I’m not convinced there’s much sense in it. The summing of “intersecting durations” per day is, however, feasible. Overlapping of tasks might need to be excluded.
ask50705DurationPerDay002.ods

=== Edited with regard to the solution posted by the questioner ( @maurice ) himself, concerning an alternative solution without helpers===

I don’t like nested IFs too much and I wouldn’t call the given formula exactly simple, but I could verify it under the obvious assumptions.

There is, however, in principle a solution to the Interval Intersection Task “hiding” the few inevitable IFs in calls of the functions MIN and MAX. I would like to explain this.

Let’s assume the intervals are - not regarding the inclusion of the limits - (a,b) and (A,B) with a<=b and A<=B assured. The intersection - if not empty - will then be an interval (L,U) for witch we have to find the limits L and U and the length d=U-L if U>L . If the inteval is empty we won’t get specific limits, but we can describe it by any pair (L,U) with L>U. For the Length we then have to set d = MAX(U-L,0) to get 0 for empty intervals. For nonempty intersection

L = MAX(a,A) and U = MIN(b,B) hold, and we get, whether or not the intersection is empty:

d = MAX( MIN(b,B) - MAX(a,A) , 0)

If we omit the above assurances and take the given intervals to denote the empty set if a>b or A>B respectively, we will get the then correct intersection length 0 in every such case.

Solving the questioners problem, however, we need to add an unknown number of intersection lengths for a single day. This should be done generating the intersections by an array formula under “iterative” evaluation and adding the results. Let’s name the ranges of the StartDateTimes and the EndDateTimes of the tasks aRange and bRange respectively and the start (midnight) of the day under evaluation A, the end of the same day B (=A+1), then we might get the total duration of tasks intersecting with this day by

{=SUM(MAX(MIN(bRange,A+1)-MAX(aRange,A),0)) and this looks pretty simple.

Alas! Every one of the three standard functions used here is one that accepts lists for its parameters - and this means it cannot return arrays by the kind of “iteration” we need. With regard to SUM the solution is very simple. We substitute SUMPRODUCT and get for free the advantage that it has ‘ForceArray’ paramteres and we needn’t even enter the formula explicitly in array mode. For MIN and MAX, however we have to find substitutes. The easiest way to do so is using a few IFs again - and an addtional trick:

=SUMPRODUCT((IF(bRange<A+1;bRange,A+1) - IF(aRange>A;aRange;A)*(IF(CURRENT()<=0;0;1)) where the usage of CURRENT substitutes the final MAX -that is the trick - can help to get better efficiency . I did not find a way to enhance efficiency even more. We still have to accept that the first two IF calls will need a second evaluation of their ranges if the contition comes out TRUE.

I would like to submit an answer for my own question considering no assumptions about what has been written in the question.
Therefore, I do not take the assumption that:

  • task durations are within one, two or any arbitrary number of days.
  • cannot overlaps each other

I found a solution that does not need any temporary columns. It sums all time periods that intersects with the day I want to compute total work duration.
The intersection function does not exists so I used a series of IF tests to emulate it.

… and I cannot post the file due to my low score on this platform

So I am going to try to describe the file here.

First the ‘register’ sheet:

(A) Task	(B) Begin time	(C) End time
foobar	2015-04-02 19:25:00	2015-04-02 19:45:00
foobar	2015-05-06 20:45:00	2015-05-06 22:30:00
foobar	2015-05-07 09:05:00	2015-05-07 12:22:00
foobar	2015-05-09 10:30:00	2015-05-09 11:15:00
foobar	2015-05-09 21:00:00	2015-05-09 23:00:00
...

Then the ‘analysis’ sheet:

(A) Day	(B) Total time
2015-04-02	00:20:00,00
2015-04-03	00:00:00,00
2015-04-04	00:00:00,00
...
2015-05-04	00:00:00,00
2015-05-05	00:00:00,00
2015-05-06	01:45:00,00
2015-05-07	03:17:00,00
2015-05-08	00:00:00,00
2015-05-09	02:45:00,00
...

The computation is in the B column of ‘analysis’ sheet. It is an array formula, here is what is in B2:

{=SUM(IF($register.$C$2:$C$998<$A2;0;IF($register.$C$2:$C$998<($A2+1);IF($register.$B$2:$B$998<$A2;$register.$C$2:$C$998-$A2;$register.$C$2:$C$998-$register.$B$2:$B$998);IF($register.$B$2:$B$998<$A2;1;IF($register.$B$2:$B$998<($A2+1);$A2+1-$register.$B$2:$B$998;0)))))}

I just could not take the cell handle to spread the formula to next rows, I had to manually enter the formula for the new row B3 (replacing all occurrences of B2 with B3) and then select both B2 and B3 and use the cell handle to spread over new rows.

What version opf LibO are you using? There was a bug with filling down/up array formulae introduced with 4.2.8 but it was fixed (with 4.3.4 I think) meanwhile.

I still would prefer using helper columns (which may be hidden, of course. I attached a reworked example (002) to my answer. You may not have inspected it.

I am using version 4.3.7.2.
I did inspect your file. However, I prefer simplicity : I can explain in one single sentence how I do the computation and I only need one formula for this.