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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Anonymous
Not applicable

Group by function in Power Query

I have a mock data table like this:

ItemPurchase Order numberShip dateQuantity
AANR13/2/20204
AANR23/5/20205
BANR56/5/20203
BANR95/4/20204
BANR44/23/20204
CANR85/12/202012

 

I want to group by Item, then get me the earliest Ship date, and the accordingly quantity of that shipment. Result should look like this:

ItemShip dateQuantity
A3/2/20204
B4/23/20204
C5/12/202012

 

Group by function in Power Query let you do all the aggregating fun stuff. I do need that to get out the Min (Ship date), but after that i just want to grab the Qty without any aggregation ( to prevent case that there are 2 orders ship on same date with different quantity). Any advice? Thanks in advance!

 

 
 
1 ACCEPTED SOLUTION
ImkeF
Community Champion
Community Champion

Hi @Anonymous ,

 

Doing it via grouping is a bit clunky in my eyes, so you can also check out the version via duplicate removal:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Vc49DoAgDAXgu3Qm6Q+t0RHdHVwJ97+GYJHA1PR96UtzhgQB0v1wHRSRBIWE6qJQwlDpar/ap6erNd0mjbMeTQ1J1+au2gKUuOLluPspj6dYoJQX", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Item = _t, #"Purchase Order number" = _t, #"Ship date" = _t, Quantity = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Item", type text}, {"Purchase Order number", type text}, {"Ship date", type date}, {"Quantity", Int64.Type}}, "en-us"),
    GroupWithAddedPartition = Table.Group(#"Changed Type", {"Item"}, {{"Earliest Date", each List.Min([Ship date]), type date}, {"ItemPartition", each _, type table [Item=text, Purchase Order number=text, Ship date=date, Quantity=number]}}),
    FilterOnEarliestDate = Table.AddColumn(GroupWithAddedPartition, "FilterEarliestDate", each Table.SelectRows([ItemPartition], (x) => x[Ship date] = [Earliest Date])),
    GetQuantity = Table.AddColumn(FilterOnEarliestDate, "GrabQuantityColumn", each [FilterEarliestDate][Quantity]{0}),
    Cleanup = Table.RemoveColumns(GetQuantity,{"ItemPartition", "FilterEarliestDate"})
in
    Cleanup

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Vc49DoAgDAXgu3Qm6Q+t0RHdHVwJ97+GYJHA1PR96UtzhgQB0v1wHRSRBIWE6qJQwlDpar/ap6erNd0mjbMeTQ1J1+au2gKUuOLluPspj6dYoJQX", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Item = _t, #"Purchase Order number" = _t, #"Ship date" = _t, Quantity = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Item", type text}, {"Purchase Order number", type text}, {"Ship date", type date}, {"Quantity", Int64.Type}}, "en-us"),
    SortByShipDate = Table.Buffer( Table.Sort(#"Changed Type",{{"Ship date", Order.Ascending}}) ),
    #"Removed Duplicates" = Table.Distinct(SortByShipDate, {"Item"})
in
    #"Removed Duplicates"

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

3 REPLIES 3
ImkeF
Community Champion
Community Champion

Hi @Anonymous ,

 

Doing it via grouping is a bit clunky in my eyes, so you can also check out the version via duplicate removal:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Vc49DoAgDAXgu3Qm6Q+t0RHdHVwJ97+GYJHA1PR96UtzhgQB0v1wHRSRBIWE6qJQwlDpar/ap6erNd0mjbMeTQ1J1+au2gKUuOLluPspj6dYoJQX", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Item = _t, #"Purchase Order number" = _t, #"Ship date" = _t, Quantity = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Item", type text}, {"Purchase Order number", type text}, {"Ship date", type date}, {"Quantity", Int64.Type}}, "en-us"),
    GroupWithAddedPartition = Table.Group(#"Changed Type", {"Item"}, {{"Earliest Date", each List.Min([Ship date]), type date}, {"ItemPartition", each _, type table [Item=text, Purchase Order number=text, Ship date=date, Quantity=number]}}),
    FilterOnEarliestDate = Table.AddColumn(GroupWithAddedPartition, "FilterEarliestDate", each Table.SelectRows([ItemPartition], (x) => x[Ship date] = [Earliest Date])),
    GetQuantity = Table.AddColumn(FilterOnEarliestDate, "GrabQuantityColumn", each [FilterEarliestDate][Quantity]{0}),
    Cleanup = Table.RemoveColumns(GetQuantity,{"ItemPartition", "FilterEarliestDate"})
in
    Cleanup

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("Vc49DoAgDAXgu3Qm6Q+t0RHdHVwJ97+GYJHA1PR96UtzhgQB0v1wHRSRBIWE6qJQwlDpar/ap6erNd0mjbMeTQ1J1+au2gKUuOLluPspj6dYoJQX", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Item = _t, #"Purchase Order number" = _t, #"Ship date" = _t, Quantity = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Item", type text}, {"Purchase Order number", type text}, {"Ship date", type date}, {"Quantity", Int64.Type}}, "en-us"),
    SortByShipDate = Table.Buffer( Table.Sort(#"Changed Type",{{"Ship date", Order.Ascending}}) ),
    #"Removed Duplicates" = Table.Distinct(SortByShipDate, {"Item"})
in
    #"Removed Duplicates"

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Anonymous
Not applicable

Hi @ImkeF 

Superb! Works like a charm!

Since you're here 😆 What do you suggest to not only sort out the date that is the earliest ship date, but also filter that that date is a future ship date (>=today). I thought about adding a column with DateTime.LocalNow(), then compare ship date with that and mark each line either "Past shipment" or "Future shipment" and then filter for just "Future shipment" and from there use your solution to get the earliest. Any word of advice? Thanks alot!

ImkeF
Community Champion
Community Champion

Hi @Anonymous ,

yes, that would be a solution.

But you could also "simply" filter out future ship dates on the date column itself with relative date filters. Same effect: sned just future transactions to the subsequent steps, but no need for additional column. But that might be only cosmetic and doesn't effect the query performance.

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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