cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
jerrycrabb
Helper II
Helper II

Looking for a better solution for Gallery Rows + Multiple Filters + Sort

Hi!

What I have built is a gallery that can be filtered by 7 combo-boxes and a text search.  The filters (combo boxes) are all on the right side of the screen, the gallery takes up most of the screen starting from the left, and a patch-form is a container made visible by clicking the gallery item.

  •  When the gallery is filtered each row is given a row number.
  • When the patch-form pops-up, and the user can cycle through the currently assigned row numbers using the left and right icons. The patch-form is used to either edit existing records, or create new ones.

 

Being able to filter, get x number of records, and cycling through those via form It's a small feature that I had no idea my users would love as much as they do.  

 

To accomplish this I have combined a filter solution I found by @WarrenBelz 

 

https://www.practicalpowerapps.com/data/filters/

Filter(
   Devices,
   If(
      IsBlank(cbMan.Selected.Title), 
      true,
      ManufacturerName = cbMan.Selected.Title
   ) && 
   If(
      IsBlank(cbType.Selected.Result), 
      true, 
      DeviceType = cbType.Selected.Result
   ) && 
   If(
      IsBlank(txtName.Text), 
      true, 
      StartsWith(
         Model,
         txtName.Text
      )
   )
)

 

 

and a gallery row number solution I found by Shane Young

 

 

https://www.youtube.com/watch?v=Uuolue31t5o

ForAll(colJuggles,
	Collect(tempCollection,
		Last(FirstN(
			AddColumns(
				DropColumns(colJuggles, "row"),
				"row",CountRows(tempCollection)+1
				),
			CountRows(tempCollection)+1)
			)
		)
	)
);
ClearCollect(ColJuggles, tempCollection);
Clear(tempCollection);

 

 

both authors would probably be quick to point out that mashing these solutions together is not a great combination. 

 

Because the solution for adding row numbers to the gallery requires that a collection be rebuilt every time a filter parameter changes - the OnChange property of each filter must rebuild the collection. 

 

It's been working for a while, but now I'd like to add the ability to sort by a few columns.  I think it's time for me to re-evaluate the whole implementation. 

 

Here's a look at my actual code for OnChange for each combo box and text search (as well as when patching, and removing, and...)

 

ClearCollect(colactions,
    Search(Filter(
    colActivityTracker,
    If(
        IsBlank(cmb_Tracker_FilterPane_Status_Filter.Selected.Value),
        true,
        Status.Value in cmb_Tracker_FilterPane_Status_Filter.SelectedItems.Value)
        &&
        If(IsBlank(cmb_Tracker_FilterPane_Priority_Filter.Selected.Value),
        true,
        PRIORITY.Value in cmb_Tracker_FilterPane_Priority_Filter.SelectedItems.Value)
         &&
        If(IsBlank(cmb_Tracker_FilterPane_ProgramArea_Filter.Selected.ProgramAreas),
        true,
        'PROGRAM AREA'.Value in cmb_Tracker_FilterPane_ProgramArea_Filter.SelectedItems.ProgramAreas)
         &&
        If(IsBlank(cmb_Tracker_FilterPane_UserLead_Filter.Selected.Users),
        true,
        'Created By'.Email in cmb_Tracker_FilterPane_UserLead_Filter.SelectedItems.Users)
        &&
        If(IsBlank(cmb_Tracker_FilterPane_UserAlt_Filter.Selected.AltUsers),
        true,
        Alternate.Email in cmb_Tracker_FilterPane_UserAlt_Filter.SelectedItems.AltUsers)
        &&
        If(IsBlank(cmb_Tracker_FilterPane_COP_Filter.Selected.COP),
        true,
        'SERVICING COP'.Value in cmb_Tracker_FilterPane_COP_Filter.SelectedItems.COP)
        &&
        If(IsBlank(cmb_Tracker_FilterPane_ReqOffice_Filter.Selected.ReqOffice),
        true,
        'REQUESTING OFFICE'.Value in cmb_Tracker_FilterPane_ReqOffice_Filter.SelectedItems.ReqOffice)
        &&
        If(varTaskerFilter=false,true,TASKER.Value="YES")),txt_Tracker_FilterPane_TitleSearch.Text,"Title")
);

ForAll(
    colactions,
    Collect(
        tempCollection,
        Last(
            FirstN(
                AddColumns(
                    DropColumns(
                        colactions,
                        "row"
                    ),
                    "row",
                    CountRows(tempCollection) + 1
                ),
                CountRows(tempCollection) + 1
            )
        )
    )
);
ClearCollect(
    colactions,
    tempCollection
);
Clear(tempCollection);
//Collection of COP PERS Users
ClearCollect(
    colusers,
    AddColumns(
        GroupBy(
            AddColumns(
                colactions,
                "Users",
                'Created By'.Email
            ),
            "Users",
            "UserData"
        ),
        "UserCountValue",
        CountRows(UserData),
        "UserCountView",
        Concatenate(
            CountRows(UserData),
            " Action(s)"
        ),
        "UserFirstName",
        Office365Users.UserProfileV2(Users).givenName,
        "UserLastName",
        Office365Users.UserProfileV2(Users).surname,
        "UserPhoto",
        If(
            !IsEmpty(Office365Users.UserPhotoV2(Users)),
            Office365Users.UserPhotoV2(Users),
            R
        ),
        "UserPhoto2",
        Office365Users.UserPhotoV2(Users),
        "UserName",
        Concatenate(Left(Office365Users.UserProfileV2(Users).givenName,1),". ",Office365Users.UserProfileV2(Users).surname)
    )
);
//Collection of COP PERS Users
ClearCollect(
    colusers,
    AddColumns(
        GroupBy(
            AddColumns(
                colactions,
                "Users",
                'Created By'.Email
            ),
            "Users",
            "UserData"
        ),
        "UserCountValue",
        CountRows(UserData),
        "UserCountView",
        Concatenate(
            CountRows(UserData),
            " Action(s)"
        ),
        "UserFirstName",
        Office365Users.UserProfileV2(Users).givenName,
        "UserLastName",
        Office365Users.UserProfileV2(Users).surname,
        "UserPhoto",
        If(
            !IsEmpty(Office365Users.UserPhotoV2(Users)),
            Office365Users.UserPhotoV2(Users),
            R
        ),
        "UserPhoto2",
        Office365Users.UserPhotoV2(Users),
        "UserName",
        Concatenate(Left(Office365Users.UserProfileV2(Users).givenName,1),". ",Office365Users.UserProfileV2(Users).surname)
    )

 

The gallery itself is using the output of "colactions" (Which I just now realized was supposed to have an upper case "A"...oops).

 

 One reason I'm using combo boxes to filter, is that it allows me to build collections that shows how many records a filter item would yield. For example I can see in the combo box that I I select myself, 9 (or however many there are) records exist for me. AND I can sort that combo box so that the user with most records is right at the top. 

 

I've started to look at @mdevaney  's sorting solution using the "With" function.  I think that could work well but I haven't entirely sorted that out yet (pun intended).

 

Thanks in advance for any recommendations!

 

All the best. 

 

-Jerry

1 ACCEPTED SOLUTION

Accepted Solutions
RandyHayes
Super User
Super User

@jerrycrabb 

Yes, you are combining a few recommendations that are not so great to start.  

I would skip the collections all together as you really don't need them.  The Filter can be cleaned up a lot (avoid using If statements in your Filter criteria).  The row numbers uses the ForAll backward (ForAll is a function that returns a table - not a For Loop!!)

 

If you set your Gallery Items property to the following, it would be all you need:

With({_items:
    Search(
        Filter(colActivityTracker,
            IsBlank(cmb_Tracker_FilterPane_Status_Filter.Selected.Value) || Status.Value in cmb_Tracker_FilterPane_Status_Filter.SelectedItems.Value) &&
            IsBlank(cmb_Tracker_FilterPane_Priority_Filter.Selected.Value) || PRIORITY.Value in cmb_Tracker_FilterPane_Priority_Filter.SelectedItems.Value) &&
            IsBlank(cmb_Tracker_FilterPane_ProgramArea_Filter.Selected.ProgramAreas) || 'PROGRAM AREA'.Value in cmb_Tracker_FilterPane_ProgramArea_Filter.SelectedItems.ProgramAreas) &&
            IsBlank(cmb_Tracker_FilterPane_UserLead_Filter.Selected.Users) || 'Created By'.Email in cmb_Tracker_FilterPane_UserLead_Filter.SelectedItems.Users) &&
            IsBlank(cmb_Tracker_FilterPane_UserAlt_Filter.Selected.AltUsers) || Alternate.Email in cmb_Tracker_FilterPane_UserAlt_Filter.SelectedItems.AltUsers) &&
            IsBlank(cmb_Tracker_FilterPane_COP_Filter.Selected.COP) || 'SERVICING COP'.Value in cmb_Tracker_FilterPane_COP_Filter.SelectedItems.COP) &&
            IsBlank(cmb_Tracker_FilterPane_ReqOffice_Filter.Selected.ReqOffice) || 'REQUESTING OFFICE'.Value in cmb_Tracker_FilterPane_ReqOffice_Filter.SelectedItems.ReqOffice)
            !varTaskerFilter || TASKER.Value="YES"
        ),
        txt_Tracker_FilterPane_TitleSearch.Text,"Title"
    )
    },
    
    ForAll(Sequence(CountRows(_items),
        Patch(Index(_items, Value), {row: Value})
    )
)

 

At this point you can apply sorting to that formula however you like.  Not sure what your criteria is for sorting, but if you need help with that...just specify.

 

The advantage of the above is that it is dynamic.  You don't need to duplicate all your data into a collection in memory.  It will change as the source changes.

 

Can you provide more about how you are using the second part of your formula - the colusers?

_____________________________________________________________________________________
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

9 REPLIES 9
RandyHayes
Super User
Super User

@jerrycrabb 

Yes, you are combining a few recommendations that are not so great to start.  

I would skip the collections all together as you really don't need them.  The Filter can be cleaned up a lot (avoid using If statements in your Filter criteria).  The row numbers uses the ForAll backward (ForAll is a function that returns a table - not a For Loop!!)

 

If you set your Gallery Items property to the following, it would be all you need:

With({_items:
    Search(
        Filter(colActivityTracker,
            IsBlank(cmb_Tracker_FilterPane_Status_Filter.Selected.Value) || Status.Value in cmb_Tracker_FilterPane_Status_Filter.SelectedItems.Value) &&
            IsBlank(cmb_Tracker_FilterPane_Priority_Filter.Selected.Value) || PRIORITY.Value in cmb_Tracker_FilterPane_Priority_Filter.SelectedItems.Value) &&
            IsBlank(cmb_Tracker_FilterPane_ProgramArea_Filter.Selected.ProgramAreas) || 'PROGRAM AREA'.Value in cmb_Tracker_FilterPane_ProgramArea_Filter.SelectedItems.ProgramAreas) &&
            IsBlank(cmb_Tracker_FilterPane_UserLead_Filter.Selected.Users) || 'Created By'.Email in cmb_Tracker_FilterPane_UserLead_Filter.SelectedItems.Users) &&
            IsBlank(cmb_Tracker_FilterPane_UserAlt_Filter.Selected.AltUsers) || Alternate.Email in cmb_Tracker_FilterPane_UserAlt_Filter.SelectedItems.AltUsers) &&
            IsBlank(cmb_Tracker_FilterPane_COP_Filter.Selected.COP) || 'SERVICING COP'.Value in cmb_Tracker_FilterPane_COP_Filter.SelectedItems.COP) &&
            IsBlank(cmb_Tracker_FilterPane_ReqOffice_Filter.Selected.ReqOffice) || 'REQUESTING OFFICE'.Value in cmb_Tracker_FilterPane_ReqOffice_Filter.SelectedItems.ReqOffice)
            !varTaskerFilter || TASKER.Value="YES"
        ),
        txt_Tracker_FilterPane_TitleSearch.Text,"Title"
    )
    },
    
    ForAll(Sequence(CountRows(_items),
        Patch(Index(_items, Value), {row: Value})
    )
)

 

At this point you can apply sorting to that formula however you like.  Not sure what your criteria is for sorting, but if you need help with that...just specify.

 

The advantage of the above is that it is dynamic.  You don't need to duplicate all your data into a collection in memory.  It will change as the source changes.

 

Can you provide more about how you are using the second part of your formula - the colusers?

_____________________________________________________________________________________
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!

@RandyHayes 

Thank you so much for that quick response!   I will dig into what you suggested in the morning. I like the looks of that, a lot. 

To be honest I implemented the the row numbers ForAll function without completely understanding it. When it worked I just sort of moved on with the expectation that I'd dig into it more later.  It's not usually how I operate. 

 

colusers (which I wish I had named colUsers), is a collection of everyone who has created an "Action". Added to that collection is a count of the number of "Actions" they have created.  That count is displayed along with the name of the user in cmb_Tracker_FilterPane_UserLead_Filter.  That combo box allows filtering the gallery of "Actions" by user, and it's sorted so that the user with the most actions is at the top.

 

I didn't post my entire code. There are a bunch of collections (one for each filter) just like the one I described above, that pull a list of some attribute and adds a column that counts the number of rows for that attribute.

 

Oh, and at that time I didn't realize I wasn't going to be able to get user photos to display in a combo box, so that's what that UserPhoto/UserPhoto2 business is about.  I guess I forgot that was still in there. 

 

-Jerry

 

 

 

@RandyHayes 

Good morning!   I've had a chance to play with your suggestion. 

I don't totally understand the second part of the formula and therefore am struggling to get it to work. 

 

Here's what I think it's doing:

*CountRows is getting total number of results from the filter criteria "_items"

*Sequence is taking the ^total and generating a single column table with 1-N (N= total)

*ForAll is taking the values in the new ^sequence table and....patching those values into a new column in _items? <-- I think this is where I'm fuzzy.

 

As written "Value", inside the Index function, isn't recognized as valid.

 

I don't have a "row" column in the source data.  My The old code was adding that column to a temporary collection.  I can add that column to colActivityTracker, just verifying that I need to.   

@RandyHayes 

I have temporarily removed the "With" function and am playing with the filter formula. 

This is probably bad phrasing, but the filters don't seem to stack, but rather override one another. 

Example: There are 114 "Active" actions.  Setting the "Status" filter to "Active" displays exactly 114 actions. BUT adding "Low" to the "Priority" filter now returns 145 actions, ignoring the "Status" filter. 

 

This is likely due to my implementation.  When writing the formula it did not like having a ")" before each "&&". I had to remove each of those.

aha figured out the filter bit.  I shouldn't have removed the ")" before "&&". I should have added "(" before "IsBlank".   The below formula works as intended for the filter part. 

Search(
    Filter(
        colActivityTracker,
            //Status Filter
        (IsBlank(cmb_Tracker_FilterPane_Status_Filter.Selected.Value) || Status.Value in cmb_Tracker_FilterPane_Status_Filter.Selected.Value) &&
            //Priority Filter
        (IsBlank(cmb_Tracker_FilterPane_Priority_Filter.Selected.Value) || PRIORITY.Value in cmb_Tracker_FilterPane_Priority_Filter.Selected.Value) &&
            //Program Area Filter
        (IsBlank(cmb_Tracker_FilterPane_ProgramArea_Filter.Selected.ProgramAreas) || 'PROGRAM AREA'.Value in cmb_Tracker_FilterPane_ProgramArea_Filter.Selected.ProgramAreas) &&
            //User Filter
        (IsBlank(cmb_Tracker_FilterPane_UserLead_Filter.Selected.Users) || 'Created By'.Email in cmb_Tracker_FilterPane_UserLead_Filter.Selected.Users) &&
            //Alternate Filter
        (IsBlank(cmb_Tracker_FilterPane_UserAlt_Filter.Selected.AltUsers) || Alternate.Email in cmb_Tracker_FilterPane_UserAlt_Filter.Selected.AltUsers) &&
            //COP Filter
        (IsBlank(cmb_Tracker_FilterPane_COP_Filter.Selected.COP) || 'SERVICING COP'.Value in cmb_Tracker_FilterPane_COP_Filter.Selected.COP) &&
            //Requesting Office Filter
        (IsBlank(cmb_Tracker_FilterPane_ReqOffice_Filter.Selected.ReqOffice) || 'REQUESTING OFFICE'.Value in cmb_Tracker_FilterPane_ReqOffice_Filter.Selected.ReqOffice) &&
            //Tasker Filter
        (!varTaskerFilter || TASKER.Value = "YES")
    ),
    txt_Tracker_FilterPane_TitleSearch.Text,
    "Title"
)

@RandyHayes 

I think I'm a LOT closer.  I found your video on alternating colors for gallery rows and came up with this for the last bit of the With function: 

ForAll(Sequence(CountRows(Actions)), Patch(Last(FirstN(Actions, Value)), {row: Value})))

I'm using "Actions" where you had "_items". 

 

I understand now that "Value" is derived from the "Sequence" Function.  I'm still a little fuzzy on the magic we're performing with Patch, and I need to figure it out - because now my buttons to cycle through records don't work (they no longer recognize "row" in the source data"). 

 

I also think the way we're  you're using patch here may be a better solution to for all the collections I'm building for my combo box filters.... I'm just not sure how...yet.

figured the back/forward cycle buttons on the patch form.  Since "row" exists in the gallery source data, I'm using the following code for the forward button (the back button just uses "-1" instead of "+1". )

Notes:  The patch from uses "varSelectedRecord" to display the correct record, "Activity Tracker" is the underlying Sharepoint list, the gallery is titled ""gal_Tracker_ActionList" - which is fed with a "With" function that adds row to "Actions".

 

Set(
     varSelectedRecord,
     Lookup(
          "Activity Tracker',
          ID = Lookup(
               gal_Tracker_ActionList.AllItems,
               row = Lookup(
                    gal_Tracker_ActionList.AllItems,
                    ID = varSelectedRecord.ID,
                    row + 1
               ),
               ID
         )
    )
)

 

 

So the last bit is to figure out what to do with my filter combo boxes.  Originally each time a combo box changed, the collection that fed the gallery was rebuilt. At the same time a collection for each combo box was rebuilt displaying the number of records each option would yield, and sorting them in descending order. 

 

In practice it's a lot cooler than it sounds, when you change one filter, the rest of the combo boxes would update to only display relevant options, and the number of actions for each option.  Example: If I set "Status" to "Delayed", then the user combo box would change to only show "users" with actions that were delayed. It would also filter the users so that the ones with the most delayed actions would be at the top. 

 

I still think something similar to the "with" function that added row numbers to the gallery will be the ticket here. I'm just not sure how, yet.

jerrycrabb
Helper II
Helper II

For those following, or who might read through at a later date, I'm using Randy's solution to filter my gallery, and I'm keeping the ClearCollect for each combo box in the OnChange property of all the combo boxes.  I'm sure there's a more elegant way to work those combo-box-collections, and I'm sure I'll come back to that at a later date 🙂

Helpful resources

Announcements
Power Apps News & Annoucements carousel

Power Apps News & Announcements

Keep up to date with current events and community announcements in the Power Apps community.

Community Call Conversations

Introducing the Community Calls Conversations

A great place where you can stay up to date with community calls and interact with the speakers.

Power Apps Community Blog Carousel

Power Apps Community Blog

Check out the latest Community Blog from the community!

Top Solution Authors
Top Kudoed Authors
Users online (14,494)