web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Apps / Merge Two Collections ...
Power Apps
Unanswered

Merge Two Collections with unique key

(0) ShareShare
ReportReport
Posted on by 347

Good morning, All.

 

I have two collections. Let's call one a fact collection and the other a dim collection. The fact collection is my main SharePoint list while the secondary list is a dimension table containing user information. Both collections have a matching key combination. So I'm trying to do merge to get my dim collection into my fact collection via unique key. 

 

fact collection (SP_List): UniqueKey Field m_Key:

fact_List.png

dim collection (UserList): UniqueKey Field u_Key:
dim_List.png

My test to adding a new column to my fact collection doesn't give me an error but also does not add a new column for me:

ClearCollect(
 CurrentUser,
 User()
);
ClearCollect(
 UserList,
 BSN_FAL_UserList
);
ClearCollect(
 SP_List,
 BSN_FAL_PowerApps
);
AddColumns(
 SP_List,
 "New Column",
 LookUp(
 UserList,
 u_Key = m_Key
 ).Reviewer
)

Should I be using a ForAll loop or is using Addcolumns and Lookup is more than enough to do the job?

Categories:
I have the same question (0)
  • RandyHayes Profile Picture
    76,297 Super User 2024 Season 1 on at

    @hnguy71 

    Your formula for adding columns will do the trick.  However, in your formula, your're adding the column but not doing anything with it.

    AddColumns returns a table with the added column, it does not modify the exiting table that you supply it.

    So, consider the following formula instead:

    ClearCollect(CurrentUser,User());
    ClearCollect(
     UserList,
     BSN_FAL_UserList
    );
    ClearCollect(
     SP_List,
     BSN_FAL_PowerApps
    );
    ClearCollect(mergedList, AddColumns( SP_List, "New Column", LookUp( UserList, u_Key = m_Key ).Reviewer )
    )

    This will add the "New Column" to the table of SP_List and will then return that as a new table.  In this case we are collecting it into a mergedList collection.

     

    Also, typically you would use a Set to a variable for the User() rather than a collect.  A collection is rows of records.  The User() function returns just a record, so no need to turn it into a table.  So, instead use Set(CurrentUser, User())

    Also you might want to consider (unless you have a specific reason for having them) skipping the first two collections. Your formula would look like this:

    Set(CurrentUser,User()); 

    ClearCollect(mergedList,
    AddColumns( BSN_FAL_PowerApps,
    "New Column", LookUp(BSN_FAL_UserList, u_Key = m_Key ).Reviewer) )

    NOW...one more thing....

    If you actually want more from the UserList in your added columns, you could either put more "new columns" in and more lookup statements to the UserList, but, if you have a need for the entire record or a subset of it, you can specify it all in the formula:

    Set(CurrentUser,User()); 
    
    ClearCollect(mergedList, 
     AddColumns( BSN_FAL_PowerApps, 
     "UserRecord", LookUp(BSN_FAL_UserList, u_Key = m_Key )
    )
    )

    In this example, the mergedList.UserRecord would be the entire looked up record.  So, you can access any information in it.  For example First(mergedList).UserRecord.Reviewer would give you the reviewer information of the first record and First(mergedList).UserRecord.Hub would give you the Hub field information for the first record. etc...

     

    I hope this is clear and helpful for you.

     

     

  • hnguy71 Profile Picture
    347 on at

    Hi @RandyHayes 

     

    The adjusted formula works beatufiully to adding that one column that I needed. However, like you said, I actually do want the rest of the columns merged into my first collection but when I tried the formula for that I'm having a delegation issue.

     

    delegation.pngBut that's not really a big issue. I can add more "columns" in using the other method, but do you know what the issue may be?

  • RandyHayes Profile Picture
    76,297 Super User 2024 Season 1 on at

    @hnguy71 

    Yes, like in my other post to you about delgation...if it will not be over 5000 records that we are dealing with, then you can go on from there.

    If it is...then let's go further on it.

  • v-xida-msft Profile Picture
    on at

    Hi @hnguy71 ,

    Based on the issue that you mentioned, I think you have faced a Delegation warning issue with your formula.

     

    I have made a test on my side, please consider modify your formula as below:

    ClearCollect(
     CurrentUser,
     User()
    );
    ClearCollect(
     UserList,
     BSN_FAL_UserList
    );
    ClearCollect(
     SP_List,
     BSN_FAL_PowerApps
    );
    ClearCollect(
    mergeList,
    AddColumns( SP_List, "UserRecord", LookUp(UserList, u_Key = m_Key) )
    )

    After that, you could use the mergeList collection within your app.

    Note: The Delegation warning issue is not an error, it just tell that your formula data process could not be delegated to your SP list, you could only process data locally. In default, you could could only process 500 records at most locally, you could change the limit to the maximum value -- 2000. If the amount of your SP list records is not more than 2000, you could ignore this warning issue.

    More details about Delegation in PowerApps, please check the following article:

    https://docs.microsoft.com/en-us/powerapps/maker/canvas-apps/delegation-overview

     

    In addition, you could also take a try with the following formula:

    ClearCollect(
     CurrentUser,
     User()
    );
    ClearCollect(
     UserList,
     BSN_FAL_UserList
    );
    ClearCollect(
     SP_List,
     BSN_FAL_PowerApps
    );
    ClearCollect(
    mergeList,
    AddColumns( SP_List, "NewColumn1", LookUp(UserList, u_Key = m_Key).Reviewer,
    "NewColumn2",
    LookUp(UserList, u_Key = m_Key).Area,
    ... )

    Please take a try with above solution, then check if the issue is solved.

     

    Best regards,

  • hnguy71 Profile Picture
    347 on at

    So weird that @RandyHayes and @v-xida-msft's solution looks exactly alike except Kris' solution utilizes the newly created collection whereas Randy's formula uses the original datasource. Is there an explanation for this?

    weird.png

    Using Kris Dais' formula everything looks good. And also, thanks for the detailed clarification regarding delegation! 

  • RandyHayes Profile Picture
    76,297 Super User 2024 Season 1 on at

    @hnguy71 

    Yes, the two are identical (interesting) to the first formula in my original reply.  After that I took you on a different journey through some options. 

    The final option was to drop the two additional collections and go direct to the data source.

    Delegation warnings will never appear as a problem for local collections as they don't have delegation issues.

    In the final example formula I gave you, we cut the collections out of the mix and went direct to the source.  I have mentioned:

    Also you might want to consider (unless you have a specific reason for having them) skipping the first two collections. Your formula would look like this:

    And then, since we're going to the source, the delegation warning will occur.

     

    HOWEVER...don't be fooled by this.  Just because you don't see a delegation warning, doesn't mean you're not going to run into issues with the collection method.

    If your source has more than 5000 records (or your current app limit) and you use my formula direct to the source - you will see that there might be an issue and you can correct for it.

    Using the other formula duplicated, will create two collections.  They will not warn about delegation, but they will only contain the maximum number of rows possible.  If your source has more, they will not have them.  Then...when you get to the formula where you are working against the collections, you will not see the warning - BUT, you will not have accurate results.

    SO...just saying...keep this in mind with formulas and delegation.

  • hnguy71 Profile Picture
    347 on at

    @RandyHayes 
    hmm.... Thanks for explanation. It does make sense, but I need a bit more clarification. Let's say I have a PowerBI report and use the PowerApps integration. I'm expecting to pull in an entire table which, as mentioned yesterday, will contain a minimum of 10,000 records. Are you saying even if there's no delegation issue visible that my collection will still only do a maximum of 2,000 rows? I actually haven't tested with the real production data yet but I was assuming that I can read in an entire table from PowerBI

  • RandyHayes Profile Picture
    76,297 Super User 2024 Season 1 on at

    @hnguy71 

    Your collections are not limited in size other than memory (and I think there is a theoretical maximum...but not sure - seems people hit out of memory before they hit that limit).

    BUT, in your formula, you are doing:

       ClearCollect(SP_List, BSN_FAL_PowerApps)

    This will only contain the app limit amount of rows.  If you've bumped up the maximum limit to 5000, then it will only have 5000 in it.

     

    This doesn't mean that you can't add another 5000 or 10000 or whatever to it, you just will not be able to do it all in one call like above.

  • hnguy71 Profile Picture
    347 on at

    Oh yes. That's why I'm building this app in pieces. If I'm to take anything from SP, CDS, or any other source I want to be ready. Here's the solution for limitation that I had come up with last week:

     

    https://powerusers.microsoft.com/t5/General-Discussion/Collect-All-SharePoint-List-Items-Using-ForAll-Loop/m-p/279866/highlight/true#M81322

  • RandyHayes Profile Picture
    76,297 Super User 2024 Season 1 on at

    @hnguy71 

    Ah yes...always good to plan when you know your data sources will be large.  There are a lot of varieties of solutions for gathering all the data in order to escape delegation limits.  The key is that the data needs to be able to accommodate what you are doing properly.  There's no "one size fits all" solution at the moment.

    But, sounds like you have a plan!

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

Forum hierarchy changes are complete!

In our never-ending quest to improve we are simplifying the forum hierarchy…

Ajay Kumar Gannamaneni – Community Spotlight

We are honored to recognize Ajay Kumar Gannamaneni as our Community Spotlight for December…

Leaderboard > Power Apps

#1
WarrenBelz Profile Picture

WarrenBelz 717 Most Valuable Professional

#2
Michael E. Gernaey Profile Picture

Michael E. Gernaey 329 Super User 2025 Season 2

#3
Power Platform 1919 Profile Picture

Power Platform 1919 268

Last 30 days Overall leaderboard