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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

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
Responsive Resident
Responsive Resident

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

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

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
Responsive Resident
Responsive Resident

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

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

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

AugPowerBI_Carousel

Power BI Monthly Update - August 2024

Check out the August 2024 Power BI update to learn about new features.

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

Sept NL Carousel

Fabric Community Update - September 2024

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