Keep Earliest record of ID in Power Query

ExcelAtEverything

Active Member
Joined
Jan 30, 2021
Messages
351
Office Version
  1. 2019
Platform
  1. Windows
Hello!
I'm looking for some code to plugin to the query editor which will allow me to keep only the earliest date for each Item code / Shipment ID combination. Here's what I mean exactly.
  • The middle column shows the item code for each item sold.
  • The left column shows the shipment ID# for the delivery package of that actual unit when it was delivered. showing a unique shipment ID for each time it was delivered.
  • The right column are sale dates for each time an item was sold.
For each & every item code, I need to find which shipment ID it came in on. Then find the earliest date within those constraints & keep that row, while filtering out the others with later sale dates. Then move on to the next item code. There will be identical item codes but with different shipment IDs, because they were received at a different time & therefore are part of a different batch.

This will be the very last line of code before my "in". Here is the 1st part of the line where I want to insert the code after:
Power Query:
 #"Renamed Columns4" = Table.RenameColumns(#"Extracted Last Characters",

1639672750251.png
 

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.
try

Power Query:
    tbl1 = Table.Group(#"Renamed Columns4", {"Item Code"}, {{"All", each Table.SelectRows(_, (x)=> x[Date] = List.Min(_[Date]))}}),
    tbl2 = Table.ExpandTableColumn(tbl1, "All", {"Shipment ID", "Date"}, {"Shipment ID", "Date"}),
    Result = Table.ReorderColumns(tbl2,{"Shipment ID", "Item Code", "Date"})
 
Upvote 0
If all you need is the earliest date, then group by the item code and select Min for the Date.
If you need the shipment ID, then you will need to dupe the original query and join (merge) it with original on the Item Code.

If you need explicit Mcode, then upload a representative sample of 15-20 records using XL2BB. If unfamiliar with this function, look at my signature. We cannot manipulate data in a picture and your current picture does not provide enough data with different dates and shipment varieties
 
Upvote 0
Solution

Forum statistics

Threads
1,215,197
Messages
6,123,585
Members
449,108
Latest member
rache47

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