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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Gutedama
Frequent Visitor

PowerQuery M Min/Max Contiguous ID Values by Group

Hi everyone,

 

I'm hoping for some assistance with flattening/grouping records across contiguous ranges. My data has 3 key columns that act as a key. My goal is to group columns within these 3 fields but only where the ranges are contiguous. There should never be any range overlap, only breaks between ranges, and there can be significant differences between the range starts and ends between different events and locations (illustrated below). Also there can be any number of events and locations (they'll repeat with some regularity within events but can change). I'm pretty new to M so a little out of my depth here.

 

Here's an example layout of my data:

 

EventLocation_1Location_2Range_StartRange_End
Event_AA1R112
Event_AA1R246
Event_AA2R112
Event_AA2R157
Event_AA2R1811
Event_AA2R11212
Event_AA2R11517
Event_AA2R12123
Event_AA3R31415
Event_BA1R125
Event_BA1R168
Event_BA1R11216
Event_BA1R12020
Event_BA1R234
Event_BA1R259
Event_BA2R31518
Event_BA2R31919


And here's the desired output, flattned for continguous ID ranges by the event, Location_1 and Location_2 fields:

 

EventLocation_1Location_2Range_StartRange_End
Event_AA1R112
Event_AA1R246
Event_AA2R112
Event_AA2R1512
Event_AA2R11517
Event_AA2R12123
Event_AA3R31415
Event_BA1R128
Event_BA1R11216
Event_BA1R12020
Event_BA1R234
Event_BA1R359
Event_BA1R31519


 The fundamental problem (I think) I'm running into is that the number of records +/- to evaluate is dynamic and I can't see an effective way to create a boundary between groups + ranges, and I haven't found a way to delineate between continguous seats within the same grouping to build a better defined group. I've attempted at a high level:

  • Expanding the ranges from a list to individual items and attempting a group by on the other 3 fields with GroupType.Local. Doesn't seem to register the range item as a local break so my min and max are across the full location values, regardless of breaks in the middle.
  • Attempting to add an index column but, again, without knowing how to define the boundary at the range level I'm not sure how to break the index meaningfully.

Would VERY MUCH appreciate anyone's help with this. Been banging my head against this for several hours. Thanks!

1 ACCEPTED SOLUTION
Gutedama
Frequent Visitor

Disregard , I finally found a solution at the link below. A huge thank you to @edhans whose solution there was more than sufficient for me to figure it out. I wasn't aware of how to lag a column's values to create the boundaries I was after in M, that was the missing key in my case.

 

Link to the original thread: https://community.powerbi.com/t5/Power-Query/Grouping-Date-sets-within-a-month/m-p/1428838#M44645

 

However, if anyone has any alternatives aside from lagging a column and then producing a conditional index that would be great. I'm a bit worried about the efficiency of this method as the data grows (could be unfounded though)  and always looking to learn.

View solution in original post

1 REPLY 1
Gutedama
Frequent Visitor

Disregard , I finally found a solution at the link below. A huge thank you to @edhans whose solution there was more than sufficient for me to figure it out. I wasn't aware of how to lag a column's values to create the boundaries I was after in M, that was the missing key in my case.

 

Link to the original thread: https://community.powerbi.com/t5/Power-Query/Grouping-Date-sets-within-a-month/m-p/1428838#M44645

 

However, if anyone has any alternatives aside from lagging a column and then producing a conditional index that would be great. I'm a bit worried about the efficiency of this method as the data grows (could be unfounded though)  and always looking to learn.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

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.