Similar but separate Feature classes with differing field names | If Then , OR won’t recognize one field

1278
6
Jump to solution
09-22-2021 07:32 PM
jhomme981
New Contributor II

Hi! Im using ArcMap 10.x in ModelBuilder working with Electrical linear segments and calculating attributes: Overhead (OH) Primary lines and Underground (UG) primary lines. OH primary has field name: conductorMaterial, and the UG primary has the equivalent field name titled: neutralMaterial. 
I’m looking to have one statement that captures both fields attributes and use the THEN to = my combinedMaterial field.
i.e., If [conductorMaterial = “aluminum” Or [neutralMaterial] = “aluminum” Then
 combinedMaterial = “ACSR Cables”

Since conductorMaterial only exists in one attribute table and neutralMaterial exists in the other, I get an error because one field doesn’t exist for the expression. I could potentially create a new field and add more steps than I’d like but is there a way for a calculation to run even though a field may not be present?  I really hope that was clear. Thank ya for checking!!

0 Kudos
2 Solutions

Accepted Solutions
curtvprice
MVP Esteemed Contributor

In ArcMap, the precondition route (no "If" tool as we have in Pro ModelBuilder) would be more complicated, so it may make more sense to build your select expression in Python using the Calculate Value tool with logic based on what fields are there, and pass that SQL expression to the select by attributes tool.

View solution in original post

curtvprice
MVP Esteemed Contributor

Here's a solution that may work for you:

ModelBuilder fmodelModelBuilder fmodel

and my Calculate Value code to build your SQL expression to select the rows you want to calculate:

# Calculate Value expression
make_expr(r"%input table%")

# code block
# returns either "CONDUCTORMATERIAL = 'aluminum' or 
#                "NEUTRALMATERIAL = 'aluminum'
def make_expr(tbl):
    f1 = "conductorMaterial".upper()
    f2 = "neutralMaterial".upper()
    fld_list = [f.name.upper() for f in arcpy.ListFields(tbl)] 
    if f1 in fld_list:
        fld = f1
    else:
        fld = f2
    expr = "{} = \'aluminum\'".format(fld)
    return expr

View solution in original post

6 Replies
curtvprice
MVP Esteemed Contributor

I do not understand well I think - how could you select on a field that is in another table? Maybe you want to add a join so both fields are available in a joined table?

If the second field may or may not be there in the table you are selecting you could use preconditions to choose which select expression to use (one with the field, one without) but it's not clear to me that is your issue.

0 Kudos
jhomme981
New Contributor II

Thank you for the response. I think we are tracking, and the join would be an option, but the OH or UG occur with many site locations and I was hoping to not have to run a join for every instance (site). It sounds like the join is the way it would have to be however.  My thought was, could I write a statement that would acknowledge one field or the other field (since only one or the other will be present each time) and then be able to disregard the field that is not present but capture the field that is present. Basically, a generic statement that works for either instance.  I'm sorry for the lack of clarity! Many thanks again for your time!! 🙂  

 

0 Kudos
curtvprice
MVP Esteemed Contributor

In ArcMap, the precondition route (no "If" tool as we have in Pro ModelBuilder) would be more complicated, so it may make more sense to build your select expression in Python using the Calculate Value tool with logic based on what fields are there, and pass that SQL expression to the select by attributes tool.

jhomme981
New Contributor II

I gotcha and that's what I was thinking/hoping to avoid since my python knowledge is pretty limited at this point. Thank you for identifying the problem/solution more specifically! I will now try heading into uncharted waters (for me, anyway)  😉   Thank you very much!! 

0 Kudos
curtvprice
MVP Esteemed Contributor

Here's a solution that may work for you:

ModelBuilder fmodelModelBuilder fmodel

and my Calculate Value code to build your SQL expression to select the rows you want to calculate:

# Calculate Value expression
make_expr(r"%input table%")

# code block
# returns either "CONDUCTORMATERIAL = 'aluminum' or 
#                "NEUTRALMATERIAL = 'aluminum'
def make_expr(tbl):
    f1 = "conductorMaterial".upper()
    f2 = "neutralMaterial".upper()
    fld_list = [f.name.upper() for f in arcpy.ListFields(tbl)] 
    if f1 in fld_list:
        fld = f1
    else:
        fld = f2
    expr = "{} = \'aluminum\'".format(fld)
    return expr
jhomme981
New Contributor II

Greetings curtvprice:

Thanks!! I was able to dissect the logic and instruction you provided and now have a much better understanding! The calculation works and is waaay simpler than the road I was going down. Thank you again!

V/r,

Jacob

0 Kudos