In PowerQuery how do I sort data within a Group By command?

Jeremy Troy 40 Reputation points
2025-11-19T13:13:24.78+00:00

I have a column which has rows of data appearing as follows:

11/12/2025 adfadkkkkdfa asfda

11/11/2025 adskjkjkjkerje

10/15/2025 iwejriewr

Each of those rows has a unique ID, some of those unique IDs have multiple data and text entries, therefore I am using a Table.Group command to have just one row per unique ID. However, I need to sort those dates by most recent (descending). How do I do that?

Thank you.

Microsoft 365 and Office | Excel | For business | Windows
0 comments No comments
{count} votes

Answer accepted by question author
  1. Ashish Mathur 101K Reputation points Volunteer Moderator
    2025-11-19T23:44:31.8033333+00:00

    Hi,

    This M code works

    let
        Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
        #"Split Column by Delimiter" = Table.SplitColumn(Source, "Comment with Date", Splitter.SplitTextByDelimiter(": ", QuoteStyle.Csv), {"Comment with Date.1", "Comment with Date.2"}),
        #"Grouped Rows" = Table.Group(#"Split Column by Delimiter", {"Work Order Number", "Property"}, {{"Count", each _[[Comment with Date.1],[Comment with Date.2]]}}),
        #"Added Custom" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.TransformColumnTypes([Count], {{"Comment with Date.1", type date}}, "en-US")),
        #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.1", each Table.Sort([Custom],{"Comment with Date.1",Order.Descending})),
        #"Added Custom3" = Table.AddColumn(#"Added Custom1", "Custom.2", each Table.TransformColumnTypes([Custom.1], {{"Comment with Date.1", type text}})),
        #"Added Custom2" = Table.AddColumn(#"Added Custom3", "Comment with date grouped", each Text.Combine(Table.ToList([Custom.2],Combiner.CombineTextByDelimiter(": ")),", "))[[Work Order Number],[Property],[Comment with date grouped]]
    in
        #"Added Custom2"
    

    User's image

    1 person found this answer helpful.

3 additional answers

Sort by: Most helpful
  1. Q&A Assist
    2025-11-19T13:13:45.4866667+00:00
    AI generated content. This question contains an answer created with AI from Q&A Assist. Learn more

    To sort data within a Group By command in Power Query, you can follow these steps:

    1. Group By: First, use the Table.Group function to group your data by the unique ID. This will create a new table where each unique ID corresponds to a single row.
    2. Sort Within Groups: After grouping, you can sort the dates in descending order. To achieve this, you can add a sorting step after the grouping operation. You can use the Table.Sort function to sort the grouped data based on the date column.

    Here’s a general example of how you might implement this:

    let
        Source = YourDataSource,
        GroupedData = Table.Group(Source, {"UniqueID"}, { {"AllData", each _, type table}}),
        SortedData = Table.TransformColumns(GroupedData, { {"AllData", each Table.Sort(_, { {"DateColumn", Order.Descending}}), type table}})
    in
        SortedData
    

    In this example:

    • Replace YourDataSource with your actual data source.
    • Replace UniqueID with the name of your unique ID column.
    • Replace DateColumn with the name of your date column.

    This will give you a table where each unique ID has its associated data sorted by date in descending order.


    References:

    0 comments No comments

  2. Henry-N 6,810 Reputation points Microsoft External Staff Moderator
    2025-11-19T14:39:37.4933333+00:00

    Hi @Jeremy Troy,  

    Thank you for posting your question in the Microsoft Q&A forum.      

    Regarding you want to  know how to sort data within a group by command. I’d like to ask a few quick questions to better understand your situation and assist you more effectively:  

    Does your data have both date and text in the same column, or are they in separate columns? 

     Is your data similar to this : Column A: Date | Column B: Text
    User's image

    Do you like to sort the data manually in Power Query, or use M code for an automated approach?  

    If I’ve misunderstood anything, please feel free to correct me. I just want to make sure I fully understand so I can support you.   

    Thank you again for your time and understanding. While my initial response may not resolve the issue immediately, I’d like to gather more details about your situation so I can assist you more effectively.    

    I really appreciate your patience, and I’m here to help. Looking forward to your response!   


    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread   


  3. Ashish Mathur 101K Reputation points Volunteer Moderator
    2025-11-20T03:00:32.51+00:00

    Hi,

    This Excel formula works as well

    =LET(s,SORTBY(A2:C7,A2:A7,1,B2:B7,1,CHOOSECOLS(DROP(REDUCE("",C2:C7,LAMBDA(s,c,VSTACK(s,TEXTSPLIT(c,": ")))),1),1),-1),GROUPBY(TAKE(s,,2),CHOOSECOLS(s,3),ARRAYTOTEXT,,0))

    Hope this helps.

    User's image

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.