I have a collection I am actioning on an item. I would like to patch the record, then recall the value, then patch to another column based on that data. Can I do this all in one go?
With({SNListTmp: Match(Concat(Sequence(ThisItem.QtyOrdered,Value(LastSN_CloudDB_1.Text)+1) As Range,Range.Value & ","), "^(?<trim>.*),$" ).trim},
Patch(Assign_SN_List,ThisItem,{
SNListItem:SNListTmp,
SNListLast:Value(Last(Split(SNListTmp,",")).Result),
SNValveTmp:"Test"
})
);
Clear(varTestList4);
ForAll(Split(ThisItem.SNListItem,",").Result As varStringList,Collect(varTestList4,Value(varStringList.Result)));
Clear(colTestList);
ForAll(
Sort(varTestList4,Value,Ascending) As Count,
If(
CountRows(colTestList)= 0 , Collect( colTestList, { FirstCol: First(varTestList4).Value, SecondCol: First(varTestList4).Value}),
Count.Value = Last( colTestList).SecondCol+1,
Update(colTestList, Last(colTestList), {FirstCol: Last(colTestList).FirstCol ,SecondCol: Count.Value}),
Collect(colTestList, { FirstCol: Count.Value, SecondCol: Count.Value}
)
)
);
Patch(Assign_SN_List,ThisItem,{
SNValveTmp:
Match(Concat( colTestList,
FirstCol &
If( FirstCol = SecondCol, "," ,"-" & SecondCol & ",")
), "^(?<trim>.*),$" ).trim
})
In my sample, "Test" writes to SNValveTmp in the first statement, but does not get updated in the last patch statement.
Solved! Go to Solution.
Well, disregard, I just nested in with another With() statement, then did one patch at the end.
With({SNListTmp: Match(Concat(Sequence(ThisItem.QtyOrdered,Value(LastSN_CloudDB_1.Text)+1) As Range,Range.Value & ","), "^(?<trim>.*),$" ).trim},
Clear(varTestList4);
ForAll(Split(SNListTmp,",").Result As varStringList,Collect(varTestList4,Value(varStringList.Result)));
Clear(colTestList);
ForAll(
Sort(varTestList4,Value,Ascending) As Count,
If(
CountRows(colTestList)= 0 , Collect( colTestList, { FirstCol: First(varTestList4).Value, SecondCol: First(varTestList4).Value}),
Count.Value = Last( colTestList).SecondCol+1,
Update(colTestList, Last(colTestList), {FirstCol: Last(colTestList).FirstCol ,SecondCol: Count.Value}),
Collect(colTestList, { FirstCol: Count.Value, SecondCol: Count.Value}
)
)
);
Patch(Assign_SN_List,ThisItem,{
SNListItem:SNListTmp,
SNListLast:Value(Last(Split(SNListTmp,",")).Result),
SNValveTmp:
Match(Concat( colTestList,
FirstCol &
If( FirstCol = SecondCol, "," ,"-" & SecondCol & ",")
), "^(?<trim>.*),$" ).trim
}))
Well, disregard, I just nested in with another With() statement, then did one patch at the end.
With({SNListTmp: Match(Concat(Sequence(ThisItem.QtyOrdered,Value(LastSN_CloudDB_1.Text)+1) As Range,Range.Value & ","), "^(?<trim>.*),$" ).trim},
Clear(varTestList4);
ForAll(Split(SNListTmp,",").Result As varStringList,Collect(varTestList4,Value(varStringList.Result)));
Clear(colTestList);
ForAll(
Sort(varTestList4,Value,Ascending) As Count,
If(
CountRows(colTestList)= 0 , Collect( colTestList, { FirstCol: First(varTestList4).Value, SecondCol: First(varTestList4).Value}),
Count.Value = Last( colTestList).SecondCol+1,
Update(colTestList, Last(colTestList), {FirstCol: Last(colTestList).FirstCol ,SecondCol: Count.Value}),
Collect(colTestList, { FirstCol: Count.Value, SecondCol: Count.Value}
)
)
);
Patch(Assign_SN_List,ThisItem,{
SNListItem:SNListTmp,
SNListLast:Value(Last(Split(SNListTmp,",")).Result),
SNValveTmp:
Match(Concat( colTestList,
FirstCol &
If( FirstCol = SecondCol, "," ,"-" & SecondCol & ",")
), "^(?<trim>.*),$" ).trim
}))
Yes, you can perform many function in one formula, but in review of your current formula, I am not sure that it is necessary to make multiple datasource calls/patches as you can do it all in one step.
I am a little confused on what you are trying to accomplish in the colTestList ForAll part (which is backward - ForAll is a function that returns a table. It is not a ForLoop like in development). There is certainly no need to create all the collections and then have all that logic from what I can tell, but I am not sure your designated purpose of that. Perhaps an example of what you are getting in the SNListTmp (or what you are trying to do in that).
You must have responded while I was responding!
Your ForAll's are still backward. This is a bad habit in PowerApps as it leads to performance issues.
Glad you have something working though now.
I'm curious on your statement on the forall() being backwards. What do you suggest with this?
ForAll(Split(SNListTmp,",").Result As varStringList,Collect(varTestList4,Value(varStringList.Result)))
Here is the ther forall() with the remarks. This one is doing something a bit complex. It is taking an array of numbers, and reformatting. input is [4,1,2,5,3,10,12,13] Output is "1-5,10,12-13" Its a little hard to explain, but the columns manage groupings of consecutive vs non-consecutive numbers. Then the final concat() statement pulls it all together into the final string.
ForAll(
// sorting the list of values makes sure you can add more values in any order without error
Sort(varTestList4,Value,Ascending) As Count,
// build collection of first and second values
If(
// create first row in collection
CountRows(colTestList)= 0 , Collect( colTestList, { FirstCol: First(varTestList4).Value, SecondCol: First(varTestList4).Value}),
// update collection rows or add new ones as required
Count.Value = Last( colTestList).SecondCol+1,
Update(colTestList, Last(colTestList), {FirstCol: Last(colTestList).FirstCol ,SecondCol: Count.Value}),
Collect(colTestList, { FirstCol: Count.Value, SecondCol: Count.Value}
)
)
);
Match(Concat( colTestList,
FirstCol &
If( FirstCol = SecondCol, "," ,"-" & SecondCol & ",")
), "^(?<trim>.*),$" ).trim
ForAll is a function that returns a table. Using it like a For/Loop, while it might work (depending on what you are doing), it impacts performance.
For your formula:
ForAll(Split(SNListTmp,",").Result As varStringList,Collect(varTestList4,Value(varStringList.Result)))
You are instantiating a Collect function on each iteration of the source table - which is the table produced from the Split function. Ultimately, the collection would have a table in it. But, in the sense of what it is doing...let's look at it like gathering records from a datasource. Let's say you have records in a datasource with ID's and you want the first 5.
You would perform (normally - if you wanted them in some collection), the following formula:
Collect(col, Filter(yourData, ID<=5))
Filter gets all the records and returns a Table. This table is then given to the collection (because a collection is a table). One step...all done.
But, the following would work too:
ForAll(Sequence(5), Collect(col, LookUp(yourData, ID=Value)))
Ignoring the actual LookUp performance, you are adding a record one-by-one.
However, using the ForAll properly (and again, ignoring the performance of the LookUp for this example), the formula would be:
Collect(col, ForAll(Sequence(5), LookUp(yourData, ID=Value)))
In this case, the ForAll (as mentioned) returns a table, so the Collect function is only instantiated once and have (just like in the Filter case) the resultant table to collect.
As for the second part, your not so much working with an array as much as a string. But, I'm not seeing how you would get 4,1,2,5,3,10,12,13 from your initial With statement. It would be sequential.
So can you explain how you get that sequence/array?
This training provides practical hands-on experience in creating Power Apps solutions in a full-day of instructor-led App creation workshop.
Come together to explore latest innovations in code and application development—and gain insights from experts from around the world.
User | Count |
---|---|
195 | |
67 | |
46 | |
41 | |
28 |
User | Count |
---|---|
255 | |
119 | |
86 | |
84 | |
83 |