Skip to main content

Notifications

Community site session details

Community site session details

Session Id : IybYygeetZYHUZjXGvTgj4
Power Apps - Building Power Apps
Answered

Lookup not working inside ForAll

Like (1) ShareShare
ReportReport
Posted on 27 Jan 2021 20:58:55 by 35

I have encountered several occasions inside a PowerApp where Lookup (even First(Filter())) does not work as intended.

 

So I created a very simple App to demonstrate.

3 Collections are created, and displayed via Gallery. The original App is way more complex, but this explains the basic issue.

 

The First Collection is the Index Collection, that consist of a Field used as ID for Lookup and an Index field.

ClearCollect(
 IndexCollection,
 {
 Title: "Title1",
 Index: "1"
 },
 {
 Title: "Title2",
 Index: "2"
 },
 {
 Title: "Title3",
 Index: "3"
 }
)

 

The second Collection represents the Data that needs the Index reference:

ClearCollect(
 MyData,
 {
 Title: "Title1",
 Text: "Additional Column"
 },
 {
 Title: "Title2",
 Text: "Additional Column2"
 },
 {
 Title: "Title3",
 Text: "Additional Column3"
 }
)

 

The Idea now is to do a ForAll on the Gallery, displaying the MyData (again, the real life scenario is a bit more complex) and creating a new Collection with Lookup information from the the Index Collection

ForAll(
 Gallery2.AllItems,
 Collect(
 myResults,
 {
 Title: ThisRecord.Title,
 Index: LookUp(
 IndexCollection,
 Title = ThisRecord.Title
 ).Index
 }
 );
);

 

This now creates a new Collection "myResults", but it has the Value "1" for every single Index.

Dajaran_0-1611778617324.png

 

If instead of doing it inside of the ForAll, I use the same Code (without the This.Record Part), it works as intended.

LookUp(
 IndexCollection,
 Title = "Title1"
).Index

 

And just out of curiosity I added another Collection inside the ForAll, to Capture Each Value

 Collect(
 StatusMessage,
 "Title: "& ThisRecord.Title & " Value for Lookup: " & LookUp(
 IndexCollection,
 Title = ThisRecord.Title
 ).Index
 );

 

The Result is the same:

Dajaran_1-1611780915225.png

 

  • nagasri Profile Picture
    10 on 08 Feb 2024 at 13:55:37
    Re: Lookup not working inside ForAll

    I appreciate your work. Thank you !!!!

  • cnr Profile Picture
    144 on 09 May 2022 at 04:15:56
    Re: Lookup not working inside ForAll

    Hi @RandyHayes,

     

    I am trying to use the same approach for the solution you provided above.

     

    I am attempting to use a Lookup within a forAll in order to match the date in my Principle & Interest collection to the date in my Paydown table and have the Amounts in my Paydown table taken away from the Principle amounts in my Principle & Interest collection.

     

    cnr_0-1652069439767.png

     

    ClearCollect(
    MyPrincipleAndInterestTable,
    ForAll(Sequence(DateDiff(varStrtDate,varEndDate)) As Num,
     Collect(
     myPrincipleAndInterest,
     {
     ItemNumber: Num,
     ItemDay: DateAdd(varStrtDate, Num.Value ,Days),
     Interest: Principle_1 * IntValue_1,
     Principle: Principle_1.Text
     }
     ))
    );
    
    
    ClearCollect(
     MyPrincipleAndInterestTableWithPaydowns,
     ForAll(
     MyPrincipleAndInterestTable As _items,
     {
     ItemNumber: _items.Value.ItemNumber,
     ItemDay: _items.Value.ItemDay,
     Interest:_items.Value.Interest,
     Principle: LookUp(PaydownGallery_1.AllItems, PaydownDate = _items.Value.ItemDay, Principle - Amount)
     }
     ));

     

    At the moment I'm getting this error on the second collection - show below (These types cant be compared Datetime/Table)

    cnr_1-1652069714806.png

     

  • tcoalson Profile Picture
    9 on 29 Jun 2021 at 16:46:29
    Re: Lookup not working inside ForAll

    This was very helpful!  Thanks!

  • RandyHayes Profile Picture
    76,287 Super User 2024 Season 1 on 27 Jan 2021 at 21:19:05
    Re: Lookup not working inside ForAll

    @Dajaran

    Yup, As is a much needed operator as you have to realize that ThisRecord will refer to the function for which it is contained.  In your case, it was the LookUp - not the ThisRecord that you wanted!

     

    As for the formula you have now, yes, you can use that method, but it is a waste and a performance hog to use the ForAll as a For/Loop.  

    In your case, you are iterating through every item and creating a record and taking the expensive action of collecting it row by row into the collection.  And your ForAll results are dumped into nowhere.

    With the formula I provided, your ForAll creates a Table of records and then assigns them all to the collection in one shot.

  • Dajaran Profile Picture
    35 on 27 Jan 2021 at 21:14:24
    Re: Lookup not working inside ForAll

    Perfect !

    I didn't know about the "As" inside the ForAll.

    Oh, and by the way it still works with the the ForAll as kind of a pseudo Loop (ignoring the resulting table 😁) and the Collect inside, with that little addition "As _items". 

    ForAll(
     Gallery2.AllItems As _items,
     Collect(
     myResults,
     {
     Title: _items.Title,
     Index: LookUp(
     IndexCollection,
     Title = _items.Title
     ).Index
     }
     );
    );

     

  • Verified answer
    RandyHayes Profile Picture
    76,287 Super User 2024 Season 1 on 27 Jan 2021 at 21:02:24
    Re: Lookup not working inside ForAll

    @Dajaran 

    This is because ThisRecord will then refer to the ThisRecord of the LookUp.

    Please consider changing your Formula to the following:

    Collect(
     myResults,
     ForAll(
     Gallery2.AllItems As _items,
     {
     Title: _items.Title,
     Index: LookUp(
     IndexCollection,
     Title = _items.Title,
     Index
     )
     }
     )
    );

    Note: ForAll returns a table, it's not a For/Loop.  Simply assign the results to the myResults you are trying to collect.

     

    I hope this is helpful for you.

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

Understanding Microsoft Agents - Introductory Session

Confused about how agents work across the Microsoft ecosystem? Register today!

Warren Belz – Community Spotlight

We are honored to recognize Warren Belz as our May 2025 Community…

Kudos to the March Top 10 Community Stars!

Thanks for all your good work in the Community!

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 146,731 Most Valuable Professional

#2
RandyHayes Profile Picture

RandyHayes 76,287 Super User 2024 Season 1

#3
Pstork1 Profile Picture

Pstork1 66,075 Most Valuable Professional

Leaderboard