Search code examples
office365powerapps

In Powerapps how to filter the gallery including an All option?


My app has 3 buttons marked Male, Female, and All, which pass search criteria to a variable used in a gallery filter.

All should return both male & female.

I have retained the search text box that the wizard adds, so users might search retail Promotions Starts With "Summer" && Category = "Male". But users also want to search for all Promotions starting with "Summer" regardless of Male/Female category. I have a Date Picker in play too.

This formula is applied to the BrowseGallry Items property:- SortByColumns(Filter('Promotions', StartsWith(PromoTitle, TxtSearch.Text) && StartDate>=DatePick.SelectedDate && PromoCategory=searchTerm), "PromoTitle", If(SortDescending1, Descending, Ascending))

The formula above works until I change the category to All. I have tried feeding "" and " " into the searchTerm variable via UpdateContext, but neither work. Any help would be much appreciated.


Solution

  • There are a few alternatives you can use for this.

    You can use another StartsWith condition, and set the search term to "". If the possible values of PromoCategory are Male and Female, then this should work (since everything "starts with" an empty string):

    SortByColumns(
        Filter(
            'Promotions',
            StartsWith(PromoTitle, TxtSearch.Text) &&
                StartDate >= DatePick.SelectedDate &&
                StartsWith(PromoCategory, searchTerm)),
        "PromoTitle",
        If(SortDescending1, Descending, Ascending))
    

    Another option is to use an If condition, so that if the search term is empty, then

    SortByColumns(
        Filter(
            'Promotions',
            StartsWith(PromoTitle, TxtSearch.Text) &&
                StartDate >= DatePick.SelectedDate &&
                If(searchTerm = "", true, PromoCategory = searchTerm)),
        "PromoTitle",
        If(SortDescending1, Descending, Ascending))
    

    Hope this helps!