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
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

M365 768x460.jpg

Microsoft 365 Collaboration Conference | December 7–9, 2021

Join us, in-person, December 7–9 in Las Vegas, for the largest gathering of the Microsoft community in the world.

Top Solution Authors
Top Kudoed Authors
Users online (2,064)