Remove duplicate rows based on max value of a different column

Zanith

New Member
Joined
Feb 12, 2013
Messages
21
Hi All,

I'm attempting to filter some of my data and I'm having problems getting it correct. I'm pulling my data in from SQL and cleaning in up in power query before visualizing it in Power BI. This is data of sales orders captured in our system which, over time, might have modifications made to them resulting in our system archiving off the older versions and applying a version number to each row of data. Each sales order has it's own unique number (Sales Order Number) which can have multiple rows of data (a row for each item on the sales order) and, as a result of the archiving process, each row will have a Version number.

I need to be able to extract all rows of a Sales Order Number which match the max value of the Version.

A rough example of the data I'm pulling is below.


SALES ORDER NUMBERVERSIONITEM
100001Item 1
100001Item 2
100001Item 3
100002Item 1
100002Item 2
100002Item 3
100003Item 1
100003Item 2
100003Item 3
100011Item 1
100012Item 1
100021Item 3
100031Item 2
100031Item 4
100032Item 2
100032Item 4

<tbody>
</tbody>


And here is an example of how I need this data to be transformed.


SALES ORDER NUMBERVERSIONITEM
100003Item 1
100003Item 2
100003Item 3
100012Item 1
100021Item 3
100032Item 2
100032Item 4

<tbody>
</tbody>


Any help is appreciated!
 

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
You can first group on Order and version, with aggregation operation "All Rows".
Next you can do the same, but only on Order, and adjust the generated code to select only the maximum version number (for each order).
Now you can expand the appropriate columns (twice).

Generated/adjusted code (only step #"Grouped Rows1" adjusted):

Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"SALES ORDER NUMBER", Int64.Type}, {"VERSION", Int64.Type}, {"ITEM", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"SALES ORDER NUMBER", "VERSION"}, {{"AllData", each _, type table}}),
    #"Grouped Rows1" = Table.Group(#"Grouped Rows", {"SALES ORDER NUMBER"}, {{"AllData", each Table.Max(_, each [VERSION]), type record}}),
    #"Expanded AllData" = Table.ExpandRecordColumn(#"Grouped Rows1", "AllData", {"VERSION", "AllData"}, {"VERSION", "AllData.1"}),
    #"Expanded AllData.1" = Table.ExpandTableColumn(#"Expanded AllData", "AllData.1", {"ITEM"}, {"ITEM"})
in
    #"Expanded AllData.1"
 
Upvote 0

Forum statistics

Threads
1,217,398
Messages
6,136,394
Members
450,008
Latest member
guptasweb

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top