After using PowerApps for the last 6 months with varying degrees of success, the one thing I still dont exactly understand is Collections. What would be a good reason to use a collection as opposed to simply Patching the data? Is it more for storing temporary data? Can anyone here cite a good use case with Collections? Thanks.
Brendon
Hi,
I use collections for many things, but one usage I think that may be useful to you is using collections to store the changes you want to Patch back to the datasource.
Sometimes you want your changes saved immediately because the data is sensitive and you don't want things to go wrong. But saving takes time and can interrupt your user if they click too many Patches. So instead of saving one by one, you can save a bulk set of changes/additions to the data source using a collection and the ForAll() function.
Collect your changes somewhere: Collect(changes,ThisItem) Commit your changes in bulk when you are ready: ForAll(RenameColumns(changes,"RecordId","RID"), If(!IsEmpty(Filter(datasource,RecordId=RID)), Patch(datasource,First(Filter(datasource,RecordId=RID)), {field: data to change } ), Patch(datasource,Defaults(datasource) {field: new data } ) ) )
The second formula means, "For each of the records you collected for changing, check if it is already in the datasource. If it's already in there, update the data accordingly, otherwise, write a new record."
User | Count |
---|---|
183 | |
123 | |
88 | |
45 | |
42 |
User | Count |
---|---|
251 | |
160 | |
126 | |
78 | |
73 |