Forum Discussion

ceov95's avatar
ceov95
Regular Visitor
6 years ago
Solved

Retrieve a specific value with conditions

So I have this kind of data:   Item Code Qty Stock DB Server Date Month Item 1 10 20/3/2020 March Item 2 20 15/3/2020 March Item 3 30 18/3/2020 March Item 1 8 23/3/2020...
  • edhans's avatar
    6 years ago

    Try this code. It returns this table. I just happened to write about this in a blog post this week.

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fZLLCsIwEEV/RbIuJJ1pTLt06cIvKF2ICAoKUtz49/ZeH6XizGYIyeE+hvR92N6P11UdqlCnaUiKGiUJzrv9eDiFoXozwmeA2WR0ulIyrcnAq4WWujLSYNgywiQYxdfBvcbmg2xu4/myTEOVZCJ04lj7DBeYXSuWsmWUqwPTuTKv8jHPvR/LLAVAZwD6LWQRMkf9D/DDeAA8OgDlBxie", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Item Code" = _t, #"Qty Stock" = _t, #"DB Server Date" = _t, Month = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Qty Stock", Int64.Type}}),
        #"Changed Type with Locale" = Table.TransformColumnTypes(#"Changed Type", {{"DB Server Date", type date}}, "en-BS"),
        #"Grouped Rows" = 
            Table.Group(
                #"Changed Type with Locale", 
                {"Item Code", "Month"}, 
                {
                    {"Latest Date", each List.Max([DB Server Date]), type nullable date}, 
                    {"Stock Amount", each Table.Max(_, "DB Server Date")[Qty Stock], Int64.Type}
                }
            )
    
    
    in
        #"Grouped Rows"

     

    1) In Power Query, select New Source, then Blank Query
    2) On the Home ribbon, select "Advanced Editor" button
    3) Remove everything you see, then paste the M code I've given you in that box.
    4) Press Done
    5) See this article if you need help using this M code in your model.