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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
LouiseSemaj
Frequent Visitor

Group rows based on values in adjacent rows

I am trying to group adjacent rows (based on 'Rock'), then show the min/max of another field (From and To).  This would be OK, but one of the Rock codes (Tf) is repeated further down the table and needs to be a seperate ros in the results.  See below the raw data and what I am chasing. I am using Power query please.  Any assistance would be much appreciated.  Many thanks

 

From This    To this   
HoleNoFromToRock  HoleNoFromToRock
Hole0101Qac  Hole0104Qac
Hole0112Qac  Hole0149Tf
Hole0123Qac  Hole01910Tk
Hole0134Qac  Hole011015Tm
Hole0145Tf  Hole011519Tf
Hole0156Tf      
Hole0167Tf      
Hole0178Tf      
Hole0189Tf      
Hole01910Tk      
Hole011011Tm      
Hole011112Tm      
Hole011213Tm      
Hole011314Tm      
Hole011415Tm      
Hole011516Tf      
Hole011617Tf      
Hole011718Tf      
Hole011819Tf      
1 ACCEPTED SOLUTION
TomMartens
Super User
Super User

Hey,

 

here you will find a PBIX file that creates this table base on your raw data:

image.png

 

There are some twists for this reason the following explains all the steps.

 

Step Grouped Rows
Basically I started with the transform "Group by" using the advanced settings to be able to group by more than one column.
I choose as "All Rows" as Grouping Operation.

The M formula that will be generated look like this:

= Table.Group(#"Changed Type", {"HoleNo", "Rock"}, {{"Columns", each _, type table}})

But this would not consider that there is a 2nd group "TF", this reason I tweaked the generated M Formula by adding GroupKind.Local (https://msdn.microsoft.com/en-us/query-bi/m/table-group)

= Table.Group(#"Changed Type", {"HoleNo", "Rock"}, {{"Columns", each _, type table}}, GroupKind.Local)

Now I get 5 groups instead of 4 🙂

Please be aware that you will not be able to use the Grouping Dialog any longer 😉

 

Added Index: Adding an Index Column (starting with 1)

 

Expanded Count: Table Expansion
I expanded the table without using a suffix, selecting just the missing columns.

 

Grouped Rows1: Group by (Index - Operation "All Rows"

Another "Group by" this time by freshly generated Index column. This will create the following M Formula:

= Table.Group(#"Expanded Count", {"Index"}, {{"Columns", each _, type table}})

Now I'm repacing the bold part of formula by this snippet:

{
{"AllRows", each _, Value.Type(#"Expanded Count")}, 
{"Minimum From", each List.Min([From]), type number},
{"Maximum To", each List.Max([To]), type number}
}

That finally leads to this formula:

= Table.Group(#"Expanded Count", 
{"Index"}, 
{
{"AllRows", each _, Value.Type(#"Expanded Count")}, 
{"Minimum From", each List.Min([From]), type number},
{"Maximum To", each List.Max([To]), type number}
}
)

The result will look like this:

image.png

 

Expanded AllRows (Table Expansion)

Once again I expand the table to get back the missing columns.

 

Removed Columns (Removing unwanted columns)

 

Removed Duplicates

 

Voila

 

Hopefully this is what you are looking for

 

Regards

Tom

 

 



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

View solution in original post

7 REPLIES 7
TomMartens
Super User
Super User

Hey,

 

here you will find a PBIX file that creates this table base on your raw data:

image.png

 

There are some twists for this reason the following explains all the steps.

 

Step Grouped Rows
Basically I started with the transform "Group by" using the advanced settings to be able to group by more than one column.
I choose as "All Rows" as Grouping Operation.

The M formula that will be generated look like this:

= Table.Group(#"Changed Type", {"HoleNo", "Rock"}, {{"Columns", each _, type table}})

But this would not consider that there is a 2nd group "TF", this reason I tweaked the generated M Formula by adding GroupKind.Local (https://msdn.microsoft.com/en-us/query-bi/m/table-group)

= Table.Group(#"Changed Type", {"HoleNo", "Rock"}, {{"Columns", each _, type table}}, GroupKind.Local)

Now I get 5 groups instead of 4 🙂

Please be aware that you will not be able to use the Grouping Dialog any longer 😉

 

Added Index: Adding an Index Column (starting with 1)

 

Expanded Count: Table Expansion
I expanded the table without using a suffix, selecting just the missing columns.

 

Grouped Rows1: Group by (Index - Operation "All Rows"

Another "Group by" this time by freshly generated Index column. This will create the following M Formula:

= Table.Group(#"Expanded Count", {"Index"}, {{"Columns", each _, type table}})

Now I'm repacing the bold part of formula by this snippet:

{
{"AllRows", each _, Value.Type(#"Expanded Count")}, 
{"Minimum From", each List.Min([From]), type number},
{"Maximum To", each List.Max([To]), type number}
}

That finally leads to this formula:

= Table.Group(#"Expanded Count", 
{"Index"}, 
{
{"AllRows", each _, Value.Type(#"Expanded Count")}, 
{"Minimum From", each List.Min([From]), type number},
{"Maximum To", each List.Max([To]), type number}
}
)

The result will look like this:

image.png

 

Expanded AllRows (Table Expansion)

Once again I expand the table to get back the missing columns.

 

Removed Columns (Removing unwanted columns)

 

Removed Duplicates

 

Voila

 

Hopefully this is what you are looking for

 

Regards

Tom

 

 



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

Adapted this to create a consecutive hour group for an hour category from a dateTime dimension table. Absolutely awesome.

pdsoutherland_0-1669851153945.png

 

  

Awesome,  I think this is just what I am chasing. How would I access and read the PBIX file please?  Many thanks Tom

Hey,

if you don't have Power BI Desktop installed on your machine, you can get from here www.powerbi.com (it's free). Maybe you get asked to sign in 😉 Just press "I already have an account", close the next dialog using the the cross in the top right corner. Now you are able to use my already downloaded pbix file, Hit Edit queries and there you will find my query 😀

Regards Tom


Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

Thats it.  Done.  Many thanks Tom Working a treat.  Kind regards

TomMartens
Super User
Super User

Hey,

 

is it valid to assume that the table can be ordered by HoleNo ASCENDING and From ASCENDING

 

And if I got the table in that order it will be valid that each time when ROC(from current row) <> ROC(from previous row) a new "grouping" starts?

 

Regards

Tom 



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

Many thanks Tom,  Yes will always be soted such

Helpful resources

Announcements
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.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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