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

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Collection code - dist...
Power Apps
Answered

Collection code - distinct and with - what's wrong with this?

(0) ShareShare
ReportReport
Posted on by 678

What's wrong with this code..
I want to get the distinct values of metroId from HotelRFP and then loop through them and get the metroCityName (and eventually other columns) from it and put that into a collection. I'm getting an error on the distinct part of this.

With(
 {_t,Distinct(HotelRFP,metroId)}
,
ClearCollect(colMetrosUsed,
 
 ForAll(
 _t,
 Patch({
 MetroName: LookUp(colMetros,ID=_t.Result,cityName)
 }
 )
 )
 )
)

 

Categories:
I have the same question (0)
  • Verified answer
    RandyHayes Profile Picture
    76,297 Super User 2024 Season 1 on at

    @sasrsc 

    Please consider changing your Formula to the following:

    ClearCollect(colMetrosUsed,
     With({_t : Distinct(HotelRFP,metroId)},
     ForAll(_t,
     {MetroName: LookUp(colMetros, ID=Result,cityName)}
     )
     )
    )

    Your use of Patch is not needed as you are not patching anything.  The ForAll function is a function that returns a table - not a For Loop!  So, in this case, you are just simply defining the return record schema as intended.

     

    I hope this is helpful for you.

  • sasrsc Profile Picture
    678 on at

    because I like to learn... this is my final product (using your suggested change). I have one question... why don't you have to do ID=_t.Result. You did ID=Result. I don't understand why that worked.  Yet, _t.Result doesn't work.

    ClearCollect(
     colMetrosUsed,
     With(
     {
     _t: Distinct(
     HotelRFP,
     metroId
     )
     },
     ForAll(
     _t,
     With(
     {
     _m: LookUp(
     colMetros,
     ID = Result
     )
     },
     {
     metroName: _m.field_1,
     metroCode: _m.Title
     }
     )
     )
     )
    )



  • RandyHayes Profile Picture
    76,297 Super User 2024 Season 1 on at

    @sasrsc 

    This is because _t is a table of records.  You only want to reference a value, not a table.

     

    Everything is on "context"!  Meaning, when you reference a column name for something, PowerApps will reference the "closest" thing to it in context.  So, since Result is used, it looks for the closest reference - which is the record of the ForAll table iteration.

     

    Also, you can refine your formula a bit more to:

    ClearCollect(colMetrosUsed,
     With({_t: Distinct(HotelRFP, metroId)},
     ForAll(_t,
     With(LookUp(colMetros, ID = Result),
     {
     metroName: field_1,
     metroCode: Title
     }
     )
     )
     )
    )

    You'll notice in the second With, there is no variable name provided. This is because With will provide an entire record for the context of the With scope.  When it encounters field_1, for example, it will look to the closest reference of that...which is the LookUp record result.

     

    I hope this is clear and helpful.

  • sasrsc Profile Picture
    678 on at

    As usual what a great explanation. I'd sign up for a class with you anytime 😉

     

  • RandyHayes Profile Picture
    76,297 Super User 2024 Season 1 on at

    @sasrsc 

    Good to know!!  That might be coming soon 😉

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 739 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 343 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard