HOW TO

Populate a new field with values based on substring values from another field using Arcade expressions in ArcGIS Online

Last Published: December 6, 2023

Summary

Arcade expressions provide the ability to calculate new fields with values from available attribute data in the feature layers in ArcGIS Online. In some cases, it is necessary to populate a new field with values based on substring values from another field. For example, when inspecting fire hydrants in a city, the area codes for the fire hydrants are determined based on the their unique ID (for example, X-100B with '-1' represents the area code 'A'). This article describes the workflow to add a new field and populate it with area codes for each fire hydrant using its ID.

The following image shows the attribute table of the fire hydrants ID field, and the new field to be populated with area codes.

Image showing the attribute table and the new field to be populated with area codes

Procedure

  1. Add a new String field in the feature layer. Refer to ArcGIS Online: Add a field for steps to do so. In this example, the new field is Area_Code.
  2. Click the new field, and select Calculate. In the Calculate Field window, select Arcade.
  3. Fill in the Arcade Calculator dialog box with the following Arcade expressions to find unique substring values in the ID field, and populate the new field based on the values.
if (Find("-1", $feature.ID, 0)>0) {
   return "A";
} else if (Find("-2", $feature.ID, 0)>0) {
   return "B";
} else if(Find("-3", $feature.ID, 0)>0) {
   return "C";
} else if(Find("-4", $feature.ID, 0)>0) {
   return "D";
} else if(Find("-5", $feature.ID, 0)>0) {
   return "E"
}
Note:
The following explains the components of the expressions:

Find("-1", $feature.ID, 0)>0)
The 'Find' function searches through the ID field, and if '-1' exists, it returns '1'. Otherwise, it returns '0'.

if (Find("-1", $feature.ID, 0)>0) {
   return "A";
The 'if' statement specifies a condition that if '-1' exists in the ID field, the condition is true, so the new field is populated with A.

} else if (Find("-2", $feature.ID, 0)>0) {
   return "B";
The 'else if' statement specifies a new true condition. Since '-2' exists in the ID field, the new field is populated with B.

The image below shows the Area_Code field populated with values calculated using Arcade expressions.

Image showing the Area_Code field populated with values

Article ID:000023586

Software:
  • ArcGIS Online

Receive notifications and find solutions for new or common issues

Get summarized answers and video solutions from our new AI chatbot.

Download the Esri Support App

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options