cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
MichelH
Advocate V
Advocate V

Sequential processing of collection seems hindered by With in ForAll

I notice a peculiar behavior since the release of the power apps release 3.22042.8

 

When I loop through a collection using the ForAll function, I find it goes through the records in the exact order that they have that collection.

 

However, when I take the record that comes back, and use the With  function and the LookUp function to lookup a second record from another source, and then write to a target collection using both obtained records, the order of the first collection is no longer respected.

 

The target collection now always has a random order.

 

I'm I the only one seeing this?

 

 

ForAll( collection1 As Record1,
	With( { Record2: LookUp( anothersource, AField=Record1.Fld1  ) },
		Collect( targetcollection,
			{
				Fld1: Record1.Fld1
				Fld2: Record1.Fld2
				Fld3: Record2.Fld1
			}
		)
	)
)

 

 

 

Thanks for any response

 

Michel

10 REPLIES 10
Pstork1
Dual Super User
Dual Super User

Technically, ForAll doesn't do sequential processing.  Frequently if you are doing something simple it will appear to be sequential because it takes the same amount of time to do all the tasks.  But Power Apps is declarative not procedural.  So even with a ForAll it actually processes the equations all at once. It is not guaranteed to follow a sequence.  When you get into something like a With there become too many variables to predict that it will be sequential.  So in that case the real nature of ForAll comes out.



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.
MichelH
Advocate V
Advocate V

Thank you @Pstork1 ,

Are you drawing from you experience here, or do you have found this somewhere in the docs?

Not that I'm questioning what you say, but I see the below in the ForAll docs . 

 

Return value

The result of each formula evaluation is returned in a table, in the same order as the input table.


The With docs do not mention anything regarding the sequence of the returned results.

The whole thing worked fine for months, until this last power apps release I believe,

 

 

ForAll( collection1 As Record1,
	Collect( log , "forall " & Record1.Fld1);
	With( { Record2: LookUp( anothersource, AField=Record1.Fld1  ) },
		Collect( log , "with " & Record1.Fld1);
		Collect( targetcollection,
			{
				Fld1: Record1.Fld1
				Fld2: Record1.Fld2
				Fld3: Record2.Fld1
			}
		)
	)
)

 

 

When I add some extra logging like above I see:

 

 

forall record1
forall record2
forall record3
forall record4
forall record5
with record2
with record5
with record1
with record4
with record3

 

 

This appears to suggest the randomization appears later.

But given the "simple" and direct nature microsoft suggests for its power apps platform, I think it is natural to assume a first in first out approach.

If microsoft intends another way, they better update their docs accordingly.


Br,
Michel

Pstork1
Dual Super User
Dual Super User

This is from the first paragraph of the docs.

The ForAll function evaluates a formula for all the records in a table. The formula can calculate a value and/or perform actions, such as modifying data or working with a connection. Use the With function to evaluate the formula for a single record.

Notice the With function is only meant to work for a single record. So do I have an explicit reference that says ForAll isn't sequential, no.  But Power Apps is not a procedural system, its declarative.  That means that formulas are simply applied.  My experience has seen multiple cases where people tried to use ForAll() like a Loop.  It is like a Loop, but one where concurrency has been enabled. I don't write the documentation.  I just try to help people solve the issues they are having. If you want true procedural sequential processing send the data set to a Power Automate flow. Its procedural.



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.
MichelH
Advocate V
Advocate V

Thank you @Pstork1 

I see what you are saying.

I appreciate your input, I will keep this in mind.

My issue is not really that it doesn't work, but more that it worked for about 8-9 months, and I must now revise lots of old code, because it suddenly works differently, (I think,) in this power apps release 3.22042.8

 

I guess this is one of the merits of working with a platform that is in development.   🙂

I'm currently adjusting all the places where this is used, and add a SortByColumn to the galleries and to make sure a sequence number is added to the ForAll collection, so that even when the results are not coming in the desired order I can still SortByColumn into that order.

Br,
Michel

There is also a comment in the documentation about how to impose a sequence on the ForAll()

Use the Sequence function with the ForAll function to iterate based on a count.

But I think the main point is that you can't think about Power Apps like a traditional programming language code generator.  Its much more like Excel. It applies formulas to data, but those formulas aren't in a program that has a sequential order.  Functions in a single event property like an OnSelect will run Synchronously, but you can't use that to change properties of controls at run time.  You have to change a value of something and have the property set to evaluate based on that value. That problem comes from the same declarative nature of the product. 

 

But I agree that its both a blessing and a curse that the whole Power platform is constantly being enhanced. Each change brings new features, but it also has a tendency to break things.



-------------------------------------------------------------------------
If I have answered your question, please mark your post as Solved.
If you like my response, please give it a Thumbs Up.
MichelH
Advocate V
Advocate V

Yes, I fear I don't have that mindset yet. I still expect it to do things when I tell it to. 🙂 And I get annoyed if it doesn't.


Since the app is interacting with the user, I will have to see more clear when it is evaluating things.

 

With some other recent changes I suddenly find power apps starting to evaluate fields, that I have not yet set to their intended values, throwing division by zero and stuff, where I'm sure these fields have correct values, at the time they actually become visible and when they should be evaluated.

Worst is that most of these errors do not even appear in the editor. I often need to run a monitor session with the end user just to see which component is actually throwing that error, before I can try to fix it.

JorisdG
Power Apps
Power Apps

Hi all,

 

We're looking into this issue as we speak. Although the documentation of ForAll calls out the indeterminate order and potentially parallel processing, it was not enabled until recently. We realize that regardless of documentation this may be an unexpected change, so we've decided to roll this back. You can expect some future communication and a plan to handle enabling parallelization and any potential new functions that we guarantee to be executed serially.

MichelH
Advocate V
Advocate V

Hi @JorisdG ,

If I'm the only one to complain there is no longer a need to roll back. I worked around it by adding a sequence number to the source collection.

But having functions that are guaranteed to be sequential sounds great to me.


I've learned yesterday that I can run my code against a newer power apps release, than the one in my production environment. That knowledge would have given me some notice of this upcoming change.

 

I can probably find myself in the excel-like ways where things get evaluated all the time, when I understand exactly how it does that. Right now I feel it is doing it long before my forms and gallery are ready. So I think I need some control there. I tend to think in some form-ready or gallery-ready event or status. 

Br,
Michel

Hi @MichelH ,

I will add a small contribution here, have you tried this ?

ForAll( 
   collection1 As Record1,
   Collect( 
      targetcollection,
      {
         Fld1: Record1.Fld1
         Fld2: Record1.Fld2
         Fld3: 
         LookUp( 
            anothersource, 
            AField=Record1.Fld1  
         ).Fld1
      }
   )
)

I use the With() statement regularly inside ForAll, however the output sequence has never been important to the process (so have never noticed this)

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 (6,661)