How do I calculate values for a field by concatenating two fields and having each combination have its independent incrementing number

1241
5
07-18-2023 11:39 AM
Labels (2)
AydanScottDSDP
New Contributor II

 

I would like to be able to calculate a text field for use as a unique ID based on two other fields for a feature. In my current case, I am creating a Light Post map and want the ID to be based on the field that says what neighborhood it is in, and the field that says what type of light post it is. 

So it would look something like this:

Neighborhood = CC, CL, CR, EV, MA, or GQ

Post Type = G, M, O, or T

So the result should look like this

CC-G-1

CC-G-2

CC-M-1

CC-G-3

CL-M-1

CL-G-1

CL-M-2

And so on

Any help would be appreciated

5 Replies
Robert_LeClair
Esri Notable Contributor

@AydanScottDSDP  - so I've been messing around with some Python code blocks in the Field Calculator and I'm getting close.  The only thing not working is the sequential numeric values for each Neighborhood/PostType combo if you don't select any features.  Instead of CC-G-1 and CC-G-2, it just goes 1-N for all Neighborhood values.  I think if you selected features based upon neighborhood, THEN run the Field Calculation GP tool, it would work fine/as expected.

The key to the field calculator is the sample scripts that come with the ArcGIS Pro help - Calculate Field Python examples—ArcGIS Pro | Documentation - I used the Accumulative and sequential calculations.

See my image below:

IncrementValues.JPG

AydanScottDSDP
New Contributor II

@Robert_LeClair  - That's about as far as I've been able to get as well. I have also been playing around with various ideas. I'm sure there is a way, but I have yet to find it.

0 Kudos
jcarlson
MVP Esteemed Contributor

It would likely run extremely slow over a network, but with a file-based layer would be okay. Basically you get the prefix minus the ID number and use that to filter the layer.

Get the count of matching features, add 1, and you've got your number. Just put it on the end of the prefix, and you've got your unique ID.

var id_pre = `${$feature['neighborhood']}-${$feature['post_type']}-`

// get other features w/ same prefix
var same_pre = Filter($layer, `new_id_field like '${id_pre}%'`)

// see how many there are
var same_pre_count = Count(same_pre)

// your current feature will be whatever that value is + 1
return `${id_pre}${same_pre_count + 1}`

 

- Josh Carlson
Kendall County GIS
AydanScottDSDP
New Contributor II

@jcarlson I have been having issues with line 3 (or 4 on your post) so far, I'm probably missing something obvious but it says "Object not found $layer"

Any idea why this would be?

If not, I'll post another question.

0 Kudos
jcarlson
MVP Esteemed Contributor

Agh, Arcade doesn't always play nice with Pro the way it does in other contexts. You need to pull in the layer somehow. Check the "helper functions" list in the expression builder, there might be something in there. If the layers are in the same data source, you can try using $datastore instead of $layer. Or you may have to use FeatureSetByPortalItem.

- Josh Carlson
Kendall County GIS
0 Kudos