Forum Discussion

Syndicate_Admin's avatar
Syndicate_Admin
Icon for Administrator rankAdministrator
4 years ago
Solved

Filter LastDate PowerQUery

Best regards... I hope you can help me with this request. I have 2 tables, both with a USER IDENTIFIER. Table 1 : 1 user can have many orders, each order has its date. I need the last date of eac...
  • Anonymous's avatar
    Anonymous
    4 years ago

    Hi avillarroelk250 , it looks like you want to find and keep the details for the last Order and Sale for each customer.  In Power Query, you could achieve this using the following steps:

    1. Sort the individual tables by Customer and Date.
    2. Add Index column column - this will find the max id if there are more sales or orders on last date.
    3. Add a Boolean (true/false) column indicating if the Row is the max id for the custom.  This is the complex part.
    4. Filter the tables for True.
    5. Merge the tables based on Customer ID.

    Try the following example in Power Query:

     

    let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WclTSUTIw1AciIwMjIyAnMSc7JU0pVidayQlTKqU4LSsHLOmMKVmckpZlDZaEGWoElyxOyYHpBBtrjCyZlpKTnYUw1QRVLgsmiWlqYkpaDopTjRH6crKBvogFAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Customer = _t, #"Sale Date" = _t, #"Sale Detail" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Sale Date", type date}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Customer", Order.Ascending}, {"Sale Date", Order.Ascending}}),
    #"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "ID", 0, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "IsMax",
    each let
    x = [Customer]
    , filter = Table.SelectRows( #"Added Index" , each [Customer] = x )
    , result = List.Max( filter[ID] )
    in
    result = [ID]
    , type logical
    ),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom", each ([IsMax] = true)),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"ID", "IsMax"})
    in
    #"Removed Columns"