Hello Friends,
Welcome back with another post on PowerApps. Sometimes, we have requirement where we have data in 2 or more collections. Now, we have to club all these collections and get only single collection so that the same can be displayed in grid or can perform any further action. This can be done simply using ClearCollect-
- ClearCollect(MergedCollection, Collection1,Collection2);
- Let's we have a SharePoint list having records.
- The same is fetched in PowerApps.
ClearCollect( DataFromExcelSelectedColumns, ShowColumns( DataFromExcel, "FirstName", "LastName", "FullName" ) );
- We have fetched only 3 columns (FirstName, LastName, FullName) and store the data in collection named "DataFromExcelSelectedColumns".
- Now we will add a column named "RowNumber" which will assign the items a sequential number. The new item collection is stored in a new collection named "DataFromExcelSelectedColumnsSequence". This collection will be used for merging.
ForAll( DataFromExcelSelectedColumns, Collect( DataFromExcelSelectedColumnsSequence, Last( FirstN( AddColumns( DataFromExcelSelectedColumns, "RowNumber", CountRows(DataFromExcelSelectedColumnsSequence) + 1 ), CountRows(DataFromExcelSelectedColumnsSequence) + 1 ) ) ) );
- The output of this collection is-
- As we can see, a new column "RowNumber" is added with a sequence number starting from 1 onwards.
- Now suppose, we have another data collection (may be locally generated or data received from Power Automate) named "TempDataColl". This collection is also having 3 columns (FirstName, LastName, FullName). Let it be as-
ClearCollect( TempDataColl, { FirstName: "Praveen", LastName: "Chinta", FullName: "Praveen Chinta" }, { FirstName: "Rajeev", LastName: "Bhuvan", FullName: "Rajeev Bhuvan" }, { FirstName: "Suyash", LastName: "Bharti", FullName: "Suyash Bharti" } );
- The collection thus created is-
- Similar way, we will add the "RowNumber" column to this collection also and save it in new collection named "DataFromExcelSelectedColumnsSequence2". The point to remember is that, the sequence number should start from the next sequence of the previous collection (DataFromExcelSelectedColumnsSequence) sequence number. In our case, it should start from 8 as the previous collection was having 7 records. For this, we will use below function-
ForAll( TempDataColl, Collect( DataFromExcelSelectedColumnsSequence2, Last( FirstN( AddColumns( TempDataColl, "RowNumber", CountRows(DataFromExcelSelectedColumnsSequence) + 1 + CountRows(DataFromExcelSelectedColumnsSequence2) ), CountRows(DataFromExcelSelectedColumnsSequence2) + 1 ) ) ) );
- As you can see, while defining the sequence number, we have added the rows count of previous collection.
- This way, we got our second collection. Now we will simply merge both collections into one-
ClearCollect(FinalCollection,DataFromExcelSelectedColumnsSequence,DataFromExcelSelectedColumnsSequence2);
- The collection "FinalCollection" will be having items of both collections.
- At last, we will clear all the intermediate collections-
ClearCollect( DataFromExcelSelectedColumnsSequence, [] ); ClearCollect( DataFromExcelSelectedColumnsSequence2, [] ); ClearCollect( TempDataColl, [] );
- This way, we can achieve the desired functionality.
With this, I am concluding this post.
Happy Coding !!!
Will see you again with some new topics.
Stay Safe !
Stay Healthy !
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.