Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
limtj
Frequent Visitor

how to group consecutive date/time range with start and end timing

Hi all, 

 

Tried to find existing solutions but cant seem to find a one that solves my problem. 

 

I am trying to transform my data in power query of Power BI. 

 

I have a data table like the below and would like to transform it to one that groups consec same status and date time to reflect start and end time of each status instead. 

 

Thanks for the help in advance!

 

Desired Format

Vehicle NumberStart TimeEnd TimeStatus Mode
Vehicle A14/3/2023 0900hr14/3/2023 0924hrA
Vehicle A14/3/2023 0924hr14/3/2023 1010hrB
Vehicle B14/3/2023 0856hr14/3/2023 0958hrA
Vehicle B14/3/2023 0958hr14/3/2023 1111hrB

 


My Data Currently

DateTimeVehicle NumberStatus Mode
14/3/2023 0900hrVehicle AStatus A
14/3/2023 0910hrVehicle AStatus A
14/3/2023 0924hrVehicle AStatus B
14/3/2023 0928hrVehicle AStatus B
14/3/2023 1010hrVehicle AStatus A
14/3/2023 0856hrVehicle BStatus A
14/3/2023 0914hr Vehicle BStatus A
14/3/2023 0958hr Vehicle BStatus B
14/3/2023 1058hrVehicle BStatus B
14/3/2023 1111hrVehicle B

Status A

2 REPLIES 2
Jakinta
Solution Sage
Solution Sage

This should do the trick.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjTRN9Y3MjAyVjCwNDDIKFLSUQpLzchMzklVcASyHZVidVAVGRKjyMgEQ5ETpiILgooMDYixzsLUDEWRE3aHmxChyNQCQxGmm4hRBATYrIsFAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [DateTime = _t, #"Vehicle Number" = _t, #"Status Mode" = _t]),
    Group1 = Table.Group(Source, {"Vehicle Number", "Status Mode"}, {{"Start Time", each _[DateTime]{0}, type table }}, GroupKind.Local),
    Group2 = Table.Combine ( Table.Group(Group1, {"Vehicle Number"}, {{"A", each let t=_ in Table.RemoveColumns ( Table.RemoveLastN ( Table.AddColumn ( Table.AddIndexColumn(t,"i",1), "End Time", each t[Start Time]{[i]}), 1), {"i"}), type table }}) [A] )
in
    Group2
limtj
Frequent Visitor

hi @Jakinta ,thanks much for your response. 

Unfortunately, i tried out the code but i dont get the grouping that i require. The vehicle and status code are still ungrouped and the start and end timing are all just 5 seconds apart. Im not sure what is wrong.

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Top Solution Authors