# Calculate (plus) multiple fields

2901
6
11-23-2015 07:33 AM
New Contributor II

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

Tags (2)
1 Solution

Accepted Solutions
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!)`
6 Replies
MVP Esteemed Contributor

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

can't wait to retire....
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?

New Contributor II

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]

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.

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!)`
MVP Esteemed Contributor

Or as documented in this link Before I forget ... # 18 ... Those pesky &lt;null&gt; things...

for a pythonic approach