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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

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
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 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