cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
saturation
Resolver I
Resolver I

SortColumn not working on Lookup

I'm trying to set SortColumn on a lookup column on my gallery item. State is the name of the lookup entity.

 

Set(SortColumn, "State");UpdateContext({SortColumn: "State", SortDescending: !SortDescending})
 
I've also tried State.Value, State.Name etc (in double quotes)
 
My sorting works on text fields using the same syntax. Can someone help here? 
1 ACCEPTED SOLUTION

Accepted Solutions

Hi @saturation ,

Have you taken a try with the solution I provided above?

 

Currently, within PowerApps, you could not specify Complex type column (e.g. LookUp, Choice, ... etc) as Sort column within the SortByColumns function.

 

As an alternative solution, I think the Sort function could achieve your needs. In addition, you could not specify variable as Sort column within the Sort function.

 

Please consider take a try with above Sort function I provided, then check if the issue is solved:

Sort(
     Filter(
             'MyData',
             ....
     ),
     If(                                // choose different Sort formula based on the SortField variable value
        SortField = "State",
        State.'Primary Name',            // 'Primary Column' represents the child property under the State LookUp field
        SortField = "Column1",
        Column1,
        ....
     )
     ,
     If(
         SortDescending,
         SortOrder.Ascending,
         SortOrder.Descending
      )
)

 

Best regards,

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

10 REPLIES 10
RezaDorrani
Dual Super User II
Dual Super User II

@saturation 

 

Firstly, use the Sort function rather than using SortByColumns

 

Secondly, Lookup columns do not support delegation on sorting

 

Assuming SP is your data source

https://docs.microsoft.com/en-us/connectors/sharepointonline/

 

--------------------------------------------------------------------------------
If this post helps answer your question, please click on “Accept as Solution” to help other members find it more quickly. If you thought this post was helpful, please give it a Thumbs Up.

Thanks,
Reza Dorrani, MVP
YouTube
Twitter

Thanks for the quick reply.

 

1) I'm using CDS/D365.

2) Why use Sort over SortByColumns?

@saturation 

 

sortbycolumns expects a simple type column as the sort field data type for this function is text

 

sort expects the column reference as the data type and works for most column types

 

--------------------------------------------------------------------------------
If this post helps answer your question, please click on “Accept as Solution” to help other members find it more quickly. If you thought this post was helpful, please give it a Thumbs Up.

Thanks,
Reza Dorrani, MVP
YouTube
Twitter

So I switched to the Sort function, and it won't sort at all. Is there something going on here? If I switch it back to SortByColumns it works (I tested both functions on a text field).
 
Sort(
Filter(
'MyData',
),
SortColumn,
If(
SortDescending,
SortOrder.Ascending,
SortOrder.Descending
)
)

@saturation 

 

You are setting the SortColumn variable as a string

 

Sort function expects the column name.

So do not use a variable and directly use the column

 

--------------------------------------------------------------------------------
If this post helps answer your question, please click on “Accept as Solution” to help other members find it more quickly. If you thought this post was helpful, please give it a Thumbs Up.

Thanks,
Reza Dorrani, MVP
YouTube
Twitter

I'm passing it because I have several different columns I need to sort on depending on the column that is clicked...Is that not possible with Sort?

@saturation 

 

Possible but more work

 

Here is an example:

 

Sort(
Filter(
'MyData',
),
Switch(SortColumn,"A",ColumnA,"B",ColumnB),
If(
SortDescending,
SortOrder.Ascending,
SortOrder.Descending
)
)
 
--------------------------------------------------------------------------------
If this post helps answer your question, please click on “Accept as Solution” to help other members find it more quickly. If you thought this post was helpful, please give it a Thumbs Up.

Thanks,
Reza Dorrani, MVP
YouTube
Twitter
v-xida-msft
Community Support
Community Support

Hi @saturation ,

Could you please share a bit more about your scenario?

Do you want to sort your Gallery Items based on a variable?

 

Firstly, I agree with @RezaDorrani 's thought almost. If you use column name as sort column in your Sort functions (Sort or SortByColumns), you need to type specific column name within the Sort function rather than use a variable.

 

On your side, please consider modify your formula as below:

Sort(
     Filter(
             'MyData',
             ....
     ),
     If(
        SortColumn = "State",
        State.'Primary Name',            // 'Primary Column' represents the child property under the State LookUp field
        SortColumn = "Column1",
        Column1,
        ....
     )
     ,
     If(
         SortDescending,
         SortOrder.Ascending,
         SortOrder.Descending
      )
)

 

Please consider take a try with above solution, check if the is issue is solved.

 

Best regards,

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

I have a gallery setup to appear like a datatable. I have separate headers for each column. When the user clicks on the header, I use OnSelect to sort ASC and DESC. Some of the data types being displayed are text, some are lookups, some are dates, some are optionsets. All D365 data types. How would Sort be advantageous? When the user clicks one of the headers, I pass 

 

Set(SortField, "fieldname"); UpdateContext({SortField: "fieldname", SortDescending: !SortDescending})
 
and then in the Items of the gallery, I set it the 2nd and 3rd parameters of SortByColumns to this
 
SortField, If(SortDescending,Ascending,Descending))

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

M365 768x460.jpg

Microsoft 365 Collaboration Conference | December 7–9, 2021

Join us, in-person, December 7–9 in Las Vegas, for the largest gathering of the Microsoft community in the world.

Top Solution Authors
Top Kudoed Authors
Users online (2,638)