[GIS] Getting maximum value from multiple values output (list of numbers) using Calculate Value in ModelBuilder

arcgis-desktoparcpymodelbuildermulti-values

I have an output from a sub model that iterates through feature classes, each with a row count. The row counts are passed to a collect values, which is a model parameter and is used outside the sub model.

How can i use simple code in a "calculate value" tool to get the maximum value from this list of numbers. I wish to then use this as a precondition to subsequent analysis so that if the sub model does not produce results then the subsequent analysis will not run.

This is similar to Exporting data from Collect Values output in ArcGIS ModelBuilder? that wished to use the actual list of numbers. The answer suggested an export to .csv file.

I only need to use a simple output so that I can use it as a pre condition on subsequent analysis. I have tried to calculate the Max(list), but it has not worked.

Best Answer

OK you have a model that is collecting values (of type long) and its a parameter. This is a sub model to the master model. In the master model connect your collection of values as a precondition to a Calculate Value tool. These collected values are called "Output Values".

In your Calculate Values tool:

Expression:

summe("%Output Values%")

Code Block:

import string
def summe(s):
  l = string.split(s,";")
  max = -1
  for i in l:
    if i > max:
      max = i
  return max

Data type:

Long

This approach is taking advantage of the fact that your collected values are "MultiValues" which are semicolon separated.

Example model finding the max value in a multivalue list