cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Powerman1
Helper I
Helper I

Filter Galleries

Hi everyone.

 

I am having troubles filtering a gallery using buttons.

 

Info: Using a powerapp to track faults in a sharepoint list.

 

Basically have it so items in a list become resolved once a date has been entered in the Date Resolved field. Otherwise it is open.

 

What I'm trying to do:

Button: Open Faults - Any faults with Date Resolved being empty.

Button: Resolved - Faults with Date Resolved with date entered.

Button: All - Display all.

 

 

So far I've tried but no such luck.

Filter('Fault Register', 'Date Resolved'=IsBlank('Date Resolved'))

 

SortByColumns(Filter('Fault Register',!IsBlank('Date Resolved')),'Date Resolved',Ascending)

1 ACCEPTED SOLUTION

Accepted Solutions

@Powerman1 

You might want to go a slightly simpler way rather than creating Collection for the gallery.

 

Here in this example, you will have the three buttons and in the OnSelect formulas for them you will have the following:

  All.OnSelect - UpdateContext({_filter:"All"})

  Resolved.OnSelect - UpdateContext({_filter:"Resolved"})

  Open.OnSelect - UpdateContext({_filter:"Open"})

 

Then on your Gallery Items the following:

   Filter('Fault Register', (IsBlank('Date Resolved') && _filter="Open") || (!IsBlank('Date Resolved') && _filter="Resolved") || (_filter="All"))

 

To add sorting to this, adjust to the following:

   SortByColumns(Filter('Fault Register', (IsBlank('Date Resolved') && _filter="Open") || (!IsBlank('Date Resolved') && _filter="Resolved") || (_filter="All")), "Created", Descending)

 

The above will give you a formula that will use the condition value of the variable _filter to determine what logic to apply to the Filter condition.  

 

Hope this helps some.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

View solution in original post

6 REPLIES 6
v-yamao-msft
Community Support
Community Support

Hi @Powerman1 ,

 

Do you want to show different items on the gallery by clicking corresponding buttons?

Please take the following steps for a reference.

There is a date time column named as DateChange in the list.

Create a connection to the list.

Add a gallery control, and three buttons above the gallery. Change the text of the button separately to OpenFaults, Resolved, All.

Then OpenFaults.OnSelect with the following code:

ClearCollect(newlist11,Filter(List11,IsBlank(DateChange)))

Set Resolved.OnSelect with the following code:

ClearCollect(newlist11,Filter(List11,Not(IsBlank(DateChange))))

Set All.OnSelect property with the following function:

ClearCollect(newlist11,List11)

Then set the Items property of the Gallery as:

newlist11

1.PNG

 

Best regards,

Mabel

 

Community Support Team _ Mabel Mao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Hi,

 

This works brilliantly, however I am also trying to filter the gallery to show the most recent created by using the created field.

 

SortByColumns('Fault Register',"Created",Descending);

 

I tried adding

 

ClearCollect('newFault Register','Fault Register');SortByColumns('Fault Register',"Created",Descending) but no affect.

 

Thanks for your help on this.

@Powerman1 

You might want to go a slightly simpler way rather than creating Collection for the gallery.

 

Here in this example, you will have the three buttons and in the OnSelect formulas for them you will have the following:

  All.OnSelect - UpdateContext({_filter:"All"})

  Resolved.OnSelect - UpdateContext({_filter:"Resolved"})

  Open.OnSelect - UpdateContext({_filter:"Open"})

 

Then on your Gallery Items the following:

   Filter('Fault Register', (IsBlank('Date Resolved') && _filter="Open") || (!IsBlank('Date Resolved') && _filter="Resolved") || (_filter="All"))

 

To add sorting to this, adjust to the following:

   SortByColumns(Filter('Fault Register', (IsBlank('Date Resolved') && _filter="Open") || (!IsBlank('Date Resolved') && _filter="Resolved") || (_filter="All")), "Created", Descending)

 

The above will give you a formula that will use the condition value of the variable _filter to determine what logic to apply to the Filter condition.  

 

Hope this helps some.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

I prefer to not use Collections for the source of a Gallery unless I have to. If your list runs over 500 items (with sundry caveats), you're going to have to run your Collect() statement a number of times. Not to mention you're then working with a copy of your data, not the actual underlying table.

 

You could set up the Gallery to have an Items property built using an If() statement...

 

If (
    Condition1, Filter(....),
    Condition2, Filter(....),
    Condition3, yourUnfilteredSource
)

Then use your buttons' OnSelect property to establish whatever that condition you want to use:

 

...a string that is one of 3 values for your three cases

...a set of 3 boolean values, where each button only sets one to be true and the rest to be false

And feed that condition test into the If() statement:

If (
    yourString = "Faults", Filter(.....),
    yourString = "Resolved", Filter(.....),
    yourString = "All", yourUnfilteredSource
)

 

 

Now, to address your Sorting requirement, if you always want the Gallery to be sorted, you have to wrap the statement producing the table of Items in a SortByColumns() statement. In @v-yamao-msft 's suggestion, that would be:

 

SortByColumns(newList11, "Created", Descending)

...But you can wrap my If() statement in the same SortByColumns() to arrive at the same place:

 

SortByColumns(
    If (
        yourString = "Faults", Filter(.....),
        yourString = "Resolved", Filter(.....),
        yourString = "All", yourUnfilteredSource
    ),
    "Created",
    Descending
)

On the other hand, if you wanted to turn the sorting on/off, then you could either have another If() statement wrapping the whole statement, with one branch incorporating the SortByColumns() verbiage and other branch no, or you could embed an If() statement into the field selection for the SortByColumns() statement, provided you could supply what field should provide the default sorting.

For example, imagine you had a field in your datasource called "Assigned", and you added a checkbox to your screen that was labeled "Sort by Assigned" and named chkSortByAssigned. Typically you wanted to sort on Created, but when you checked the box, you wanted to sort on Assigned. That Items statement would look like:

SortByColumns(
    If (
        yourString = "Faults", Filter(.....),
        yourString = "Resolved", Filter(.....),
        yourString = "All", yourUnfilteredSource
    ),
    If ( chkSortByAssigned,
        "Assigned",
        "Created"
    )
    Descending
)

Some people might find that more dense to parse out what is going on, but I find it more helpful to have all of my code language right there in the one place, especially if it gets me the added benefits I listed, above.

I prefer the shorter formula version I posted, but the post by @TimRohr will work equally well and, as he mentioned, if you can avoid creating collections for these things, unless absolutely necessary, you are better off.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up below. Solved your problem? - Click on Accept as Solution below. Others seeking the same answers will be happy you did.
NOTE: My normal response times will be Mon to Fri from 1 PM to 10 PM UTC (and lots of other times too!)
Check out my PowerApps Videos too! And, follow me on Twitter @RandyHayes

Really want to show your appreciation? Buy Me A Cup Of Coffee!

This worked perfectly, exactly what I was trying to do. Thank for your help. I will look through the alternative as well to learn more.

Helpful resources

Announcements
PA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

This training provides practical hands-on experience in creating Power Apps solutions in a full-day of instructor-led App creation workshop.

Microsoft Build 768x460.png

Microsoft Build is May 24-26. Have you registered yet?

Come together to explore latest innovations in code and application development—and gain insights from experts from around the world.

May UG Leader Call Carousel 768x460.png

What difference can a User Group make for you?

At the monthly call, connect with other leaders and find out how community makes your experience even better.

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