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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Error occurred when us...
Power Apps
Unanswered

Error occurred when using ascending order

(0) ShareShare
ReportReport
Posted on by 15
I need help with Power Apps. It is a news application that receives a list of news, displays the list, and when someone selects a news item, it shows the details of the selected news. There are three screens, on the screen called "Noticias" there is a gallery: "Lista Noticias". The code for the gallery is:


++++++++++++++++++++++++++
SortByColumns(
    Filter(        [@GeparNews];        (IsBlank(selectedTag) || CountIf(TAGS; Value = selectedTag) > 0) &&
        (IsBlank(TextSearchBox1.Text) &&
            StartsWith(Título; TextSearchBox1.Text) || 
            StartsWith('Modificado por'.DisplayName; TextSearchBox1.Text) || 
            StartsWith(Empresa.Value; TextSearchBox1.Text) || 
            StartsWith(Ticker.Value; TextSearchBox1.Text)
        )
    );
    "Modified";
    If(SortAscending1; SortOrder.Ascending; SortOrder.Descending)
)
++++++++++++++++++++++++++

and I also have an icon to sort the list, which in the OnSelect property receives the formula:


UpdateContext({SortAscending1: Not(SortAscending1)})

 
++++++++++++++++++++++++

 The problem is when I touch the sort icon, the view moves with the selected item to the top or the end, but I want the view to stay at the top.
  
ps.: In Brazil we use ";" not ",' 
 
Categories:
I have the same question (0)
  • Suggested answer
    MParikh Profile Picture
    313 Super User 2025 Season 2 on at
    The key is to temporarily clear the selected item just before sorting, then optionally reset it or let the user select again. Here's how you can do it:

    Modify your Sort Icon’s OnSelect:
     
    UpdateContext({selectedItem: ListaNoticias.Selected});
    UpdateContext({SortAscending1: Not(SortAscending1)});
    Reset(ListaNoticias);

    This saves the selected item in context (optional), toggles sort, and then resets the gallery.

    If you don’t need to preserve the selected item after sort (to avoid scroll completely),
    you can also do:

     
    UpdateContext({SortAscending1: Not(SortAscending1)});
    Set(ListaNoticias.Selected, Blank());
    Reset(ListaNoticias);

    Alternative: Force Scroll to Top
    You can place an invisible label or control at the top of the gallery and then Select it after sorting to reset scroll position.

     
    UpdateContext({SortAscending1: Not(SortAscending1)});
    Select(lblScrollTop)

    Where lblScrollTop is a small label placed at the very top of the first item of the gallery.
     
  • CU23051955-2 Profile Picture
    15 on at
     
    Erro: Caracteres inesperados. Caracteres são usados na fórmula de forma inesperada.
    I forgot to mention in the post that I already tried asking Copilot and other GPTs for help. I think that by searching there and bringing the answer from there, we won't be able to succeed.
  • BCBuizer Profile Picture
    22,505 Super User 2025 Season 2 on at
    Hi!
     
    To chain multiple formulas, you need to use ";;" instead of ";" due to your language settings.
     
    Please try:
    UpdateContext({SortAscending1: Not(SortAscending1)});;
    Set(ListaNoticias.Selected, Blank());;
    Reset(ListaNoticias);;
     
     
    If this reply helped you in any way, please give it a Like 💜 and in case it resolved your issue, please mark it as the Verified Answer ✅.
  • CU23051955-2 Profile Picture
    15 on at
    Tentei assim, mas não foi também, funciona a fórmula, no sentido de não apontar erros, mas não mantém o primeiro item da nova lista selecionado, mantém o item da antiga lista selecionado:
     
    UpdateContext({
        SortAscending1: Not(SortAscending1);
        selectedItem: First(ListaNoticias.AllItems)
    })
     
     
    Pensei em mesmo quando a lista mudar a ordem selecionar o primeiro item da lista.
    Como o power apps não lida bem com isso =(
  • Verified answer
    BCBuizer Profile Picture
    22,505 Super User 2025 Season 2 on at
    Hi!,
     
    As I understand you would always like to have the first item in the gallery selected after changing the sortorder.
     
    What you can use for this is:
    UpdateContext({SortAscending1: !SortAscending1});;
    UpdateContext(
        {
            selectedItem: LookUp(
                [@GeparNews];
                ID = First(ListaNoticias.AllItems).ID
            )
        }
    );;
    
    Then make sure to set the Default propery of ListaNoticias (your gallery) to selectedItem .
     
    The reason this works as opposed to just using First(ListaNoticias.AllItems), is that the LookUp will return a record that matched the schema of the datasource used in the Items property of the gallery. If you examine the schema of the ListaNoticias.AllItems property, you will find it contains many more fields than the datasource, hence it does not match and nothing gets selected when updating the selectedItem variable.
     
    Secondly, two UpdateContect functions should be used for better code readability. If the two variables get updated within the same function, that means the First() fucntion will select the first item before changing the sortorder. As a result always the last item in the gallery will be selected. Of course this can be mitigated by using the Last function instead, but that will look counterintuitive when reading the formula, hence my recommendation to use two functions.
     
     
    If this reply helped you in any way, please give it a Like 💜 and in case it resolved your issue, please mark it as the Verified Answer ✅.
     

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 796 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 327 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard