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

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! Register now.

Reply
Anonymous
Not applicable

Group records - keeping first Start Date (oldest) and last End Date (most recent)

Hey there,

I'm new to Power Query, so I hope someone can help me with this query. 

 

I have a dataset with ID, Status, Start Date and End Date. There are multiple rows for each ID with different start and end dates. Here's a sample of my dataset.

 

IDStatusStart DateEnd Date
1A01/04/201528/05/2015
1A28/05/201515/06/2016
1B15/06/201619/06/2016
1B19/06/201631/07/2016
1B31/07/2016 
2B01/03/201703/06/2018
2A03/06/201807/08/2018
2A07/08/201831/12/2018
2C31/12/201801/09/2019
2C01/09/201903/05/2020
2A03/05/2020

 

3C01/02/2020

03/05/2020

3C03/05/2020

05/08/2020

 

I want to group consecutive rows (End Date same as the Start Date of next row) with same status for each ID and take the oldest Start Date and the most recent End Date. Here is the the output I'm looking for. 

IDStatusStart DateEnd Date
1A01/04/201515/06/2016
1B15/06/2016 
2B01/03/201703/06/2018
2A03/06/201831/12/2018
2C31/12/201803/05/2020
2A03/05/2020 
3C01/02/202005/08/2020

 

Is this possible? 

 

Many Thanks for your help.

 

1 ACCEPTED SOLUTION
Payeras_BI
Solution Sage
Solution Sage

Hi @Anonymous ,

Payeras_BI_0-1627025705705.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bZBLDoUgDEW3Yjom6YeHwFDfMoz734YtEKXRAYN7esItHAcwBNj0ECP9UIiTBilIqYczPM6EA3BCWi2st7N7rKF+O3VyohbntzPhAEsbyRjZqtFG2UIcd5Xb2TzWkJHKh/Pg1sfinb/HvbhaqM6ZcC+2PxJ67TNwe855AQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Status = _t, #"Start Date" = _t, #"End Date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Status", type text}, {"Start Date", type date}, {"End Date", type date}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"ID", "Status"}, {{"Start Date", each List.First([Start Date]), type nullable date}, {"End Date", each List.Last([End Date]), type nullable date}},GroupKind.Local

)
in
    #"Grouped Rows"

 

If this post answered your question, please mark it as a solution to help other users find useful content.
Kudos are another nice way to acknowledge those who tried to help you.

J. Payeras
Mallorca, Spain

View solution in original post

4 REPLIES 4
Payeras_BI
Solution Sage
Solution Sage

Hi @Anonymous ,

Payeras_BI_0-1627025705705.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("bZBLDoUgDEW3Yjom6YeHwFDfMoz734YtEKXRAYN7esItHAcwBNj0ECP9UIiTBilIqYczPM6EA3BCWi2st7N7rKF+O3VyohbntzPhAEsbyRjZqtFG2UIcd5Xb2TzWkJHKh/Pg1sfinb/HvbhaqM6ZcC+2PxJ67TNwe855AQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Status = _t, #"Start Date" = _t, #"End Date" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Status", type text}, {"Start Date", type date}, {"End Date", type date}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"ID", "Status"}, {{"Start Date", each List.First([Start Date]), type nullable date}, {"End Date", each List.Last([End Date]), type nullable date}},GroupKind.Local

)
in
    #"Grouped Rows"

 

If this post answered your question, please mark it as a solution to help other users find useful content.
Kudos are another nice way to acknowledge those who tried to help you.

J. Payeras
Mallorca, Spain
Anonymous
Not applicable

Hi @Payeras_BI,

 

Related to the above question,  if there is a break in dates for the same ID and status, then that should be kept as a different group. 

IDStatusStart DateEnd Date
1A01/04/201528/05/2015
1A28/05/201515/06/2016
1A01/05/201731/03/2018
1A01/04/201903/04/2019
1B15/06/201619/06/2016
1B19/06/201631/07/2016
1B31/07/201610/09/2016
1A10/09/2016 

For e.g rows 3 and 4 in the table above, should be as in the table below,

IDStatusStart DateEnd Date
1A01/04/201515/06/2016
1A01/05/201731/03/2018
1A01/04/201903/04/2019
1B15/06/201610/09/2016
1A10/09/2016 

 

Is this possible at all? Please can you/someone help me with this?

Many Thanks.

Hi @Anonymous ,

In this case try with this code:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ZY5LDsAgCAWv0rA2AWr9Les1jPe/RkHTVsOCxbwMPFoDBge3DDHShSdxEDgzUpjQ3e8ssQMOSFEhbo7eGU4S8AJeIRtndBUF/8Hr1P28QDFddY9nV7LOEssCIRX78xI7OKD3Bw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Status = _t, #"Start Date" = _t, #"End Date" = _t]),
    ChangedType = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Status", type text}, {"Start Date", type date}, {"End Date", type date}}),
    #"Added Index" = Table.AddIndexColumn(ChangedType, "Index", 0, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "GroupBy", each let 
     Check1 = try Duration.Days(ChangedType[Start Date]{[Index]} - ChangedType[End Date]{[Index]-1}) otherwise 1,
     Check2 = try ChangedType[ID]{[Index]} = ChangedType[ID]{[Index]-1} otherwise false,
     Check3 = try ChangedType[Status]{[Index]} = ChangedType[Status]{[Index]-1} otherwise false
     in 
     if Check1 = 0 and Check2 =true and Check3 = true then null else [Start Date], type date),
    #"Filled Down" = Table.FillDown(#"Added Custom",{"GroupBy"}),
    #"Grouped Rows" = Table.Group(#"Filled Down", {"ID", "Status", "GroupBy"}, {{"Start Date", each List.Min([Start Date]), type nullable date}, {"End Date", each List.Max([End Date]), type nullable date}}),
    #"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"GroupBy"})
in
    #"Removed Columns"

 

If this post answered your question, please mark it as a solution to help other users find useful content.
Kudos are another nice way to acknowledge those who tried to help you.

J. Payeras
Mallorca, Spain
Anonymous
Not applicable

Thanks so much @Payeras_BI, exactly what I needed. Much appreciated..

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Kudoed Authors