Split a start and end date into set durations

th081

Board Regular
Joined
Mar 26, 2006
Messages
98
Office Version
  1. 365
Platform
  1. Windows
Hi,

I have a table in with a start date and time eg 01/12/20 04:05 and a end date and time 03/12/20 12:07 . These are in separate columns.

I would like to split the duration into 4 hour chunks starting at 01/12/20 00:00 to 04:00 then 04:00 to 08:00 etc. So for the date time in question it would have zero value for 00:00 to 04:00, 235 mins for 04:00 to 08:00 etc. I will then apply a charge per minute to the value shown, the charge per minute is different in each chunk hence the need to split.

Can anyone advise if Power Query can do this or if I would be better using VBA.
 

Excel Facts

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.
just for fun, on cito I did something like this
Duration012345
240 x 001/11/2020
240 x 302/11/202002/11/202002/11/202002/11/2020
240 x 502/11/202002/11/202002/11/202002/11/202002/11/202002/11/2020
 
Last edited:
Upvote 0
or like this with different layout than yours

DaysHoursMinutes012345
073001/11/2020
301202/11/202002/11/202002/11/202002/11/2020
52002/11/202002/11/202002/11/202002/11/202002/11/202002/11/2020
 
Last edited:
Upvote 0
or even like this

DurationDaysHoursMinutes012345
0011501/11/2020
3012202/11/202002/11/202002/11/202002/11/2020
50202002/11/202002/11/202002/11/202002/11/202002/11/202002/11/2020
 
Upvote 0
please check the expected results in post #7, i'm not quite clear about the logic. if you can make more clarification i think it could be done using PQ.
1604293823318.png
 
Upvote 0
In my understanding, the expected result is:

PQ小题.xlsx
ABCDEFGHIJKLMNO
1Start Date and TimeEnd Date and Time2020/11/1 00:00:00
22020/11/2 04:05:002020/11/2 16:07:00Slices (hours)4 hours
32020/11/2 00:25:002020/11/2 20:45:00End Date and Time2020/11/3 04:00:00
42020/11/1 01:30:002020/11/1 02:45:00
5
6
7Results
8
9
10Start Date and TimeEnd Date and Time2020/11/1 0:00:002020/11/1 4:00:002020/11/1 8:00:002020/11/1 12:00:002020/11/1 16:00:002020/11/1 20:00:002020/11/2 0:00:002020/11/2 4:00:002020/11/2 8:00:002020/11/2 12:00:002020/11/2 16:00:002020/11/2 20:00:002020/11/3 0:00:00
112020/11/2 04:05:002020/11/2 16:07:000000000235240240700
122020/11/2 00:25:002020/11/2 20:45:00000000215240240240240450
132020/11/1 01:30:002020/11/1 02:45:0075000000000000
Sheet5


Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table10"]}[Content],
    start = DateTime.From("2020/11/1 0:00:00"),
    end = DateTime.From("2020/11/3 4:00:00"),
    lst = List.DateTimes(start,Number.From(end-start)*6, #duration(0,4,0,0)),
    res = List.Accumulate(
                            lst,
                            Source,
                            (x,y)=>
                                    Table.AddColumn(
                                                    x,
                                                    Text.From(y),
                                                    (z)=>
                                                          let
                                                              s=z[Start Date and Time],
                                                              e=z[End Date and Time],
                                                              d=#duration(0,4,0,0),
                                                              a=if y+d<=s then 0
                                                                else
                                                                    (if y+d>=e then e else y+d)-(if s<=y then y else s),
                                                              b=Number.From(a)*1440
                                                          in
                                                              if b<0 then 0 else b
                                                    )
    )
in
    res
 
Upvote 0

Forum statistics

Threads
1,215,202
Messages
6,123,625
Members
449,109
Latest member
Sebas8956

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top