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 / Combining If, SortByCo...
Power Apps
Answered

Combining If, SortByColumns, and StartsWith to search a gallery populated with a collection

(0) ShareShare
ReportReport
Posted on by 12

Hi everyone,

 

I am trying to include a search bar to search my gallery, which is currently populated by a collection.

 

Right now, I am populating my gallery successfully as follows: If Field1DropDown is not blank, filter Collection on Field1=Field1DropDown.Selected.Result. If Field1DropDown is blank, make my gallery blank.

I am sorting my gallery by "ID" field and using a context variable SortDescending to determine whether my gallery is sorted descending or ascending.

 

Here is my current gallery Items property which works great:

 

 

SortByColumns(
 If(
 !IsBlank(Field1DropDown.Selected.Result),
 Filter(
 Collection, 
 Field1=Field1DropDown.Selected.Result
 ),
 Blank()
 ),
 "ID",
 If(
 SortDescending,
 Descending,
 Ascending
 )
)
 

 

 

 

Everything falls when I try to add in my search bar functionality using StartsWith(). I know the basic structure should be something like:

 

 

SortByColumns(
If(
 IsBlank(SearchBar.Text),
 .....everything I had before with my Filter statement etc....),
 StartsWith(Field1, SearchBar.Text))),
 ...everything I had before with "ID" and SortDescending etc....
)
)
 

 

 

 
But for the life of me I cannot figure out the syntax. Probably something with parentheses, I'm not quite sure. If anyone has any advice here, I'd appreciate it so much! Thanks in advance.

Categories:
I have the same question (0)
  • Verified answer
    RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @delluser 

    Try to avoid using IF statements in your filter formula!  This leads to a lot of redundancy in your formulas as well as confusion.

     

    Please consider changing your Formula to the following:

    SortByColumns(
     Filter(Collection, 
     IsBlank(SearchBar.Text) || StartsWith(Field1, SearchBar.Text)
     ),
     "ID", 
     If(SortDescending,Descending,Ascending
     )
    )

     

    Your other formula (if you're still trying to use that) should be:

    SortByColumns(
     Filter(Collection, 
     !IsBlank(Field1DropDown.Selected.Result) && Field1=Field1DropDown.Selected.Result
     ),
     "ID", 
     If(SortDescending,Descending,Ascending
     )
    )

     

     

    NOTE: on the first formula, the logic is that if the Search text is blank, then you will get all results.  If your intention is to have NO results unless something is entered, then change that first formula to:

    SortByColumns(
     Filter(Collection, 
     !IsBlank(SearchBar.Text) && StartsWith(Field1, SearchBar.Text)
     ),
     "ID", 
     If(SortDescending,Descending,Ascending
     )
    )

     

    I hope this is helpful for you.

  • delluser Profile Picture
    12 on at

    Wow, that was awesome!! Thanks for the fast and accurate response. I ended up combining your first and second blocks of code like this to get both of those pieces of logic embedded into my gallery:

    SortByColumns(
     Filter(Collection, 
     !IsBlank(Field1DropDown.Selected.Result) && Field1=Field1DropDown.Selected.Result && 
     (IsBlank(SearchBar.Text) || StartsWith(Field1, SearchBar.Text))
     ),
     "ID", 
     If(SortDescending,Descending,Ascending
     )
    )

    Works great. Thanks again so much!

  • RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @delluser 

    Yes...I wasn't sure if you were trying to combine or replace.  I assumed replace.  Glad you figure out combine!!

     

    Happy to help!

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the April Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
Vish WR Profile Picture

Vish WR 936

#2
Valantis Profile Picture

Valantis 604

#3
11manish Profile Picture

11manish 518

Last 30 days Overall leaderboard