# Calculate field by comparing a field value to two other fields as min and max

Using ArcMap 10.5, I'm trying to compare recommended fire regimes for different veg types (min/ max fire frequency) with actual burns from the past 10 years to see whether they are in or out of regime.... but I'm stumped on how to achieve this.

I have 2 layers:

• 'Since burnt' with attribute 'no. of years since burnt'
• 'Veg groups' with attributes 'veg type' (3 types), 'min freq' and 'max freq'

First, I spatial joined the 2 layers using 'Since burnt' as the target so now I have a table with the required fields (no of years since burnt, min freq and max freq) to do the calculation. My idea was to then add a field and populate it with 'over', 'under' or 'within' according to where the 'since burnt' value fits in the min/ max range.

With my limited knowledge of python, I came up with:

``````def calc(y1,y2,y3):   #where y1=yrs since burnt, y2=max, y3=min
if y1 > y2:
return "OVER"
elif y1 < y3:
return "UNDER"
else:
return "WITHIN"
``````

Of course this doesn't account for equal to, which I couldn't get to work. And I assume there's a much better way of using y2, y3 as a range.

Since you want to compare the dates of Year-1 with the maximum Year-2 and minimum Year-3, you need to add one more condition which is:

``````elif y1 <= y2 and y1 >= y3:
return "WITHIN"
``````

In Field calculator it will like this:

``````def calc(y1,y2,y3):   #where y1=yrs since burnt, y2=max, y3=min
if y1 > y2:
return "OVER"
elif y1 < y3:
return "UNDER"
elif y1 <= y2 and y1 >= y3:
return "WITHIN"
``````

When executing the above function, you need to put:

``````calc(!Year1!,!Year2!,!Year3!)
``````

Applied the above function on a new field using sample data, this is the result:

You can check if values are in range with:

``````if y2 <= y1 <= y3
``````

I think you also should check if any of the inputs are None (missing value) since for example `None < 2000` will return True when this might not be what you want.

``````def calc(y1,y2,y3):
if None in (y1,y2,y3):
value = "Missing value(s)"
elif y2 <= y1 <= y3:
value = "WITHIN"
elif y1 > y2:
value = "OVER"
else:
value = "UNDER"
return value
``````

You then call the function with the acutal field names enclosed in !!, for example:

``````Expression:
calc(!y1!,!y2!,!y3!)
``````