cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
bolin-stratus
Continued Contributor
Continued Contributor

Merging Two Collections and Retaining ALL Columns?

What is the best method to merge these two collections and retain ALL columns?  The two collections are joined via the deal id which is "Title" in colDashDealIntakeApp and "Deal_x002d_Ref_x002d_ID" in colDashDealUpdates.

ClearCollect(colDashDealIntakeApp, ShowColumns('Deal-Intake-App', "Title", "Close_x002d_Date")); 
ClearCollect(colDashDealUpdates, ShowColumns('Deal-Updates', "Deal_x002d_Ref_x002d_ID", "Pursuit_x002d_Closedown_x002d_Da"));

 

1 ACCEPTED SOLUTION

Accepted Solutions
Drrickryp
Super User
Super User

The principle behind merging the collections is to use the AddColumns() function to create a new column in one of the collections. So starting with the first collection, first you give the new added column a "name", then you use Lookup() on the second collection to match the common fields, and then select the column in the second collection that you want to add to the first collection. The syntax is

AddColumnsTable, "ColumnName1"Formula1 [, "ColumnName2"Formula2, ... ] ) 

where in your case, the table is a collection, the second element is a new name you will give to the added column and the formula is a Lookup(Table2, Deal-ID=Title, PCDDate) which looks up in a second collection for a row that matches the dealID to the title, and then adds the column from the second table. 

It appears that you are using SharePoint and I suggest that you review https://www.practicalpowerapps.com/data/constructing-your-first-power-app-before-you-start/ 

The way you have constructed your lists makes it extremely difficult to work with them.

View solution in original post

6 REPLIES 6
Drrickryp
Super User
Super User

Hi @bolin-stratus 

You could use the AddColumns() function to create the final merged collection

 

ClearCollect(colDashDealIntakeApp, ShowColumns('Deal-Intake-App', "Title", "Close_x002d_Date")); 
ClearCollect(colDashDealUpdates, ShowColumns('Deal-Updates', "Deal_x002d_Ref_x002d_ID", "Pursuit_x002d_Closedown_x002d_Da"));

With({cddia:colDashDealIntakeApp, 
      cddu:colDashDealUpdates,
      dealID:"Deal_x002d_Ref_x002d_ID",
      pursuitclosed:"Pursuit_x002d_Closedown_x002d_Da"},
   ClearCollect(
             colAll,AddColumns(
                                cddia,"DealUpdates", Lookup(
                                                            cddu,dealID = "Title",'Deal-Updates'
                                                      ),
                                       "PursuitCloseDown", Lookup(
                                                                 cddu,"dealID= "Title",pursuitclosed
                                                          )
                      )
     )
)

 

This assumes that the first two collections actually exist. The merged collection would be colAll.  The With() function is used to clean up your collection names and fields.

bolin-stratus
Continued Contributor
Continued Contributor

@Drrickryp - Thanks for the response. Unfortunately, AddColumns has some invalid code.  Any recommendations? 

JOIN 2 Collections.PNG

Drrickryp
Super User
Super User

@bolin-stratus 

My code makes assumptions about your collections. I revised it to try to make it less likely to have typos.  However, could you put a couple of datatables on a screen and set the Items property to the two collections  you are trying to merge so that I can get a better idea about your data?

bolin-stratus
Continued Contributor
Continued Contributor

This what the data looks like.  The bold header text is not the actual col names.  "PCDate" = "Pursuit_x002d_Closedown_x002d_Da".

JOIN 2 Collections 2 tables.PNG

Drrickryp
Super User
Super User

The principle behind merging the collections is to use the AddColumns() function to create a new column in one of the collections. So starting with the first collection, first you give the new added column a "name", then you use Lookup() on the second collection to match the common fields, and then select the column in the second collection that you want to add to the first collection. The syntax is

AddColumnsTable, "ColumnName1"Formula1 [, "ColumnName2"Formula2, ... ] ) 

where in your case, the table is a collection, the second element is a new name you will give to the added column and the formula is a Lookup(Table2, Deal-ID=Title, PCDDate) which looks up in a second collection for a row that matches the dealID to the title, and then adds the column from the second table. 

It appears that you are using SharePoint and I suggest that you review https://www.practicalpowerapps.com/data/constructing-your-first-power-app-before-you-start/ 

The way you have constructed your lists makes it extremely difficult to work with them.

bolin-stratus
Continued Contributor
Continued Contributor

@Drrickryp  - Thanks for the great explanation of merging collections! I agree about my mistake with the column names. 

It looks like you edited your original proposed solution so I ran the updated version.  I am getting error "AddColumns has some invalid arguments".  It looks like you are using aliases to make references easier.  Any recommendations on how to fix this are greatly appreciated.  

JOIN 2 Collections II.PNG
 
 

Helpful resources

Announcements
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

May UG Leader Call Carousel 768x460.png

June User Group Leader Call

Join us on June 28 for our monthly User Group leader call!

PA Virtual Workshop Carousel 768x460.png

Register for a Free Workshop

This training provides practical hands-on experience in creating Power Apps solutions in a full-day of instructor-led App creation workshop.

PA.JPG

New Release Planning Portal (Preview)

Check out our new release planning portal, an interactive way to plan and prepare for upcoming features in Power Platform.

Top Solution Authors
Users online (1,285)