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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

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
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.

Top Solution Authors