Using the Get Count tool as field calculator expression - ModelBuilder

869
5
02-20-2020 11:43 AM
LeahFulton
New Contributor

Hello,

Trying to figure out how to use the Get Count Tool as a variable and to use it in a field calculator expression. Tried to use inline variable substitution but it is not working properly and giving me an error related to string/interger. The Row Count is set as a precondition. Attached is a photo for reference. any advice would be well appreciated!

0 Kudos
5 Replies
DavidPike
MVP Frequent Contributor

Can you try without the quotations around Row Count

0 Kudos
LeahFulton
New Contributor

Tried without the quotations and it only works without any other numbers in the box

0 Kudos
DavidPike
MVP Frequent Contributor

ok try this (sentences which start with a # are just comments to you, not part of the code).

# insert this where it says 'code block'

def func(field):
    result = (float(field) + 1.0)/ 2.0
    
    return result

#put this where it says Med_PS =

func(%Row Count%)




#if that doesnt work try changing the 'code block' to

def func(field):
    result = (float(field[0]) + 1.0)/ 2.0
    
    return result




#if that doesnt work try changing the 'code block' to

def func(field):
    result = (float(field.getOutput(0)) + 1.0)/ 2.0
    
    return result
0 Kudos
DuncanHornby
MVP Notable Contributor

I have spoofed up a similar situation as shown below.

If I attempt to validate the expression I get a Syntax Error as shown above.

If I IGNORE that error message and run the model it runs without error!

curtvprice
MVP Esteemed Contributor

My suggestion is to calculate your value first using the Calculate Value tool.

Set the Row Count as precondition to the Calculate Value tool

Calculate Value Expression: ((%Row Count% + 1) / 2

Calculate Value output data type: Expression

Then you can connect the output of the Calculate Value to your Calculate Field tool as an input parameter.

0 Kudos