Forum Discussion

pmsilvbi's avatar
pmsilvbi
Frequent Visitor
3 years ago
Solved

Finding the penultimate date from a list

Hi All

I have a table containing all stock codes and the dates they were purchased. One Stock code may have been purchased multiple times.

I am trying to show the last time each stock code was purcahsed, the qty ordered and the price paid as well as the previous time it was purcahsed, qty and the price paid then.

I have managed to extract the last purchase date by grouping the stock codes then using list.max to extract the required information. But do I need to maybe create an index on the original data to lookup the required information? I have some 170,000 rows

 

SupplierStockCodePoDatePurchaseOrderPurchaseOrderLinLineDueDateDateReceivedQtyReceivedPriceReceived
EDENT/A014/003W05/06/2023000000000078577112/06/202328/06/2023649.9
EDENT/A014/003W30/03/2022000000000075200115/04/202229/04/20221249.9
EDENT/A014/003W01/03/2021000000000071790112/03/202123/03/2021635
EDENT/A014/003W19/11/2020000000000070866330/11/202009/12/20206.16235
EDENT/A014/003W31/10/2019000000000067684108/11/201925/11/2019635
EDENT/A014/003W12/12/2017000000000061481115/12/201707/02/20181227
EDENT/A014/003W18/03/2016000000000055865124/03/201612/04/20161227
EDENT/A014/003W18/03/2016000000000055865124/03/201619/04/2016127
EDENT/A014/003W10/07/2014000000000050078118/07/201423/07/201413.8654
EDENT/A014/003W06/11/2012000000000044619122/11/201227/11/201213.38427.2
EDENT/A014/003W02/06/2011000000000040319117/06/201123/08/20111227.2
EDENT/A014/003W22/10/2009000000000035762130/10/200909/11/200912.00827.2
EDENT/A014/003W23/07/2004000000000016338130/07/200405/08/20046.48421.57
EDENT/A014/003W16/01/2003000000000010282124/01/200307/02/2003616.5
EDENT/A014/003W21/05/2002000000000008129123/05/200223/05/20026.5716.5
EDENT/A014/003W07/11/2001000000000006491609/11/200108/11/20016.98816.5
EDENT/A014/003W10/01/2001000000000003761112/01/200115/01/2001617.45

Any assistance would be greatly apprecaited

  • Use List.MaxN(Your_List,2){1}

    However, if you are returning multiple columns in a "group", you might be better served using Table.MaxN

     

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("rVQxbsMwDPxL5kAiKYmixgLN2qlAhyD//0ZFWbJJoLGXGvBwxvmOIk98Pm+Pz8fX7X77jh+AOQKkn46gROBIQEnB/lQptfYvqC8ZCokB3N/cQru97m/kE0RISicvXwhgyfcK8qJQMwDpUh9w6qPXx9rAlL9TKBmg5afyXhxbRFQyeHEQHr9u5zOUzqcFOCDThUHCiNB/wOYMuLLkWT3IZjAoVAy4rJ62arB6ccyCR+sNpUYYQFbrqZ6oy9ZIZKdeinCZ6pQNRaeQD/Df6s2qX4n3TFYlZy+uqV+NEUPRzOwAUxA1KfkklDzH5EOfM4/BjfrIUKga0A3SmD7VQCce81KiD36GtHtgNRQ9hCwwXc/0tUCNJvhoplJHqnFm/6DMuzIAUgCQa4/ZWPCDQE5JDg9DKdsZBuCQtzZhKGfD5gijLr/fEEjWOTRJB2XeAlj7DTmcXDLq+6co3Y8aBGkfdTIUB1grv3KAGQ7wgwbObe2wvfd2YQzAoYlcWuiF+MsiVd43BRmKbuzDYiQt5K7/+gU=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Supplier = _t, StockCode = _t, PoDate = _t, PurchaseOrder = _t, PurchaseOrderLin = _t, LineDueDate = _t, DateReceived = _t, QtyReceived = _t, PriceReceived = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{
            {"Supplier", type text}, {"StockCode", type text}, {"PoDate", type date}, {"PurchaseOrder", Int64.Type}, 
            {"PurchaseOrderLin", Int64.Type}, {"LineDueDate", type date}, {"DateReceived", type date}, 
            {"QtyReceived", type number}, {"PriceReceived", type number}},"en-150"),
        
        #"Grouped Rows" = Table.Group(#"Changed Type", {"StockCode"}, {
            {"all", (t)=>Table.MaxN(t, each [PoDate],2), 
            type table [Supplier=nullable text, StockCode=nullable text, PoDate=nullable date, PurchaseOrder=nullable number, PurchaseOrderLin=nullable number, LineDueDate=nullable date, DateReceived=nullable date, QtyReceived=nullable number, PriceReceived=nullable number]}}),
        #"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"StockCode"}),
        #"Expanded all" = Table.ExpandTableColumn(#"Removed Columns", "all", 
            {"Supplier", "StockCode", "PoDate", "PurchaseOrder", "PurchaseOrderLin", "LineDueDate", "DateReceived", "QtyReceived", "PriceReceived"})
    in
        #"Expanded all"

     

     

    In the #"Expanded All" step you can deselect the columns you might not want to show.

     

     

2 Replies

  • Use List.MaxN(Your_List,2){1}

    However, if you are returning multiple columns in a "group", you might be better served using Table.MaxN

     

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("rVQxbsMwDPxL5kAiKYmixgLN2qlAhyD//0ZFWbJJoLGXGvBwxvmOIk98Pm+Pz8fX7X77jh+AOQKkn46gROBIQEnB/lQptfYvqC8ZCokB3N/cQru97m/kE0RISicvXwhgyfcK8qJQMwDpUh9w6qPXx9rAlL9TKBmg5afyXhxbRFQyeHEQHr9u5zOUzqcFOCDThUHCiNB/wOYMuLLkWT3IZjAoVAy4rJ62arB6ccyCR+sNpUYYQFbrqZ6oy9ZIZKdeinCZ6pQNRaeQD/Df6s2qX4n3TFYlZy+uqV+NEUPRzOwAUxA1KfkklDzH5EOfM4/BjfrIUKga0A3SmD7VQCce81KiD36GtHtgNRQ9hCwwXc/0tUCNJvhoplJHqnFm/6DMuzIAUgCQa4/ZWPCDQE5JDg9DKdsZBuCQtzZhKGfD5gijLr/fEEjWOTRJB2XeAlj7DTmcXDLq+6co3Y8aBGkfdTIUB1grv3KAGQ7wgwbObe2wvfd2YQzAoYlcWuiF+MsiVd43BRmKbuzDYiQt5K7/+gU=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Supplier = _t, StockCode = _t, PoDate = _t, PurchaseOrder = _t, PurchaseOrderLin = _t, LineDueDate = _t, DateReceived = _t, QtyReceived = _t, PriceReceived = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{
            {"Supplier", type text}, {"StockCode", type text}, {"PoDate", type date}, {"PurchaseOrder", Int64.Type}, 
            {"PurchaseOrderLin", Int64.Type}, {"LineDueDate", type date}, {"DateReceived", type date}, 
            {"QtyReceived", type number}, {"PriceReceived", type number}},"en-150"),
        
        #"Grouped Rows" = Table.Group(#"Changed Type", {"StockCode"}, {
            {"all", (t)=>Table.MaxN(t, each [PoDate],2), 
            type table [Supplier=nullable text, StockCode=nullable text, PoDate=nullable date, PurchaseOrder=nullable number, PurchaseOrderLin=nullable number, LineDueDate=nullable date, DateReceived=nullable date, QtyReceived=nullable number, PriceReceived=nullable number]}}),
        #"Removed Columns" = Table.RemoveColumns(#"Grouped Rows",{"StockCode"}),
        #"Expanded all" = Table.ExpandTableColumn(#"Removed Columns", "all", 
            {"Supplier", "StockCode", "PoDate", "PurchaseOrder", "PurchaseOrderLin", "LineDueDate", "DateReceived", "QtyReceived", "PriceReceived"})
    in
        #"Expanded all"

     

     

    In the #"Expanded All" step you can deselect the columns you might not want to show.

     

     

    • pmsilvbi's avatar
      pmsilvbi
      Frequent Visitor

      Thanks so much for your suggestion. I'll do some further research on how to apply this.