cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
dtsiniavskyi
Kudo Collector
Kudo Collector

Increment a field of a record in a collection

When we update a context to increment the value we can do the following: 

 

UpdateContext({ValueToIncrement: ValueToIncrement + 1})

But when I try to Patch() a collection and increment existed value using the next formula: 

 

Patch(CollectionToPatch, LookUp(CollectionToPatch, logical expression), { ValueToIncrement: ValueToIncrement + 1 })

 I get Invalid name error. So I have to do the LookUp() again:

Patch(CollectionToPatch, LookUp(CollectionToPatch, logical expression), { ValueToIncrement: LookUp(CollectionToPatch, logical expression).ValueToIncrement + 1 })

Why can't I use this syntax:

{ValueToIncrement: ValueToIncrement + 1}

to increment a value during Patch(). Is there some other, better way of doing so?

 

Edits (to be more clear about what I want to achieve).

Lets say my TableX looks like:

 

Category Page

C1            5

C2            10

C3            15

 

What I want to do is to increment Page value of C2 category. So table will look like:

 

Category Page

C1            5

C2            11

C3            15

 

To do so I came up with a formula:

Patch(TableX, LookUp(TableX, Category = "C2"), { Page: LookUp(TableX, Category = "C2").Page + 1 })

 As you can see I have to execute LookUp() function twice. What I am asking is: is there some more elegant or efficient way to change the value of a record field to some other value, which depends on the previous value. Increment as an example. I hoped the formula could look like:

Patch(TableX, LookUp(TableX, Category = "C2"), { Page: Page + 1 })

Why can't it be done this way? Or is there another way? Should I use Filter() instead of LookUp()?

1 ACCEPTED SOLUTION

Accepted Solutions
CarlosFigueira
Power Apps
Power Apps

Currently Patch calls only accept values, not expressions, in the update parameter. Please create a new idea at https://aka.ms/powerapps-ideas if you feel that this scenario is important to you.

 

You can avoid multiple LookUp calls by saving it first in a context variable, then using it in the patch call, like in the example below:

UpdateContext({ toUpdate: LookUp(TableX, Category = "C2") }); Patch(TableX, toUpdate, { Page: toUpdate.Page + 1 })

 

View solution in original post

8 REPLIES 8
hpkeong
Community Champion
Community Champion

Hi

 

I try to figure out your question. Let me try to reply here.

If you wish to have increment +1, you may do like this:

Say the TableX looks like this:

ID    Name      Age

1      Tom        35

2      Vicky       27

 

To have increment in ID =3 when patching new record,

OnSelect = Patch(TableX, EditRecord, {ID: If(!IsBlank(ID), EditRecord.ID, Text(Max(TableX, ID) + 1)), ....})

 

Explanation:

1. When you click on the row (of a gallery) and wish to edit and because the ID is NOT Blank, it will still show the existing ID (say ID = 2 if you click on row 2) - this is extracted from EditRecord.ID

2. If you have a button to ADD a new record, say UpdateContext({EditRecord: Defaults(TableX)})...EditRecord is a Context Variable to hold Defaults(TableX)..a new empty row temporarily...then, ID will be BLANK (Defaults(TableX) will automatically append a new row below row 2. As ID is Blank, Max(TableX, ID) will look for maximun value of ID at row ID = 2 and then + 1, to generate 3. and you may continue to include other paramenters to save.

 

Hope this helps. G9

 

hpkeong
mr-dang
Community Champion
Community Champion

It looks like you are Patching an existing record.

 

Patch(CollectionToPatch, 
LookUp(CollectionToPatch, logical expression),
{ ValueToIncrement: ValueToIncrement + 1 })

It might be that you need to convert ValueToIncrement to a Text value. My experience with Patch is that it generally requires text. I don't know how to override it permanently. 

 

Patch(CollectionToPatch, 
LookUp(CollectionToPatch, logical expression), 
{ ValueToIncrement: Text(Value(ValueToIncrement) + 1) })

 

Microsoft Employee
@8bitclassroom

@hpkeong, thanks for the ansver but I've added more details to my original post to make it more clear about what I want to achieve. 

CarlosFigueira
Power Apps
Power Apps

Currently Patch calls only accept values, not expressions, in the update parameter. Please create a new idea at https://aka.ms/powerapps-ideas if you feel that this scenario is important to you.

 

You can avoid multiple LookUp calls by saving it first in a context variable, then using it in the patch call, like in the example below:

UpdateContext({ toUpdate: LookUp(TableX, Category = "C2") }); Patch(TableX, toUpdate, { Page: toUpdate.Page + 1 })

 

View solution in original post

Hi

 

Thanks for entrusting.

Hope the attached file helps. In fact, Dang's idea is the same.

 

You got to have a TextBox to contain the selected Category so that it can be used for increment + 1.

Even + 1 can be chosen from any other list.

 

Hope it meets your intention.

 

Screenshot (140).png

hpkeong

Hi Carlos:

 

As Dang also mentioned, we have to make it:

{Page: Text(toUpdate.Page + 1}.... use Text otherwise it doesn;t work.

 

Hope we are correct!

hpkeong
mr-dang
Community Champion
Community Champion

If you are looking to change an existing record, Update or UpdateIf might be easier to write.

 

UpdateIf(TableX, Category = "C2", { Page: Text(Value(Page) + 1) })

The expression section is shortened because you've already specified which table you're working in.

 

The only catch with UpdateIf is that it will change multiple records. To change just one record, you might want Update instead:

Update(TableX, First(Filter(TableX, Category = "C2")), { Page: Text(Value(Page) + 1) })

This changes the first record in TableX that is part of Category C2.

Microsoft Employee
@8bitclassroom

This answer was really helpful to me. 

I'm using barcode scanned data (Full name) to update a record of in/out for a fire register.

The increment is on a counter. A calculated 'in/out' field refers to that counter.

 

Works beautifully.

Helpful resources

Announcements
PA User Group

Welcome to the User Group Public Preview

Check out new user group experience and if you are a leader please create your group

MBAS Attendee Badge

Claim Your Badge & Digital Swag!

Check out how to claim yours today!

secondImage

Are Your Ready?

Test your skills now with the Cloud Skill Challenge.

Top Solution Authors
Top Kudoed Authors
Users online (39,382)