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 / Creating logic for mul...
Power Apps
Answered

Creating logic for multiple search filters on event items

(0) ShareShare
ReportReport
Posted on by 70
Hello!
 
I have a gallery where each item has several different text fields that I want to be filterable. I have search bars that are tied to those different fields, which then filters the results. I currently have a setup where I have several nested if statements depending on which search bars have content and which don't. However, as I add more search fields, I am realizing that the more fields I add, the nested if statements grow exponentially. I figure there has got to be a smarter way to filter items based on which search fields have text or not. Anyone have good alternatives other than a bunch of nested ifs? For reference, my current setup is like this
 
If(isEmpty(OwnerSearch) and isEmpty(NarrativeSearch),
   //dont sort,
   If(!isEmpty(OwnerSearch),
       If(!isEmpty(NarrativeSearch),
           //sort with both fields,
           //sort with just owner
       ),
       //sort with just narrative
)
Any suggestions on how to improve this searching algo without needed to add exponential lines of code for logic would be greatly appreciated. Thank you!
 
Categories:
I have the same question (0)
  • Suggested answer
    WarrenBelz Profile Picture
    155,838 Most Valuable Professional on at
    The code below takes into account the Delegation limitation for dynamic Sort criteria, so the output (the data can be of any size) needs be under your Data Row Limit. Also where I have ID, use whatever your data is sorted by default. Your current gallery Items also need to go where indicated.
    With(
       {
          _Sort1: If(
             !IsEmpty(OwnerSearch),
             "Owner",
             "ID"
          ),
          _Sort2: If(
             !isEmpty(NarrativeSearch),
             "Narrative",
             "ID"
          ),
          _Data: YourCurrentGalleryItemsHere
       },
       SortByColumns(
          _Data,
          _Sort1,
          SortOrder.Ascending,
          _Sort2,
          SortOrder.Ascending
       )
    )
     
    Or do you actually mean Search, not Sort - then you would be looking for something like
    Filter(
       DataSource,
       Len(OwnerSearch.Text) = 0 || StartsWith(Owner, OwnerSearch.Text),
       Len(NarrativeSearch.Text) = 0 || StartsWith(NarrativeSearch, NarrativeSearch.Text)
    )
     
    Please ✅ Does this answer your question if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider answering Yes to Was this reply helpful? or give it a Like ♥
    Visit my blog
    Practical Power Apps    LinkedIn  
  • Verified answer
    11manish Profile Picture
    3,333 on at
    If you're using SharePoint or Dataverse, prefer StartsWith() over in or Search() whenever possible because StartsWith() is delegable for many data sources and performs much better on large datasets.
    Example:
    SortByColumns(
        Filter(
            MyDataSource,
            IsBlank(txtOwner.Text) || StartsWith(Owner, txtOwner.Text),
            IsBlank(txtNarrative.Text) || StartsWith(Narrative, txtNarrative.Text),
            IsBlank(txtCategory.Text) || StartsWith(Category, txtCategory.Text),
            IsBlank(txtStatus.Text) || StartsWith(Status, txtStatus.Text)
        ),
        "Modified",
        Descending
    )
     
    This pattern is considered the best practice in Power Apps because it scales linearly—you simply add one filter condition for each new search field instead of creating exponentially more If() combinations.
  • Suggested answer
    WarrenBelz Profile Picture
    155,838 Most Valuable Professional on at
    Your original post asked for variable Sort - not variable Filter (you seem to have changed it since) - my last line actually actually asked for the clarificaiton, My response was based on the question you asked. My response to the question now posted - assuming you are using Classic Text Inputs is as per the alternative posted.
    Filter(
       DataSource,
       Len(OwnerSearch.Text) = 0 || StartsWith(Owner, OwnerSearch.Text),
       Len(NarrativeSearch.Text) = 0 || StartsWith(NarrativeSearch, NarrativeSearch.Text)
    )
     
    Please ✅ Does this answer your question if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider answering Yes to Was this reply helpful? or give it a Like ♥
    Visit my blog
    Practical Power Apps    LinkedIn  
  • AB-03121629-0 Profile Picture
    70 on at
    @11manish thanks so much for this! One of the fields I have is a dropdown menu of choices that are essentially labels. These choices line up with choice a field in the events im trying to search. Currently, I am trying to do:
    IsBlank(EventTypeFilter.Selected.Value) || EventTypeFilter.Selected.Value in Text(event_type)
    As you may guess, this doesn't work on large datasets unfortunately and ruins the results. I cant compare them directly without converting to text because EventTypeFilter.Selected is of type "Record" and event_type is of the dataverse choice type. Are there any ways to do this on large data sets?
  • WarrenBelz Profile Picture
    155,838 Most Valuable Professional on at
    If you are referring to a Single choice column then you just need 
    Len(EventTypeFilter.Selected.Value) = 0 || EventTypeFilter.Selected.Value = event_type.Value
    If this is the only Multiple choice (the rest being single choice or Text), you can do this
    With(
       {
          _Data:
          Filter(
             DataSource,
             Len(OwnerSearch.Text) = 0 || StartsWith(Owner, OwnerSearch.Text),
             Len(NarrativeSearch.Text) = 0 || StartsWith(NarrativeSearch, NarrativeSearch.Text)
          )
       },
       Filter(
          _Data,
          Len(EventTypeFilter.Selected.Value) = 0 || event_type.Value in EventTypeFilter.SelectedItems
       )
    )
    There are other combinations to manage Delegation as best as possible if I know exactly what your Data Source type is, what type of fields you have and what type of controls you are using to filter them.
     
    Note the Len() argument (Length) covers both IsBlank() and IsEmpty().
     
    Please ✅ Does this answer your question if my post helped you solve your issue. This will help others find it more readily. It also closes the item. If the content was useful in other ways, please consider answering Yes to Was this reply helpful? or give it a Like ♥
    Visit my blog
    Practical Power Apps    LinkedIn  
     

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

Season of Sharing Community Challenge Launch!

Jump in, show your community spirit, and win prizes!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
Valantis Profile Picture

Valantis 424

#2
WarrenBelz Profile Picture

WarrenBelz 355 Most Valuable Professional

#3
11manish Profile Picture

11manish 290

Last 30 days Overall leaderboard