cancel
Showing results for
Did you mean:
Helper II

Update a column in a collection based on the value in that column

Hi

I have a collection as follows:

 Name Quantity Ed 8 Tom 3 Alan 1

I need to update these records based on new records in a specified order:

 Name Quantity Ed -3 John 13

So my final collection will be:

 Name Quantity Ed 5 Tom 3 Alan 1 John 13

I'm looping through each new row in order using a ForAll.  I don't know how to update the Quantity based on the existing Quantity, something like:

Patch(

Rows,

LookUp(

Rows,

Rows[@Name]=NewRow[@Name]

),

{

Quantity=Quantity+NewRow[@Quantity]

}

)

Any help would be appreciated.  Thanks.

1 ACCEPTED SOLUTION

Accepted Solutions
Super User

@edhalsim Absolutely simple!

Just have to see it from inside out.

1. Combining the two collections, Collect(col1, col2)
2. Grouping them on the Name as its the Key. Call this Group as "Grouped Data". The result of this will have the "Name" Column and a "Grouped Data" Column. This "Grouped Data" Column will have the Quantity for each person (E.g. it will have 2 records for Ed, 8 & -3)
3. Add a Column called Quantity, and set its value as the Sum of the Quantity in the "Grouped Data" (E.g. for Ed, it will be Sum(8, -3) and the result is 5).
4. Drop the "Grouped Data" Column as we don't need it any more.
5. Store the results in another collection.
6. Time for an Ice Cream!

Please remember to give a 👍 and accept my solution as it will help others in the future.

3 REPLIES 3
Super User

Please try the following (replace collection names as applicable),

``ClearCollect(colCombined, DropColumns(AddColumns(GroupBy(Collect(col1, col2), "Name", "GroupedData"), "Quantity", Sum(GroupedData, Quantity)), "GroupedData"));``

Please remember to give a 👍 and accept my solution as it will help others in the future.

Helper II

Wow.  This is beyond my current level of understanding.  Would you please explain what you're doing?

Super User

@edhalsim Absolutely simple!

Just have to see it from inside out.

1. Combining the two collections, Collect(col1, col2)
2. Grouping them on the Name as its the Key. Call this Group as "Grouped Data". The result of this will have the "Name" Column and a "Grouped Data" Column. This "Grouped Data" Column will have the Quantity for each person (E.g. it will have 2 records for Ed, 8 & -3)
3. Add a Column called Quantity, and set its value as the Sum of the Quantity in the "Grouped Data" (E.g. for Ed, it will be Sum(8, -3) and the result is 5).
4. Drop the "Grouped Data" Column as we don't need it any more.
5. Store the results in another collection.
6. Time for an Ice Cream!

Please remember to give a 👍 and accept my solution as it will help others in the future.

Announcements

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.

Microsoft Build is May 24-26. Have you registered yet?

Come together to explore latest innovations in code and application development—and gain insights from experts from around the world.

What difference can a User Group make for you?

At the monthly call, connect with other leaders and find out how community makes your experience even better.

Top Solution Authors
Top Kudoed Authors
Users online (2,813)