Forum Discussion

klehar's avatar
klehar
Helper V
1 year ago
Solved

Top 2 for each date

Hi     This is my data Here for each date, I want the top 2 sales numbers 16 and 14 for 1/1 17 and 15 for 2/1   How can i do this in M code/query editor?
  • tackytechtom's avatar
    1 year ago

    Hi klehar ,

     

    How about this?

     

    Here the code in Power Query M that you can paste into the advanced editor (if you do not know, how to exactly do this, please check out this quick walkthrough

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjDUBSIjAyNTJR0lQzOlWB10MRMsYkZYxAwgYkbIYuZYxEyxiBljETNUio0FAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [date = _t, sales = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"date", type date}, {"sales", Int64.Type}}),
        #"Grouped Rows" = Table.Group(#"Changed Type", {"date"}, {{"TopSales", each 
            Table.FirstN( 
                Table.Sort(_, {{"sales", Order.Descending}}), 2
            ), type table [sales=Int64.Type]}}),
        #"Expanded TopSales" = Table.ExpandTableColumn(#"Grouped Rows", "TopSales", {"sales"}, {"TopSales.sales"})
    in
        #"Expanded TopSales"


    /Tom
    https://www.tackytech.blog/
    https://www.instagram.com/tackytechtom/