web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Still can't figure out...
Power Apps
Answered

Still can't figure out syntax using 2 variables in a with for gallery items

(0) ShareShare
ReportReport
Posted on by 106

I have a working solution for filtering my gallery, but I noticed when I filter that my alternating row color is now off because it is based on ID. I found another solution to help with the alternate row color but I am having trouble merging the two solutions because they are both on Gallery Items. Any help merging and explaining the sections/syntax would be appreciated.

 

1st Working filter code:

 

 

 

 

With({_preFilter2:
 Filter(
 AddColumns(collEmailAlerts,
 "_dateColumn2", EmailDateTime
 ),
 !IsBlank(_dateColumn2) //And _dateColumn > DateAdd(Today(), -2, Hours)
 )
 }, 
 SortByColumns(
 Filter(_preFilter2,
 If(!IsBlank(_dateColumn2), 
 Switch(ddlAlertsDateFilter.Selected.Value,
 "24 Hrs", EmailDateTime > DateAdd(Now(), -1, Days),
 "4 Hrs", EmailDateTime > DateAdd(Now(), -4, Hours),
 "8 Hrs", EmailDateTime > DateAdd(Now(), -8, Hours),
 "3 Days", EmailDateTime > DateAdd(Now(), -3, Days),
 "7 Days", EmailDateTime > DateAdd(Now(), -7, Days),
 true
 ),
 true
 )
 ),
 "_dateColumn2", Descending
 )
 )

 

 

 

 

 

2nd Code I want to incorporate to help with alt row color (from http://powerapps.co.nz/auckland/index.php/our-blog/41-add-alternating-row-colours-to-power-apps-gallery)

 

 

 

 

With(
 {
 records:CustomGallerySample
 },
 ForAll(
 Sequence(CountRows(records)),
 Patch(
 Last(
 FirstN(records,Value)),
 {rowNumber: Value}
 )
 )
)

 

 

 

 

 

Categories:
  • Kevin00 Profile Picture
    106 on at

    Think I figured out the top part where I added "records: collEmailAlerts", but still having trouble with the rest of it.

    With({_preFilter2:
     Filter(
     AddColumns(collEmailAlerts,
     "_dateColumn2", EmailDateTime
     ),
     !IsBlank(_dateColumn2) //And _dateColumn > DateAdd(Today(), -2, Hours)
     ),
     records: collEmailAlerts 
     }, 
     SortByColumns(
     Filter(_preFilter2,
     If(!IsBlank(_dateColumn2), 
     Switch(ddlAlertsDateFilter.Selected.Value,
     "24 Hrs", EmailDateTime > DateAdd(Now(), -1, Days),
     "4 Hrs", EmailDateTime > DateAdd(Now(), -4, Hours),
     "8 Hrs", EmailDateTime > DateAdd(Now(), -8, Hours),
     "3 Days", EmailDateTime > DateAdd(Now(), -3, Days),
     "7 Days", EmailDateTime > DateAdd(Now(), -7, Days),
     true
     ),
     true
     )
     ),
     "_dateColumn2", Descending
     )
     )
  • Kevin00 Profile Picture
    106 on at

    Nope, Still not getting it. Tried below but that still returns a table for _rowNumber. Just not understanding syntax/order.

    With({_preFilter2:
     Filter(
     AddColumns(collEmailAlerts,
     "_dateColumn2", EmailDateTime,
     "_rowNumber", 
     ForAll(
     Sequence(CountRows(CollEmailAlerts)),
     Patch(
     Last(
     FirstN(collEmailAlerts,Value)),
     {-rowNumber: Value}
     )
     )
     ),
     !IsBlank(_dateColumn2) //And _dateColumn > DateAdd(Today(), -2, Hours)
     ),
     records: collEmailAlerts 
     }, 
     SortByColumns(
     Filter(_preFilter2,
     If(!IsBlank(_dateColumn2), 
     Switch(ddlAlertsDateFilter.Selected.Value,
     "24 Hrs", EmailDateTime > DateAdd(Now(), -1, Days),
     "4 Hrs", EmailDateTime > DateAdd(Now(), -4, Hours),
     "8 Hrs", EmailDateTime > DateAdd(Now(), -8, Hours),
     "3 Days", EmailDateTime > DateAdd(Now(), -3, Days),
     "7 Days", EmailDateTime > DateAdd(Now(), -7, Days),
     true
     ),
     true
     )
     ),
     "_dateColumn2", Descending
     )
     )

     

  • Verified answer
    RandyHayes Profile Picture
    76,299 Super User 2024 Season 1 on at

    @Kevin00 

    ForAll always returns a table...always!

     

    Please consider changing your Formula to the following:

    With({
     _compDate:
     Switch(ddlAlertsDateFilter.Selected.Value,
     "24 Hrs", DateAdd(Now(), -1, Days),
     "4 Hrs", DateAdd(Now(), -4, Hours),
     "8 Hrs", DateAdd(Now(), -8, Hours),
     "3 Days", DateAdd(Now(), -3, Days),
     "7 Days", DateAdd(Now(), -7, Days)
     ),
     },
     With({_data:
     SortByColumns(
     Filter(collEmailAlerts,
     !IsBlank(EmailDateTime) &&
     EmailDateTime > _compDate
     ),
     "EmailDateTime", Descending
     )
     },
     
     ForAll(
     Sequence(CountRows(_data), Patch(Last(FirstN(_data, Value)), {rowNumber: Value}))
     )
     ) 
    ) 

    That should give you what you are looking for.

    In the above formula, the ForAll returns the table that you need for your gallery with all of the columns of your data from the filter and the added column of the rowNumber.  

    Note, it does this with the table in _data - which is the table returned from the filter AND (importantly) is the sorted table.  This way your row numbers are always accurate.

     

    Ideally, you should consider changing the Items property of the ddlAlertsDateFilter to:

    Table(
     {Value: "24 Hrs", compDate: DateAdd(Today(), -1, Days)},
     {Value: "4 Hrs", compDate: DateAdd(Today(), -4, Hours)},
     {Value: "8 Hrs", compDate: DateAdd(Today(), -8, Hours)},
     {Value: "3 Days", compDate: DateAdd(Today(), -3, Days)},
     {Value: "7 Days", compDate: DateAdd(Today(), -7, Days)}
    )

    Then your Gallery Items property would be the following:

    With({_data:
     SortByColumns(
     Filter(collEmailAlerts,
     !IsBlank(EmailDateTime) &&
     EmailDateTime > ddlAlertsDateFilter.Selected.compDate
     ),
     "EmailDateTime", Descending
     )
     },
     ForAll(
     Sequence(CountRows(_data), Patch(Last(FirstN(_data, Value)), {rowNumber: Value}))
     )
    ) 
    

     

    I have a video on the subject of alternating row colors that might also be of interest to you.

     

    I hope this is helpful for you.

     

  • Kevin00 Profile Picture
    106 on at

    Thank you so much, especially for the explanation. I am trying to reformat my thinking to understand this way of doing things. 

    So basically I have to work inside-to-out with columns & data and then return a table. The first With returning the adjusted date field. Then in the formula portion of the 1st With, Use a 2nd With to set "_data" to a returned table with sorted columns (which used the "_compDate" obtained in the 1st With). Then in the formula part of the 2nd With, use the returned table (_data) to return another table with the sequence numbers to the gallery. Is my understanding correct?

     

    In the simpler version a table of display date groupings (24hr, 3day, etc.) under the field of "Value" and "_compDate" is the actual date/time. So a table with one row is being returned when the user selects from the "ddlAlertDateFilter" drop down. This is then used in filter to get "_data", which is also sorted. The "_data" table is then used in the formula part of the With to get sequence numbers.

     

    Trying to adjust my thinking and I think I am tracking what you are doing, but to be honest not sure I would have come up with the drop-down returning table rows on selection. This will take more practice for me.

     

    Thank you again for the solution and the explanation. 

  • Kevin00 Profile Picture
    106 on at

    One slight issue utilizing the second streamlined solution. I am getting "cannot use _Min values in this context" error. Trying to figure it out now.

  • Kevin00 Profile Picture
    106 on at

    Found it... just needed a closing parenthesis after "Sequence(CountRows(_data)"

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Season of Sharing Community Challenge Winners!

Congratulations to our community stars!

Kudos to our 2025 Community Spotlight Honorees

Expanding mentorship, skilling, and AI innovation

Leaderboard > Power Apps

#1
11manish Profile Picture

11manish 342 Super User 2026 Season 2

#2
M Bilal Khan Profile Picture

M Bilal Khan 271

#3
Mohsin Ali Profile Picture

Mohsin Ali 223

Last 30 days Overall leaderboard