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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
ngct1112
Post Patron
Post Patron

PowerQuery - Remove Duplicated lines based on criteria

Hi all, I would like to remove lines with duplicated "ID" and keep the lowest "price" line. May I know if there any quick way(not using groupby) I could do for this case?

Original Table

IDPrice
14
1346
327
4458
5734
56
745
785
77

 

Desired outcome:

IDPrice
14
327
4458
56
77

 

Appreciated if any help

2 ACCEPTED SOLUTIONS
smpa01
Super User
Super User

@ngct1112  try this

CT = Table.TransformColumnTypes(#"Promoted Headers", {{"ID", Int64.Type}, {"Price", Int64.Type}}), 
  #"Filtered Rows" = Table.SelectRows(
    CT, 
    each ([Price] = List.Min(Table.SelectRows(CT, (q) => q[ID] = [ID])[Price]))
  )
Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

View solution in original post

freginier
Solution Specialist
Solution Specialist

Easier way, add sorted rows, buffer and remove duplicate step 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTI0UIrVgTDNwSwnIMsSzjI0UoqNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", Int64.Type}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Column2", Order.Ascending}}),
    #"Buffered" = Table.Buffer(#"Sorted Rows"),
    #"Removed Duplicates" = Table.Distinct(Buffered, {"Column1"})
in
    #"Removed Duplicates"

 

View solution in original post

7 REPLIES 7
freginier
Solution Specialist
Solution Specialist

Easier way, add sorted rows, buffer and remove duplicate step 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTI0UIrVgTDNwSwnIMsSzjI0UoqNBQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", Int64.Type}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Column2", Order.Ascending}}),
    #"Buffered" = Table.Buffer(#"Sorted Rows"),
    #"Removed Duplicates" = Table.Distinct(Buffered, {"Column1"})
in
    #"Removed Duplicates"

 

ngct1112
Post Patron
Post Patron

@smpa01 it works perfectly. Thanks for your solution!

smpa01
Super User
Super User

@ngct1112  try this

CT = Table.TransformColumnTypes(#"Promoted Headers", {{"ID", Int64.Type}, {"Price", Int64.Type}}), 
  #"Filtered Rows" = Table.SelectRows(
    CT, 
    each ([Price] = List.Min(Table.SelectRows(CT, (q) => q[ID] = [ID])[Price]))
  )
Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

This feels like a DAX sort of approach. In M, you want to be very careful about doing table filtering like this since the O(n^2) complexity will make it impractical for larger datasets.

 

In this particular case, we can do this with a simple Group By:

= Table.Group(#"Changed Type", {"ID"}, {{"Price", each List.Min([Price]), type nullable number}})

AlexisOlson_0-1642086663996.png

@AlexisOlson  This feels like a DAX sort of approach -  yes it is  😀 I was simply testing out how can I avoid  groupby (OP wants to avoid groupby)and still get there and translate DAX to M

 

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Ah, this makes much more sense now. I must have skimmed over that particular requirement.

 

Group by is the perfect tool for this, so I was rather confused about why no one suggested it. Turns out I need to learn how to read.

@smpa01  it works perfectly. Thanks for your solution!

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.

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

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

Top Solution Authors