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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
CJLewis87
Frequent Visitor

Grouping rows by min start data and max end date, only with consecutive dates (Episode)

 

Hi All

 

I'm new to the forum and am currently enrolled in a Data Analyst apprenticeship with my employer (due to finish in the next few months). I have been using Power Query for the past couple of years and have recently started my journey with Power BI/DAX. I am running into an issue where i want to group service lines together from our system into proper "episodes". In the test example I've attached below, the same ID number has had 4 service lines with the same provider, giving me 2 actual episodes (1st April to 14th June and then again from 18th June to 30th June). Thus, I want to group the service lines together by ID number/name/provider with the min start date and max end date, but only where there are no gaps between one service lines end date and the next lines start date. 

 

I am hitting a bit of a brick wall trying to get my head around how I can go about this. I have done some googling and plenty of searches on YouTube, but haven't managed to find a solution so far.


Any help would be much appreciated 

Thanks

Cal

Service Line Grouping Episodes.png

 

1 ACCEPTED SOLUTION
slorin
Super User
Super User

Hi

 

let
Source = YourSource,
#"EndDate+1" = Table.AddColumn(Source, "End Date+1", each Date.AddDays([End Date],1)),
Join = Table.NestedJoin(#"EndDate+1", {"ID Number", "Start Date"}, #"EndDate+1", {"ID Number", "End Date+1"}, "NextRow?", JoinKind.LeftOuter),
Test_Group = Table.TransformColumns(Join,{{"NextRow?", each Table.IsEmpty(_), type logical}}),
LocalGroup = Table.Group(
Test_Group,
{"ID Number", "Name", "Provider", "NextRow?"},
{{"Start Date", each List.Min([Start Date]), type date}, {"End Date", each List.Max([End Date]), type date}},
GroupKind.Local,
(x,y) => if y[#"NextRow?"] then 1 else 0)
in
LocalGroup

 Stéphane

View solution in original post

3 REPLIES 3
CJLewis87
Frequent Visitor

Brilliant, works as needed. Thanks so much

slorin
Super User
Super User

Hi

 

let
Source = YourSource,
#"EndDate+1" = Table.AddColumn(Source, "End Date+1", each Date.AddDays([End Date],1)),
Join = Table.NestedJoin(#"EndDate+1", {"ID Number", "Start Date"}, #"EndDate+1", {"ID Number", "End Date+1"}, "NextRow?", JoinKind.LeftOuter),
Test_Group = Table.TransformColumns(Join,{{"NextRow?", each Table.IsEmpty(_), type logical}}),
LocalGroup = Table.Group(
Test_Group,
{"ID Number", "Name", "Provider", "NextRow?"},
{{"Start Date", each List.Min([Start Date]), type date}, {"End Date", each List.Max([End Date]), type date}},
GroupKind.Local,
(x,y) => if y[#"NextRow?"] then 1 else 0)
in
LocalGroup

 Stéphane

Many thanks for your response. I will have a play with this later on and will accept solution if all working 😀

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.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.