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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

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
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors