cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
hnguy71
Level 8

Merge Two Collections with unique key

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?

10 REPLIES 10
Super User
Super User

Re: Merge Two Collections with unique key

@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.

 

 

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up. Solved your problem? - Click on Accept as Solution. Others seeking the same answers will be happy you did.
hnguy71
Level 8

Re: Merge Two Collections with unique key

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?

Super User
Super User

Re: Merge Two Collections with unique key

@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.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up. Solved your problem? - Click on Accept as Solution. Others seeking the same answers will be happy you did.
Community Support Team
Community Support Team

Re: Merge Two Collections with unique key

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,

Community Support Team _ Kris Dai
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
hnguy71
Level 8

Re: Merge Two Collections with unique key

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! 

Super User
Super User

Re: Merge Two Collections with unique key

@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.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up. Solved your problem? - Click on Accept as Solution. Others seeking the same answers will be happy you did.
hnguy71
Level 8

Re: Merge Two Collections with unique key

@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

Super User
Super User

Re: Merge Two Collections with unique key

@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.

_____________________________________________________________________________________
Digging it? - Click on the Thumbs Up. Solved your problem? - Click on Accept as Solution. Others seeking the same answers will be happy you did.
hnguy71
Level 8

Re: Merge Two Collections with unique key

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-ForAl...

Helpful resources

Announcements
thirdimage

Power Automate Community User Group Member Badge

Fill out a quick form to claim your user group badge now!

sixthImage

Power Platform World Tour

Find out where you can attend!

Power Platform 2019 release wave 2 plan

Power Platform 2019 release wave 2 plan

Features releasing from October 2019 through March 2020

fifthimage

Microsoft Learn

Learn how to build the business apps that you need.

Top Kudoed Authors
Users Online
Currently online: 212 members 5,299 guests
Please welcome our newest community members: