cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
AlexanderUK
Advocate I
Advocate I

Filter Gallery using Text Input and Date Range Using Two DatePickers

Hi all.

 

I'm trying to expand a search tool on my powerapp that currently allows to filter by the "Title" Column on the "Cases" list (and has a Sort Button tool) with

 

SortByColumns(Filter([@'Cases'], StartsWith(Title, TextSearchBox1.Text)), "Title", If(SortDescending1, Descending, Ascending)) 

 

But I'm trying to get it to also include a Date Range as listed in StartDatePicker and EndDatePicker to filter by column "EnquiredDate"

 

I have the code for the date picker working solo as:

 

Sort(
    If(
        IsBlank(EndDatePicker.SelectedDate),
        If(
            IsBlank(StartDatePicker),
            'Cases',
            Filter(
                'Cases',
                'EnquiredDate' >= StartDatePicker.SelectedDate
            )
        ),
        If(
            IsBlank(StartDatePicker),
            Filter(
                'Cases',
                'EnquiredDate' <= EndDatePicker.SelectedDate
            ),
            Filter(
                'Cases',
                'EnquiredDate' <= EndDatePicker.SelectedDate,
                'EnquiredDate' >= StartDatePicker.SelectedDate
            )
        )
    ),
    'EnquiredDate',
    Descending
)

 

I've tried a few different variations with no luck, usually just filtering by the 'Title' search and it completely ignores the Date Pickers. Can't quite get my head around how to merge these codes together, any ideas?

1 REPLY 1
Drrickryp
Super User
Super User

@AlexanderUK As far as I can tell, you have four conditions,
1 Both datepickers have selected dates,
2 Only the start datepicker has a selected date,

3 Only the end datepicker has a selected date,

4 Neither datepicker has a selection.
I found it easier to think about the problem from the standpoint of which datepickers have a selection rather then which ones are empty.  The solution below used the With() function to do an initial filter on the cases based on the Textbox search and the sort is done at that level. Then, the resulting sorted and filtered table (_cases) is then further filtered in a second stage according to the datepickers.   Only one If() is needed because the logic proceeds until a true condition is found and then it stops and performs the filter.    

 

 

With({_cases:SortByColumns(Filter([@'Cases'], StartsWith(Title, TextSearchBox1.Text)), "Title", If(SortDescending1, Descending, Ascending)) 
}, 
      If(!isBlank(StartDatePicker.SelectedDate) && !IsBlank(EndDatePicker.SelectedDate,
          Filter(
                _cases,
                'EnquiredDate' <= EndDatePicker.SelectedDate,
                'EnquiredDate' >= StartDatePicker.SelectedDate
            ),
           !isBlank(StartDatePicker.SelectedDate), 
          Filter(
                _cases,
                'EnquiredDate' >= StartDatePicker.SelectedDate
                 ),
           !IsBlank(EndDatePicker.SelectedDate),
          Filter(
                _cases,
                'EnquiredDate' <= EndDatePicker.SelectedDate
                 ),
           _cases
       )
)

 

 

 

 

 

 

 

Helpful resources

Announcements
Ignite 2022

WHAT’S NEXT AT MICROSOFT IGNITE 2022

Explore the latest innovations, learn from product experts and partners, level up your skillset, and create connections from around the world.

Power Apps Africa Challenge 2022

Power Apps Africa Challenge

Your chance to join an engaging competition of Power Platform enthusiasts.

Users online (1,868)