Calculate (plus) multiple fields

2840
6
Jump to solution
11-23-2015 07:33 AM
Highlighted
New Contributor II

I have problems to calculate (plus) multiple fields in armap. It is normal? any solution?

Tags (2)
Reply
0 Kudos
1 Solution

Accepted Solutions
Highlighted
MVP Honored Contributor

As Richard Fairhurst​ indicates, you can get as this with a fairly compact Python code block:

def calc(*args):

  return sum(i for i in args if i is not None) # loop through all arguments in function, adding if not 'None' (i.e. null)

Your expression would be something like below (listing all desired candidate fields):

calc (!Field1!, !Field2!, ..., !Fieldn!)

View solution in original post

6 Replies
Highlighted
MVP Esteemed Contributor

Perhaps you could be a little more specific and provide details of what you are trying to accomplish.

Reply
0 Kudos
Highlighted
MVP Honored Contributor

Post the calculation you used.  Did you build the expression using the expression builder field list?  Are you sure all of the fields are Double, Float, Long or Short fields?  Is the output field the correct type to hold the addition result (typically you should use either a Double or a Long output field).  Are there Null values in the fields you are adding together?  Is the data source a shapefile/dbf file or a geodatabase feature class/table.  Is it an SDE database?  What version of ArcMap are you using?

Highlighted
New Contributor II

Hi thanks for answering,

all fields are double,

the output field is double,

there are null values in the fields,

I'm doing it on a feature class (geodatabase)

armap version 10.3

Calculo:

[PSB_01_V_AÑO]+ [PSB_05_V_AÑO]+ [PSB_03_V_AÑO]+ [PCC_01_V_AÑO]+ [PCC_02_V_AÑO]+ [PCC_04_V_AÑO]+ [PCC_05_V_AÑO]+ [PAM_01_V_AÑO]+ [PAM_02_V_AÑO]+ [PAM_03_V_AÑO]+ [PCE_01_V_AÑO]+ [PCE_02_V_AÑO]+ [PCE_03_V_AÑO]+ [PCE_04_V_AÑO]+ [PCE_05_V_AÑO]+ [PCE_07_V_AÑO]+ [PRE_01_V_AÑO]+ [PRE_02_V_AÑO]+ [PRE_03_V_AÑO]+ [PSP_01_V_AÑO]+ [PSP_02_V_AÑO]+ [PDSP_01_V_AÑO]+ [PDSP_2_V_AÑO]+ [PDSP_3_V_AÑO]+ [PFPS_2_V_AÑO]+ [PFPS_3_V_AÑO]+ [PFPS_1Y4_V_AÑO]

Reply
0 Kudos
Highlighted
MVP Honored Contributor

You cannot add Null values with other values.  See Fezter's answer to this post for a Python method you should be able to adapt to your problem.

Highlighted
MVP Honored Contributor

As Richard Fairhurst​ indicates, you can get as this with a fairly compact Python code block:

def calc(*args):

  return sum(i for i in args if i is not None) # loop through all arguments in function, adding if not 'None' (i.e. null)

Your expression would be something like below (listing all desired candidate fields):

calc (!Field1!, !Field2!, ..., !Fieldn!)

View solution in original post

Highlighted
MVP Esteemed Contributor

Or as documented in this link Before I forget ... # 18 ... Those pesky <null> things...

for a pythonic approach

Reply
0 Kudos