Select to view content in your preferred language

How can I calculate a "moving" conditional evaluation in Field Calculator?

678
2
04-11-2019 02:20 AM
Labels (1)
Stefande_Bruin1
New Contributor

Hi

I was wondering if anyone has ever tried doing this kind of calculation in field calculator, or if it is even possible. What I am trying to do is to calculate an order field, based on repeating polyline IDs. In excel I would do something like "If ID2 = ID1, Order1 + 1, else 1. In essence, the query refers to another column and to itself, and it refers to specific rows, sometimes other than the row being calculated. Is this doable with a VB or Python script?

Best regards

Stefan

Tags (1)
0 Kudos
2 Replies
DanPatterson_Retired
MVP Emeritus

Sort of, but for text fields.

Table Tools for Pro  on the Code Sharing site.

There are a number of tools there.  Some are documented in a blog I wrote

/blogs/dan_patterson/2018/02/12/table-tools-a-set-of-tools-for-working-with-tabular-data 

If you are code adept, you could modify to your specific needs, the above example was used for object subclassing and sequencing

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

The following should work in Pro, not sure about ArcMap since it uses Python 2.x.

Code Block:

from collections import defaultdict

def counter_gen():
    counter = defaultdict(int)
    v = None
    while True:
        counter[v] += 1
        v = yield counter[v] if v else None
        
g = counter_gen()
next(g)

and Expression:

g.send(!ID!)

The code above works whether the table is sorted by ID groups or not.  If the table is sorted by some other column, the code just takes the first ID value x and makes it 1, the second ID value x as 2, etc.... as it finds them.

0 Kudos