Using OR statement i Field Calculator

198
5
Jump to solution
04-08-2021 11:10 AM
GjermundDeggerdal
New Contributor II

I am using ArcMAP 10.8 and i am trying to add a new field to a table. So far so good..

In this new field i want a  number between 1 and 5. The number is decided based on what is written in three other fields in the table ([MATERIAL], [YEAR] and [DIM]) So therefore i am using the Field Calculator. My problem is that i have never gotten the OR statement to work without writing the whole code again.

Therefore I have to write it like this to work:

dim n
if [DIM]=< 100 and [YEAR] =< 1959 and [MATERIAL] = "SJG" or  [DIM]=< 100 and [YEAR] =< 1959 and [MATERIAL] = "SJ" then
n = 5

 

But, I want to write it like this (or something similar);

dim n
if [DIM]=< 100 and [YEAR] =< 1959 and [MATERIAL] = "SJG", "SJ" then
n = 5

 

I would really appriciate if there is some solution to this problem 😉 

0 Kudos
2 Solutions

Accepted Solutions
Jelle_Stuurman
Occasional Contributor

I would use a python a script by setting the expression type to python.

Expression:

 

reclass(!DIM!,!YEAR!,!MATERIAL!):

 

code block:

 

def reclass(dim, year, material):
    if dim <= 100 and year <= 1959 and material in ("SJG", "SJ"):
        return 5

 

View solution in original post

JayantaPoddar
MVP Esteemed Contributor

In Pre-logic Script, it's just a variable name used in the function. In the expression those variable named get replaced by actual Field Names.

In Expression, delete the fieldnames, and add them again by double-clicking the appropriate items in the Fields list, separated by comma.

Also you won't get any result, if the logic is not true for any record.

View solution in original post

5 Replies
Jelle_Stuurman
Occasional Contributor

I would use a python a script by setting the expression type to python.

Expression:

 

reclass(!DIM!,!YEAR!,!MATERIAL!):

 

code block:

 

def reclass(dim, year, material):
    if dim <= 100 and year <= 1959 and material in ("SJG", "SJ"):
        return 5

 

View solution in original post

JayantaPoddar
MVP Esteemed Contributor

This has also worked for me (Parser: Python).

Code Block:

 

 

def reclass(dim,year,material):
    if dim<=100 and year<=1959 and material in ("SJG","SJ"):
        return 5

 

 

Expression:

 

 

reclass( !DIM!, !YEAR!, !MATERIAL! )

 

 

 

GjermundDeggerdal
New Contributor II

Thank you very much for your helpfull respons! I am truly sorry if my follow-up questions are stupid and basic, but I`m stuck.

I have probably done something wrong, because I can`t get this command to work.  I wrote it in like this (Copy - paste from your post). Is there something more I have to add to the statement?

GjermundDeggerdal_0-1617951716737.png

And I dont understand why you don`t need exclamation marks around the variable inside the pre logic script, this seems diffrent then in VB Script?

 

0 Kudos
JayantaPoddar
MVP Esteemed Contributor

In Pre-logic Script, it's just a variable name used in the function. In the expression those variable named get replaced by actual Field Names.

In Expression, delete the fieldnames, and add them again by double-clicking the appropriate items in the Fields list, separated by comma.

Also you won't get any result, if the logic is not true for any record.

View solution in original post

GjermundDeggerdal
New Contributor II

Thanks, Your code worked perfect with copy-paste, the problem was that i had empty values "Null" and shape did not allow it. I converted it to gdb and it worked right away! Cheers!

 

0 Kudos