web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Sort, CountRows and Fi...
Power Apps
Unanswered

Sort, CountRows and Filter to order a Gallery

(0) ShareShare
ReportReport
Posted on by 18

Hello experts,

I want to sort my gallery based on the number of rows the item has in another related list.

At the moment I have the code as shown below:

Sort(
 'DS SPO CoEFullStack Shared Ideas';
 CountRows(
 Filter(
 'DS SPO CoEFullStack Shared Ideas Comments';
 'Main Idea'.Value = "Title"
 )
 );
 Descending
 );

It does not give me any error but it is not ordering the gallery by counting the rows.

Can you help me with this?

Thank you very much in advance

 

Categories:
  • WarrenBelz Profile Picture
    156,969 Most Valuable Professional on at

    Hi @marinadengra9 ,

    Try this

    Sort(
     AddColumns(
     'DS SPO CoEFullStack Shared Ideas';
     "SortRows";
     CountRows(
     Filter(
     'DS SPO CoEFullStack Shared Ideas Comments';
     'Main Idea'.Value = Title
     )
     )
     );
     SortRows;
     Descending
    );

     

    Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

    MVP (Business Applications)   Visit my blog Practical Power Apps

  • marinadengra9 Profile Picture
    18 on at

    Thanks for answering @WarrenBelz  !

     

    With the code you sent I get following error:

     

    marinadengra9_0-1676632295110.png

    It is fixed if I adding comas to "Title", but it still not work, it does not order correctly.

     

    However, in the gallery I paint in a label the following:

    CountRows(
     Filter(
     'DS SPO CoEFullStack Shared Ideas Comments';
     'Main Idea'.Value = ThisItem.Title
     )
     )

    Perhaps the gallery could be sorted according to the number painted on the label?

    marinadengra9_1-1676633145319.png

     

    Many thanks again!!

  • WarrenBelz Profile Picture
    156,969 Most Valuable Professional on at

    @marinadengra9 ,

    I assumed Title was the field name, but are you saying it is simply the word "Title" in which case yes - you need quotes around it. But back to your issue - I simply used the CountRows code you posted as I assumed this was what ytou wanted to count. How do you calculate the highlighted item ?

  • marinadengra9 Profile Picture
    18 on at

    Thank you again for the reply @WarrenBelz 

    Are you right, Title is the field name, but it does not work without quotes. I also have tried with Thisrecord.Title as follows but it does not work either.

    Sort(
     'DS SPO CoEFullStack Shared Ideas';
     CountRows(
     Filter(
     'DS SPO CoEFullStack Shared Ideas Comments';
     'Main Idea'.Value = ThisRecord.Title
     )
     );
     Descending
    )

    Answering what is being calculated in the highlighted item I have the following:

    CountRows(
     Filter(
     'DS SPO CoEFullStack Shared Ideas Comments';
     'Main Idea'.Value = ThisItem.Title
     )
    )

     

     

    I've tried a slightly different solution by doing the following, and I get to pick up the number of comments from the other related list. But I don't know how to join that new column to my original list that I want to show in the gallery.

    AddColumns(
     GroupBy(
     AddColumns(
     'DS SPO CoEFullStack Shared Ideas Comments';
     "MainIdea";
     'Main Idea'.Value
     );
     "MainIdea";
     "CountItems"
     );
     "NComments";
     CountRows(CountItems)
    )

    I get the following:

    marinadengra9_0-1676851557983.png

    Now I want to relate the MainIdea column to the Title of the list I actually want to display in the gallery, which is called 'DS SPO CoEFullStack Shared Ideas', and sort the gallery based on the number of comments in the "NComments" column. ¿It is possible?

     

    Many many many thanks.

     

     

  • WarrenBelz Profile Picture
    156,969 Most Valuable Professional on at

    @marinadengra9 ,

    The only occasions you need quotes around a field name is in functions such as SortByColumns, GroupBy and Show/Remove/RenameColumns. If you put it in a Filter or LookUp, it is looking for the word "Title", not the Field Title. Also what you have done (now I am a bit clearer on what your data structure actually is) will certainly count the grouped data in each 'Main Idea' (which was not evident in your initial post). So how exactly are the records in your code related to the Title column and in what list is this situated ?

  • marinadengra9 Profile Picture
    18 on at

    @WarrenBelz thank you very much for the clarification, it will be useful to me from now on.

    My data structure is as follows, I have two lists;
    - 'DS SPO CoEFullStack Shared Ideas'
    - 'DS SPO CoEFullStack Shared Ideas Comments'
    They are related by a lookup column in 'DS SPO CoEFullStack Shared Ideas Comments' called 'Main Idea', which contains the title associated with 'DS SPO CoEFullStack Shared Ideas'.


    What I want to do in powerapps is sort the gallery with the items of the first list 'DS SPO CoEFullStack Shared Ideas', based on the number of items that are related in the second list 'DS SPO CoEFullStack Shared Ideas Comments'.

  • WarrenBelz Profile Picture
    156,969 Most Valuable Professional on at

    @marinadengra9 ,

    A lookup column does not "join" the lists - and you are better off without them (but that is another discussion). Which field in 'DS SPO CoEFullStack Shared Ideas Comments' has the same value as 'Main Idea".Value in 'DS SPO CoEFullStack Shared Ideas'  ?

  • marinadengra9 Profile Picture
    18 on at

    @WarrenBelz ,

    'Main Idea' from 'DS SPO CoEFullStack Shared Ideas Comments' list matches with Title from 'DS SPO CoEFullStack Shared Ideas' list.

  • WarrenBelz Profile Picture
    156,969 Most Valuable Professional on at

    @marinadengra9 ,

    Best effort without testing below - I will be offline shortly due to time zone

    AddColumns(
     GroupBy(
     AddColumns(
     Filter(
     'DS SPO CoEFullStack Shared Ideas Comments';
     'Main Idea'.Value in 'DS SPO CoEFullStack Shared Ideas'.Title
     );
     "MainIdea";
     'Main Idea'.Value
     );
     "MainIdea";
     "CountItems"
     );
     "NComments";
     CountRows(CountItems)
    )

     

    Please click Accept as solution if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider giving it Thumbs Up.

    MVP (Business Applications)   Visit my blog Practical Power Apps

  • marinadengra9 Profile Picture
    18 on at

    @WarrenBelz,

    It is not working, The code is creating a table with the number of comments (NComments) and the Title (MainIdea), but I need to paint in the gallery the items from the 'DS SPO CoEFullStack Shared Ideas' list.

    marinadengra9_0-1676903945067.png

     


    Is there any way to associate this table to the 'DS SPO CoEFullStack Shared Ideas' items?
    That is; now in the table I have MainIdea that corresponds to the Title (in list i want to represent) and the number of comments. What I would like to have is add the NComments column to the list and then sort by this one.

     

    Many thanks.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 220 Super User 2026 Season 2

#2
WarrenBelz Profile Picture

WarrenBelz 189 Most Valuable Professional

#3
Mohsin Ali Profile Picture

Mohsin Ali 139

Last 30 days Overall leaderboard