# Calculate maximum value from a subset of fields ending with a standard suffix

I'm using the Python built-in function in the calculate field tool in Modelbuilder to calculate maximum values for each record from a list of fields:

This is the field calculator expression I use in the moment: `max[(!field1_abc!, !field2_abc!, !field3_abc!)]`

It worked perfectly so far. But I selected the fileds that are calculated manually. So now I'd like to find a way to automate it. I'd like to calculate the maximum from all fields ends with "abc".

Is it possible to combine the built- in fuction with something like "calculate the max from all fields .endwith("abc")"?

I tried something like: `max([!endswith("abc")!])` but it is not working.

• When you say python built-in function in modelbuilder, how exactly are you calling the max() function? In a field calculate tool, select by attribute or calculate value? It's not at all clear what you are doing, suggest you edit your question to show what you model is doing and how. May 11, 2018 at 11:19
• @Hornbydd: Is it more understandable now?
– Lisa
May 11, 2018 at 11:44

This can be done in modelbuilder but you need to construct the expression for the calculate tool. The model is simply this:

I have a table called test and it is loaded into the map document:

Calculate Value tool is as this:

Note that I pass in the name of the table, in my case `test`. The code is constructing the expression that feeds into the calculate field tool expression parameter. It creates an expression choosing the max value from all fields ending with `_abc`. Obviously your fields need to be numeric! This code assumes that is the case.

The code block is provided below to copy:

``````import arcpy
def createExpression(dataset):
l = arcpy.ListFields(dataset,"*_abc","ALL")
sql = "max(["
for f in l:
sql = sql + "!" + f.name + "!" + ","
sql = sql[:-1] + "])"
return sql
``````

The calculate tool is set up as:

It takes the expression built by the calculate value tool and computes the maximum value into an existing numeric field called `m`.

• thank you so much - it works perfectly :) Saves me at least 2 weeks of work...Thank you
– Lisa
May 14, 2018 at 8:00
• can I also use the create value tool to select all my imports for a union? I tried something like: Expression: CreatePolygon("test") Code Block: import os import arcpy def createExpression(dataset): featureclasses = arcpy.Listfeatureclasses(dataset,"_","ALL") for fc in featureclasses: arcpy.union_analysis(["fc", "otherPolyon"],"Exportpath","NO_FID") Datatype: Poygon But it is not working :(
– Lisa
May 15, 2018 at 9:05
• I would not use the tool in that manner, use the Union tool directly in the model. Also you are break GIS SE rules you are asking a NEW question but in the comments of an answer to another question...how are others supposed to find that Q&A? ...They can't May 15, 2018 at 10:23
• sry...did not thought about that
– Lisa
May 15, 2018 at 11:04

Please give this a try in the python console. Assuming you only have 3 fields and are putting the max value of fields in another field, so we create a max field

``````import arcpy

arcpy.env.workspace = (r"E:/folder1/folder2")

inputFc = "test.shp" # set to the right file
fieldList = []

#create field for max value (may not be what you want)

fields = arcpy.ListFields(inputFc)
for field in fields:
if (field.name).endswith('abc'):
fieldList.append(field.name)

with arcpy.da.UpdateCursor(inputFc, fieldList) as uc:
for row in uc:
row[3] = max(row[0], row[1], row[2])
uc.updateRow(row)
``````
• thank you for the answer :) but i haven't tried it because the previous answer aready helped me.
– Lisa
May 14, 2018 at 8:01