Calculate Field (Data Management Tools)
Summary
Calculates the values of a field for a feature class, feature layer, or raster.
Usage
Caution:
This tool modifies the input data. See Tools that modify or update the input data for more information and strategies to avoid undesired data changes.
To learn more about Python expressions, see Calculate Field Python examples.
To learn more about Arcade expressions, see ArcGIS Arcade in the Developer help.
To learn more about SQL expressions, see SQL expression syntax and Calculate field values.
To learn more about VBScript expressions, see Calculate Field VBScript examples.
This tool's Arcade expressions use the Field Calculation Arcade profile. An Arcade profile is a context in which an Arcade expression is evaluated and understood. When an Arcade expression is evaluated, the return value is cast to the field type of the output value.
When used with a selected set of features, such as those created from a query using the Make Feature Layer or Select Layer By Attribute tool, this tool will only update the selected records.
Caution:
With Windows 11, version 24H2, VBScript becomes a feature on demand and requires the VBScript capability.
To continue to use the VBScript option, do one of the following:
Convert the expression type to Python, Arcade, or SQL.
Enable the VBScript optional feature in the Windows system settings.
To learn more, see Removal and deprecation notices.
The calculation can only be applied to one field per operation. To apply multiple calculations, use the Calculate Fields tool.
Existing field values will be overwritten. To preserve the original values, make a copy of the input table as a backup, or use the Enable Undo option in the Geoprocessing pane.
For Python calculations, field names must be enclosed in exclamation points (for example,
!fieldname!).For Arcade calculations, field names must be prefixed with
$feature.(for example,$feature.fieldname).Note:
To transfer temporal values between field types (Date, Date Only, Time Only, Timestamp Offset, and Text), use the Convert Temporal Field tool instead.
To calculate strings to text or character fields, on the dialog box, the string must use double quotation marks (for example, "string"), or in scripting, the string using double quotation marks must also be enclosed in single quotation marks (for example,
'"string"').To calculate a field to be a numeric value, provide the numeric value in the Expression parameter with no quotation marks around the value.
Legacy:
arcgis.rand()is no longer supported as of ArcGIS Pro 2.0. Comparable functions using Python'srandommodule should be used instead. To use therandommodule successfully, add it as an import in the Code Block parameter.You can create complex expressions using the Code Block parameter. Provide the code block either directly on the dialog box or as a string in scripting. The expression and code block are connected. The code block must relate back to the expression; the result of the code block must be passed to the expression.
The Code Block parameter is only supported for Python expressions.
You can use the Python
mathmodule and formatting in the Code Block parameter. You can also import additional modules. Themathmodule provides number-theoretic and representation functions, power and logarithmic functions, trigonometric functions, angular conversion functions, hyperbolic functions, and mathematical constants. To learn more about themathmodule, see the Python help.To calculate geometry information in Python, use
Geometryobject properties. For example, use an expression of!shape!.pointCount!to calculate the number of vertices in a feature.Note:
The Calculate Geometry Attributes tool supports similar calculations.
To calculate area and length in Python, use the
getAreaandgetLengthmethods with a method and unit type.To calculate the geodesic area of polygons in square kilometers, use the following expression:
!shape!.getArea('GEODESIC', 'SQUAREKILOMETERS')!To calculate the planar length of polylines (or polygons) in yards, use the following expression:
!shape!.getLength('PLANAR', 'YARDS')
See the ArcPy
PolygonandPolylineobjects for more information.Learn more about geoprocessing tools and linear and areal units
ArcGIS applications use UTF-16-LE encoding to read and write
.calfiles. Other applications (for example, Notepad) can be used to create or modify.calfiles as long as the file is written using UTF-16-LE encoding. A file with any other encoding will not load into the code block.Python and SQL expressions that attempt to concatenate string fields that include a null or divide by zero value will return null for that field value.
Note:
Using an SQL expression to concatenate string fields in a mobile geodatabase will combine values while ignoring null values.
When working with joined data, you can only update fields from the origin table. You cannot update fields from the joined table. To perform a calculation on the joined table, perform the calculation directly on that table.
SQL expressions support faster calculations for feature services and enterprise geodatabases. Instead of performing calculations one feature or row at a time, a single request is sent to the server or database, resulting in faster calculations.
Feature services, enterprise geodatabases, mobile geodatabases, file geodatabases, databases, SQLite, and GeoPackage support SQL expressions. For other formats, use Python or Arcade expressions.
Using the SQL option for the Expression Type parameter has the following limitations:
The option is only supported for file geodatabases, mobile geodatabases, Db2, Oracle, PostgreSQL, SAP HANA, and SQL Server (with or without enterprise geodatabases in them), SQLite, and GeoPackage.
Calculating field values on joined tables is not supported.
Versioned and archived enterprise geodatabase data is not supported.
Undoing geoprocessing operations is not supported.
See your database vendor documentation for SQL expression help.
The Expression parameter on the tool dialog box has an Insert Values drop-down list where you can add field values from the selected field in the Fields list or domain values from the selected field's domain. Use the domain values to ensure that only valid values for the field domain are inserted into the field.
Parameters
| Label | Explanation | Data type |
|---|---|---|
|
Input Table |
The table containing the field that will be updated with the new calculation. |
Mosaic Layer; Raster Layer; Table View |
|
Field Name (Existing or New) |
The field that will be updated with the new calculation. If a field with the specified name does not exist in the input table, it will be added. |
Field |
|
Expression |
The simple calculation expression that will be used to create a value that will populate the selected rows. |
SQL Expression |
|
Expression Type (Optional) |
Specifies the type of expression that will be used. If the input is a feature service, the default expression type is SQL. For any other type of input, the default expression type is Python. To learn more about Python expressions, see Calculate Field Python examples. To learn more about Arcade expressions, see ArcGIS Arcade in the Developer help. To learn more about SQL expressions, see SQL expression syntax and Calculate field values. SQL expressions support faster calculations for feature services and enterprise geodatabases. Instead of performing calculations one feature or row at a time, a single request is sent to the server or database, resulting in faster calculations. Only feature services, enterprise geodatabases, mobile geodatabases, file geodatabases, databases, SQLite, and GeoPackage support SQL expressions. For other formats, use Python or Arcade expressions. To learn more about VBScript expressions, see Calculate Field VBScript examples.
|
String |
|
Code Block (Optional) |
A block of code that will be used for complex Python or VBScript expressions. |
String |
|
Field Type (Optional) |
Specifies the field type of the new field. This parameter is only used when the field name does not exist in the input table. If the field is of type text, the field will have a length of 512, unless the input is a shapefile or dBASE file, in which case the length will be 254. To adjust the length, use the Alter Field tool.
|
String |
|
Enforce Domains (Optional) |
Specifies whether field domain rules will be enforced.
|
Boolean |
Derived output
| Label | Explanation | Data type |
|---|---|---|
|
Updated Input Table |
The updated table. |
Table View; Raster Layer; Mosaic Layer |
Environments
Current Workspace, Extent, Transfer Field Domain Descriptions
Special cases
- Transfer Field Domain Descriptions
-
When this environment is turned on, any fields that have an associated domain will use the domain description instead of the field's value. Values from fields that do not have a domain will be unaffected.
Licensing information
- Basic: Yes
- Standard: Yes
- Advanced: Yes