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
Microsoft 365 Conference – December 6-8, 2022

Microsoft 365 Conference – December 6-8, 2022

Join us in Las Vegas to experience community, incredible learning opportunities, and connections that will help grow skills, know-how, and more.

Difinity Conference 2022

Difinity Conference 2022

Register today for two amazing days of learning, featuring intensive learning sessions across multiple tracks, led by engaging and dynamic experts.

European SharePoint Conference

European SharePoint Conference

The European SharePoint Conference returns live and in-person November 28-December 1 with 4 Microsoft Keynotes, 9 Tutorials, and 120 Sessions.

Power Apps Ideas

Changes to Ideas Coming

We are excited to announce a new way to share your ideas for Power Apps!

Top Solution Authors
Top Kudoed Authors
Users online (1,859)