Forum Discussion

espensun's avatar
espensun
Copper Contributor
Oct 19, 2024
Solved

Sorting a table with groups of people (no VBA)

Hi,
I have a table with groups of people in a team. The individual team can vary from 3 to 4 persons. The table list the group and their group score/result (se attached image).
How can I sort the table so that the team with the highest score/result is listed in ascending order and  keep the team members together? 
Any help or hint is very much appreciated.

  • espensun 

    If you already have GROUPBY:

     

    =LET(
      grouped,      GROUPBY( Table[Group], Table[Res.], SUM,, 0 ),
      filled_res,   XLOOKUP( Table[Group], CHOOSECOLS( grouped, 1 ), CHOOSECOLS( grouped, 2) ),
      sorted,       SORTBY( Table,
                      filled_res, 1,
                      SEQUENCE( ROWS( Table ) ), 1
                    ),
      IF( ISBLANK( sorted ), "", sorted )
    )

     

9 Replies

  • peiyezhu's avatar
    peiyezhu
    Bronze Contributor

    espensun 

    sql:

     select * from OriginalData;

     select f01,f02,f03 from (select rowid old_rowid,*,max(f03) over(partition by f02) ord from OriginalData) order by ord,old_rowid;

     

    f01f02f03
    bbb1 
    aaa1 
    dddd1 
    cccc149
    eee2 
    kkk2 
    hhhh248
    zzzz3 
    yyyy3 
    www3 
    xxxx355
    f01f02f03
    eee2 
    kkk2 
    hhhh248
    bbb1 
    aaa1 
    dddd1 
    cccc149
    zzzz3 
    yyyy3 
    www3 
    xxxx355
  • Lorenzo's avatar
    Lorenzo
    Silver Contributor

    espensun 

    If you already have GROUPBY:

     

    =LET(
      grouped,      GROUPBY( Table[Group], Table[Res.], SUM,, 0 ),
      filled_res,   XLOOKUP( Table[Group], CHOOSECOLS( grouped, 1 ), CHOOSECOLS( grouped, 2) ),
      sorted,       SORTBY( Table,
                      filled_res, 1,
                      SEQUENCE( ROWS( Table ) ), 1
                    ),
      IF( ISBLANK( sorted ), "", sorted )
    )

     

  • Lorenzo's avatar
    Lorenzo
    Silver Contributor

    Hi espensun 

     

    Did it with a Table (not mandatory) - sample attached

     

     

    =LET(
      unique_group, UNIQUE( Table[Group] ),
      group_res,    XLOOKUP( unique_group, Table[Group], Table[Res.],,, -1 ),
      filled_res,   XLOOKUP( Table[Group], unique_group, group_res ),
      sorted,       SORTBY( Table,
                      filled_res, 1,
                      SEQUENCE( ROWS( Table ) ), 1
                    ),
      IF( ISBLANK( sorted ), "", sorted )
    )

     

     

    EDIT: shortened the formula and applied Cond. Format. to separate the Groups

Resources