cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
PaulD1
Super User
Super User

Combining GroupBy And Sort

I'm using a GroupBy in my datasource for a Gallery-SubGallery set up. I'm taking data from an OrderDetails table which includes an OrderHeaderID which is the field I am grouping on.

 

GroupBy(Filter('[Order].[OrderDetail]',OrderDetailTimeInt >= varTodayInt)  ,"OrderHeaderID","GrpOrderByHeader")

By befault, the records appear to be shown in the Gallery in Ascending order (of the OrderHeaderID). I would prefer they be shown in descending order. I have tried adding a Sort to the formula as follows:

SortByColumns(GroupBy(Filter('[Order].[OrderDetail]',OrderDetailTimeInt >= varTodayInt)  ,"OrderHeaderID","GrpOrderByHeader"),"OrderHeaderID",Descending)

Unfortunately, this not only appears to have a considerable hit on performance but doesn't actually perform a sort, instead mixing up the records so I actually get multiple copies of the same OrderHeaderID with only some of the related records appearing under each.

 

Is there a way to sort the result set of a GroupBy? What I really want to do is lookup the OrderHeaderDate for each OrderHeaderID and sort by that, but as OrderHeader records have an auto-incrementing ID, sorting by the ID would be sufficient for now...

 

Thanks

Paul

1 ACCEPTED SOLUTION

Accepted Solutions
hpkeong
Community Champion
Community Champion

Hi @PaulD1

 

Normally, GroupBy will be used in conjunction with ClearCollect so that the results will be collected into Temporary table and this table is referred to Gallery,Items = TableCollected.

 

In this case, you shall be getting smooth results.

 

Please see this good tutorial and definitely helps.

https://powerapps.microsoft.com/en-us/tutorials/function-groupby/

 

TQ

hpkeong

View solution in original post

11 REPLIES 11
Leo09
Helper V
Helper V

Maybe you need to seperate the steps. You need to first groupby the gallery and then use another place to sort the gallery.

PaulD1
Super User
Super User

Well, something seems to have changed because now my results do sort (yay!). This is using the SortBy syntax used in the original post.

 

But... as I scroll through the returned results in a gallery they consistently cause the app to crash (boo!)

hpkeong
Community Champion
Community Champion

Hi @PaulD1

 

Normally, GroupBy will be used in conjunction with ClearCollect so that the results will be collected into Temporary table and this table is referred to Gallery,Items = TableCollected.

 

In this case, you shall be getting smooth results.

 

Please see this good tutorial and definitely helps.

https://powerapps.microsoft.com/en-us/tutorials/function-groupby/

 

TQ

hpkeong

View solution in original post

Thanks @hpkeong

 

For various reasons, I have ended up using a Collection in the end. Still getting the occasional crash in the designer when I am dealing with 'large' amounts of test data, but seems to be OK in the player with more realistic work-loads, so fingers crossed, all good for now 🙂 

satya
Helper II
Helper II

Hi Paul,

 

i have a sharepoint list which is having morethan 3000 records, in that list a column having yes/no field. i have created a gallery which shows based on the auditor(people picer) i am using the below query in main gallery but i am not able to filter. i need sorting based on auditor field.

ClearCollect(data,GroupBy(SortByColumns(Filter(Begehungen,,Abgeschlossen=0),AddColumns(Begehungen,"DisplayName",Auditor.DisplayName),If(SortDescending1,Descending,Ascending)),"DisplayName","BegehungenGroup"))

AyoubH
Frequent Visitor

So, I am trying to group the row by value to give something like that, but I can not get it done. If someone can assist/help it would be very appreciated please and thanks

 

Capture.PNG

Sorry, very late reply, but in case it still helps...

In a flexible height vertical gallery, set the Items as a GroupBy of the 'Unite de travail' and assign the GroupColumnName as something like 'UniteDetail'

Add a label to the gallery and set the text property it to ThisItem.'Unite de travail' and set AutoHeight to true.

Add a normal vertical gallery inside the existing gallery to the right of 'Unite de travail' label. Set it's Items property to ThisItem.'UniteDetail'. You can now add the labels for 'Statut du poste', etc.

You'll need to ensure that each instance of your inner gallery is tall enough to show the data. Figure out the height of the template for the inner gallery and set the inner gallery height to CountRows(ThisItem.'UniteDetail' * template_height) - you may also need to account for any template padding. Now the inner gallery should grow vertically to fit your data and this will make the template of the outer gallery grow to accommodate it.

Hi @PaulD1 ,

 

Saw your Youtube video on Groupby collapse and uncollapsed Gallery.

 

Could you share your solution on how you manage to sort your GroupBy?

 

 

@keyboardWrench 

If you need to sort the Parent - in your main gallery you can set the items property to:

Sort(GroupBy(MyTable,MyGroupField1,GroupedFields), MyGroupField1)

 

If you want to sort the Child elements, in your Subgallery set the Items property to Sort(ThisItem.GroupedFields,FieldToSortBy).

 

Hope that makes sense.

Helpful resources

Announcements
PA User Group

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

PA Community Call

Power Apps Community Call

Next call is happening on April 21st at 8a PST.

MBAS Carousel

Sign up for our May 4th event!

May the fourth be with you, join us online!

secondImage

Experience what’s next for Power Apps

See the latest Power Apps innovations, updates, and demos from the Microsoft Business Applications Launch Event.

Top Solution Authors
Top Kudoed Authors
Users online (70,486)