cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
AaronO
Helper II
Helper II

Things work, then they don't.

Hi,

 

I have a gallery I need to sort by 4 different options.

 

Initially, I had this working.  But now this formula for the gallery items complains that the variable( global or context) is unknown.  Even after I have declared it in the screen OnVisible as well as in the Dropdown OnChange.  Initially I was going to ask a different question here, but in the process of trying to specify details of my question, PowerApps decided to preclude my issue with this inexplicable error.  This is typical behavior for PowerApps for me.  Things work, then they don't.  Even after reverting to the original code which worked before.

 

OnChange of DropDown: 

 

UpdateContext({SortingColumn: Switch(DDSort.SelectedText.Value,"ID","ID")})

 

(I used to have all four columns in here, but pared it down to troubleshoot)

 

OnVisible of Screen:

 

 UpdateContext({SortingColumn: "ID"});

 

 

Code calling the variable:

 

SortByColumn(Filter(
    'Supplier Risk Issues',
    (
        TxtRisksOverviewSearch.Text in ID || 
        TxtRisksOverviewSearch.Text in Title || 
        TxtRisksOverviewSearch.Text in 'Supplier:Title'.Value || 
        TxtRisksOverviewSearch.Text in 'Crisis Team Lead'.DisplayName || 
        TxtRisksOverviewSearch.Text in 'Segment:Title'.Value || 
        TxtRisksOverviewSearch.Text in 'Segment:Acronym'.Value ||
        TxtRisksOverviewSearch.Text in 'Division:Title'.Value ||
        TxtRisksOverviewSearch.Text in 'Division:Abbreviation'.Value
    ) && (
        (
            varShowAgenda = true && 
            (
                'Segment Radio'.SelectedText.Value = 'Segment:Acronym'.Value && 
                (
                    'Meeting Radio'.SelectedText.Value = "Steering" && 'Included in Steering ID' = Coalesce(varCurrentMeetingID,0)
                ) || (
                    'Meeting Radio'.SelectedText.Value = "Crisis Board" && 'Included in Crisis Board ID' = Coalesce(varCurrentMeetingID,0)
                )
            ) || 
            Not(varShowAgenda)
        )
    ) && (
        TogShowClosed.Value= true || 
        (TogShowClosed.Value = false && 'Process Stage'.Value <> "Risk Closed")
    )
),SortingColumn,Descending)

 

 

 

Initially I was using a global variable for SortingColumns, but that doesn't work now either, and I think the context variable is the proper choice here anyway.

6 REPLIES 6
RandyHayes
Super User
Super User

@AaronO 

First off, when using SortByColumns, you need to specify the real name of the column.  So, if any of your columns are renamed, this will be the source of problems if you are not providing the real column name.

 

Your formula is mixing and matching what it is searching over - in some cases text in some cases numbers.  I would make sure you are using the appropriate operators for the data types and convert the search text to the appropriate data type as well.

 

Also, you are using the SelectedText property on your controls - the SelectedText property is a deprecated property and should not be used.  I would suggest removing it and replacing with the .Selected property and the appropriate column from that control's record.  i.e. Selected.Value, etc.

 

Also to consider in case your sort column variable is not set for some reason...use a Coalesce function in the Sort column name parameter to default it to a specific column if not specified.

 

I hope this is helpful for you.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!
v-bofeng-msft
Community Support
Community Support

Hi @AaronO :

Agree with @RandyHayes , I'm afraid that is not available to use a variable to specify the column to be sorted.Maybe you could try:

 

 

SortByColumns(
  YourDataSource,
  Switch(
    SortingSolumn,
    "ID",
    "ID"
   ),
  Descending)

 

 

Best Regards,

Bof

AaronO
Helper II
Helper II

I don't think I got an answer to the question, but maybe I didn't pose the question very well.

 

Ultimately, I switched to Sort instead of SortByColumn, and moved the sort values into the argument instead of building the sort values outside then passing it into the argument as a variable.  This mostly works, but sorting by Segment completely doesn't work as it "sorts" by a seemingly random order when that column is selected for sort.  No idea why, but I'll post a new question for that.

RandyHayes
Super User
Super User

@AaronO 

Actually, I believe there was a misunderstanding of my response - I never mentioned that you CANNOT use a variable to specify the sort column.  In fact - I do this all the time with the SortByColumns.

You cannot specify a variable for the sort column with the Sort function, but SortByColumns will let you do it.

 

What I was mentioning is that you MUST use the real name of the column in your variable.

Example, if you created a column called "something" in your list and then later changed that to "nothing", then your variable must contain "something" in it in order to sort the "nothing" column.  That is because the SortByColumns wants the "real" name of the column and the real name never changes once you create the column.

 

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

Yes, but with Sort it was giving me troubles getting the right types of values through.  Maybe I was just doing that wrong, but that's an issue for another day.  I had already given up on SortByColumns by this time due to the facts you pointed out.  My main issue was that the entire formula was throwing an error when it was not doing so before.  I think Power Apps just gets into these inescapable error conditions which requires you to close the app to clear it.  I'm just learning that with variable declarations as well.  If you have one Set function which results in an error it causes a failure across the entire app for that variable until you close the app.

RandyHayes
Super User
Super User

@AaronO 

I'm confused though, your original formula showed SortByColumns - not Sort!?

 

I would avoid the variables though.  You really don't need them.

Here's how - set the Items property of your sort dropdown to something like this:

Table(
    {Value: "Title Column", sortCol: "Title"},
    {Value: "Record ID", sortCol: "ID"},
    {Value: "Client Name", sortCol: "columnName"}
)

Made up names in the above, and let's assume the dropdown is called drpSortColumns.

 

Then the Items formula would be (very simplified version for demonstration):

SortByColumns(
    Filter(myList, SomeColumn="someValue"),
    drpSortColumns.Selected.sortCol,
    Descending
)

 

This works perfectly - again, as long as you are using the real name of the column.

No variables needed!

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

Helpful resources

Announcements
User Group Leader Meeting January 768x460.png

Calling all User Group Leaders!

Don't miss the User Group Leader meetings on January, 24th & 25th, 2022.

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

Top Solution Authors
Top Kudoed Authors
Users online (1,746)