Forum Discussion

phull543's avatar
phull543
Frequent Visitor
1 year ago

Sort grouped row by created on date in Order Ascending

I have created a column which shows all notes for each activity and the date each note was created. This has been grouped with another field from a different table. I am trying to sort this column so that the newest created note is always shown for each activity, can someone explain how to do that? It is currently showing the last created note first as can be seen in the screenshot included.

13 Replies

  • phull543 

     

    Split the column into 2 columns : Date and Note, and sort by the Date. 

     

    You can sort by other column(s) first to maintain some other order you might need before sorting by this new Date column.

     

    Phil

  • phull543's avatar
    phull543
    Frequent Visitor

    I have tried to do that but it take the first date which is the oldest date and i am trying to sort by newest. 

      • phull543's avatar
        phull543
        Frequent Visitor

        I have tried to do that but it just sorts the latest date created. I need it to change the order of all the dates in the note from newest to oldest. 

    • phull543's avatar
      phull543
      Frequent Visitor

      Yes it does but once I have grouped those rows I merge with another query and this is when the issue arises. 

  • AntrikshSharma's avatar
    AntrikshSharma
    Icon for Community Champion rankCommunity Champion

    phull543 Use Lines.FromText

     

     

    And then Sort by extracting the date part

     

    Ascending

     

    Descending

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIyMDLRNTDVNTRT8EgsKKhUcMosKslISayMyQNLGRrpGhsqRKYmFim45qVABQ0MgUjBL7UcLAETNNc1tFDwTSwqykxMT1VwzMvLLEstKk4sqlSKjQUA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [ID = _t, Events = _t]),
        AddedCustom = 
            Table.AddColumn (
                Source, 
                "Custom", 
                each List.Sort (
                    Lines.FromText ( [Events] ), 
                    {
                        each Date.FromText ( Text.Start ( _, 10 ), [ Format = "yyyy-MM-dd" ] ), 
                        Order.Descending
                    }
                )
            )
    in
        AddedCustom

     

     

     

     

  • The Table.Group() does not preserve sort order or defines a sort order. So if you want your groups to be sorted, you do it AFTER the sort, but on each group individually. 

     

    Do the following just after your group by (check the step and field names first!):

    = Table.TransformColumns(#"Grouped Rows",{{"Notes", each Table.Sort(_, {{"Latest Date", Order.Descending}})}})

     

    • phull543's avatar
      phull543
      Frequent Visitor

      I have added that command and I am now getting the following error.

       

      • PwerQueryKees's avatar
        PwerQueryKees
        Icon for Super User rankSuper User

        Can you share all the M Code. I want to see how you create the "Combined Notes" column. It is probaly after that step where you need to do the sort.