Select to view content in your preferred language

# Calculate Value Expression Help

1446
4
04-24-2014 01:33 PM
Occasional Contributor
Hi every one,

I have a very simple model that selects an attribute by a layer and counts the rows of that attribute type. I would like to use the calculate value tool to make a boolean true and false out puts. Just to check if the value is greater than X else less than X.

I've tried following the help here but I can't seem to get it right:

http://blogs.esri.com/esri/arcgis/2011/06/06/modelbuilderifthenelse2/

any ideas?

Best,
Devon
Tags (3)
4 Replies
Occasional Contributor
MVP Esteemed Contributor
I have a very simple model that selects an attribute by a layer and counts the rows of that attribute type. I would like to use the calculate value tool to make a boolean true and false out puts. Just to check if the value is greater than X else less than X.

A really tricky part about this in ModelBuilder is that a zero count is false - if you use this as a precondition this will prevent anything after it from running.

The only way I've seen around this is to run the Get Count tool with Python code inside a Calculate Value tool and return 1 or 2, not 0 and n.

Here's an example, you can modify this for any boolean choice based on count.

```expression: oneOrTwo(r"%RSL%")

# code block - note converting result to integer
import arcpy
def oneOrTwo(ds):
# In python you need to unpack count from result object
numsel = int(arcpy.GetCount_management(ds).getOutput(0))
if numsel == 0:
numsel = 1
else:
numsel = 2
return numsel
```

You can then precondition the output of this to two Calculate Value tools (%output_value% == 1, %output_value% == 2) and use them as preconditions for branches, merging the two down the processing chain with the model tool Merge Branch.

By the way, the If you are stuck at if series of blogs you linked is a great resource for advanced ModelBuilder.

If you're trying to calculate one thing or another in your table based on the value of another field, you may be going at this wrong - you may want to look into a code block inside Calculate Field instead of this method.
Occasional Contributor
hey hey,

thank you for your time. I guess I need to try and learn python better. ultimately I'd like to have multiple outputs... not just true and false. For ex. if the count is higher than 100, continue to the next model too. elseif, elseif else etc. Very similar to the test filter/tester tool in FME if you are familiar with the software.

So the code you sent me just prevents the value calculator from allowing zero/n as an option. hmm okay. just need to study up and learn how to add in the count operators.

thanks for your help.
MVP Esteemed Contributor
I think you are thinking right: when you need to do multiple branching like that you should be looking into going to a Python script.

When you add branches, the model gets very very complex and has so many elements it starts to take a long time to validate. The most complex branching I've worked with is three branches.

I have found that when I need to do something complex I can sometimes write a short python function (debugged outside of the environment of course) and drop it inside Calculate Value. The downside is you can't do any messaging from inside the Calculate Value tool - you need a separate python script tool if you want to provide messages.