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 / Filter a data source f...
Power Apps
Answered

Filter a data source for any string from collection

(1) ShareShare
ReportReport
Posted on by 716 Moderator

As the user types into a text control, I want to display FAQ questions in a gallery below the input based on words entered.

Here is where I'm at:

TextControl.OnChange:

Set(varQString, Self.Text);
ClearCollect(colQOWords, Split(varQString, " "));
ForAll(colQOWords, If(Len(ThisRecord.Value) > 4, Collect(colFWords, ThisRecord)));
That takes the contents of the control, splits it into a Collection at each space and then I look through that collection and build a second containing only the strings >4 characters.
This is what i was originally using on the Gallery.Items property:
If(
    !(
txtFrmQuestion
.Text),
    Filter(
        'colFAQ',
        string1 in Text(Question) || string2 in Text(Answer)
    )
)
I just can't figure out how to put a variable quantity of strings into the Filter to search in the Question and Answer column. 
Categories:
I have the same question (0)
  • CU06122028-0 Profile Picture
    4 on at
    Use a dynamic approach to handle a variable number of search strings. 
     
    # Sample FAQ data
    $faqs = @(
        @{Question = "How to reset my password?"; Answer = "Go to the settings page and select 'Reset Password'."},
        @{Question = "Where can I find the user manual?"; Answer = "The user manual is available under the 'Help' section."},
        @{Question = "How to contact support?"; Answer = "You can contact support via email or phone number provided on our website."}
    )
    # Export to a JSON file
    $faqs | ConvertTo-Json | Set-Content -Path "faqs.json"
    # Function to get filtered FAQs
    function Get-FilteredFAQs {
        param(
            [string[]]$keywords
        )
        
        # Read FAQ data from JSON
        $faqs = Get-Content -Path "faqs.json" | ConvertFrom-Json
        
        # Filter FAQs based on input keywords
        $filteredFAQs = $faqs | Where-Object {
            $match = $false
            foreach ($keyword in $keywords) {
                if ($_.Question -like "*$keyword*" -or $_.Answer -like "*$keyword*") {
                    $match = $true
                    break
                }
            }
            $match
        }
        
        return $filteredFAQs
    }
    # Example usage
    $searchKeywords = @("password", "support") # You can add more keywords as needed
    $filteredFAQs = Get-FilteredFAQs -keywords $searchKeywords
    $filteredFAQs | Format-Table -Property Question, Answer
     
  • Verified answer
    WarrenBelz Profile Picture
    154,799 Most Valuable Professional on at
    This should do the first part in the Items of the Gallery and also avoids "one use" collections and variables.
    If(
       Len(YourTextInput.Text) > 0,
       With(
          {
             _Items: 
             Split(
                YourTextInput.Text,
                " "
             )
          },
          Ungroup(
             ForAll(
                _Items As _I,
                Filter(
                   colFAQ,
                   _I.Value in Question || _I.Value in Answer
                )
             ),
             Value
          )
       )
    )
    If you want to only compare entered strings over 4 characters
    If(
       Len(YourTextInput.Text) > 4,
       With(
          {
             _Items:
             Filter(		 
                Split(
                   YourTextInput.Text,
                   " "
                ),
                Len(Value) > 4
             )
          },
          Ungroup(
             ForAll(
                _Items As _I,
                Filter(
                   colFAQ,
                   _I.Value in Question || _I.Value in Answer
                )
             ),
             Value
          )
       )
    )
     
    Please click 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 giving it a Like.
    MVP (Business Applications)    Visit my blog Practical Power Apps    Buy me a coffee
     
  • DCHammer Profile Picture
    716 Moderator on at
    @WarrenBelz You're brilliant. It works. Nearly perfectly. I'm going to mark it the solution now but there is one shortcoming.
     
    If you enter two keywords that exist within the same Question or Answer, you get two rows in the gallery.
     
    I believe the Ungroup function needs to be modified to only return distinct records somehow but I don't understand With and Ungroup functions well enough to do it without help.
  • Verified answer
    WarrenBelz Profile Picture
    154,799 Most Valuable Professional on at
    Yes you would get two records (it was not an issue you mentioned before). If you really want to eliminate these (you will only get the Question and Answer columns presented here)
    If(
       Len(txMultiSearch.Text) > 0,
       With(
          {
             _Items: Filter(
                Split(
                   txMultiSearch.Text,
                   " "
                ),
                Len(Value) > 4
             )
          },
          DropColumns(
             GroupBy(
                Ungroup(
                   ForAll(
                      _Items As _I,
                      Filter(
                         colFAQ,
                         _I.Value in Question || _I.Value in Answer
                      )
                   ),
                   Value
                ),
                Question,
                Answer,
                Grouped
             ),
             Grouped
          )
       )
    )
    The Ungroup function is required as the ForAll Table produces a Table field called Value for each iteration (containing all the records returned), so you need to expand that back out to display the records.
    A With() statement simply sets a (very) temporary Variable and is no different from the Collection you had except it does not require a "trigger" (it can be used directly in the Items of a Gallery), runs faster and does not leave a "one use" collection clogging up your App resources.
     
    Please click 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 giving it a Like.
    MVP (Business Applications)    Visit my blog Practical Power Apps    Buy me a coffee
     
     
  • DCHammer Profile Picture
    716 Moderator on at
    Thank you @WarrenBelz
     
    I didn’t mention it because it wasn’t happening the way I was ineffectually doing it initially. 
     
    I'm starting to get the With function. I can now understand what’s happening by reading the code block but I cant quite write one yet. 
     
    Although I have managed to modify a Filter statement you helped me with in the past that also populated a gallery and added a RowNo value.
     
    I now use that Wirh statement in every gallery I build, except this one so far, so that I can do all kinds of things based on knowing which row is which. 
     
    I’ll reply again on Monday after I get a chance to test this. 

    Enjoy your weekend. 
  • DCHammer Profile Picture
    716 Moderator on at
    @WarrenBelz Thank you so much sir. 
    This is a chunk of code that I'll certainly get the opportunity to reuse.

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 March Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 530

#2
WarrenBelz Profile Picture

WarrenBelz 459 Most Valuable Professional

#3
Haque Profile Picture

Haque 314

Last 30 days Overall leaderboard