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

Microsoft is giving away 50,000 FREE Microsoft Certification exam vouchers. Get Fabric certified for FREE! Learn more

Reply
Mythicos
Frequent Visitor

Remove rows in Power Query based on multiple logical criteria

Hi,

 

I have a table with 3 columns:

 

[ID]: Whole Number

[Informations]: Text

[Info quality]: Whole Number, from 1 to 4

 

I want to keep one row for each distinct [ID], but the row I keep must be the one with the lowest number in the [Info quality] column.

 

So for example if I have:

 

IDInformationsInfo Quality
235Blue3
425Blue3
425Red2
838White4
235Red2

 

... I want to remove row #1 (ID = 235; Info Quality = 3) instead of row #5 (ID = 235; Info Quality = 2) and remove row #2 (ID = 425; Info Quality = 3) instead of row #3 (ID = 425; Info Quality = 2), ending up with the following:

 

IDInformationsInfo Quality
425Red2
838White4
235Red2

 

Thanks!

2 ACCEPTED SOLUTIONS
SachinNandanwar
Super User
Super User

rowstobedeleted= Table.Sort(Yoursource,{{Quality, Order.Ascending}})

and then you could use Table.Distinct to remove the duplicates.



Regards,
Sachin
Check out my Blog

View solution in original post

Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Informations", type text}, {"Info Quality", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {{"Count", each Table.Min(_,"Info Quality")}}),
    #"Expanded Count" = Table.ExpandRecordColumn(#"Grouped Rows", "Count", {"Informations", "Info Quality"}, {"Informations", "Info Quality"})
in
    #"Expanded Count"

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

8 REPLIES 8
HotChilli
Super User
Super User

Correct. It can't be relied on.

Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"ID", Int64.Type}, {"Informations", type text}, {"Info Quality", Int64.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"ID"}, {{"Count", each Table.Min(_,"Info Quality")}}),
    #"Expanded Count" = Table.ExpandRecordColumn(#"Grouped Rows", "Count", {"Informations", "Info Quality"}, {"Informations", "Info Quality"})
in
    #"Expanded Count"

Hope this helps.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi,

Your code is based off my example, which was a simplified version of my real situation because I wanted to not overburden my question.

My real table has 16 columns, and I see in your code:

 #"Expanded Count" = Table.ExpandRecordColumn(#"Grouped Rows", "Count", {"Informations", "Info Quality"}, {"Informations", "Info Quality"})

... that you refer to the columns other than "ID". Do I need to insert all 15 column names twice? Or is there a short hand for doing this?

Thank!

Hi, Just double click on Expanded Count step and check the box of the column which you want to see in the result.

Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Hi, Just double click on Expanded Count step and check the box of the column which you want to see in the result.

Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
HotChilli
Super User
Super User

Just throwing in a warning about relying on a sort order and using Table.Distinct.  The documentation has some warnings against this.

Can you elaborate a bit? You think in my scenario, there's a risk the lowest value in the sorted column won't be the one preserved post Table.Disctinct?

SachinNandanwar
Super User
Super User

rowstobedeleted= Table.Sort(Yoursource,{{Quality, Order.Ascending}})

and then you could use Table.Distinct to remove the duplicates.



Regards,
Sachin
Check out my Blog

Helpful resources

Announcements
PBIApril_Carousel

Power BI Monthly Update - April 2025

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

Notebook Gallery Carousel1

NEW! Community Notebooks Gallery

Explore and share Fabric Notebooks to boost Power BI insights in the new community notebooks gallery.

April2025 Carousel

Fabric Community Update - April 2025

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