HOW TO

Subtract field values and exclude negative results using Arcade in ArcGIS Online

Last Published: February 7, 2024

Summary

In ArcGIS Online, subtracting field values from a specified value may return negative results, as shown in the image below.

The subtraction outputs negative results

Having results devoid of negative values when substracting is ideal in situations involving boundary protection, where negative values may return unexpected results on the map, and logical consistency, where returning '0' as the result maintains a meaningful interpretation in the output.

This article provides the workflow to subtract field values and exclude negative results using Arcade in ArcGIS Online.

Procedure

  1. Log in to ArcGIS Online and click Content > My Content.
  2. Click the hosted feature layer containing the numeric values to open the item details page.
  3. Calculate the field to subtract using Arcade. In this example, the 'Subtract' field is selected. Refer to ArcGIS Online: Calculate values for a field for instructions.
  4. In the Expression window, specify the following Arcade expression:
    1. Define the field to subtract. Replace '<variableName1>' with a variable name and '<fieldName>' with the name of the field.
var <variableName1> = $feature.<fieldName>;
  1. Define the value to subtract. Replace '<variableName2>' with a new variable name and '<subtractionValue>' with the desired value.
var <variableName2>= <subtractionValue>;
  1. Define the IIf conditional expression. If the value in the field to subtract is less than the value used to subtract, 0 is returned instead of negative results.
IIf(<variableName1> < value, 0, <variableName1> - <variableName2>);

The code block below shows an example of the full working script.

var subtract_from = $feature.Subtract;
var value = 1200;

IIf(subtract_from < value, 0, subtract_from - value);
  1. Click OK.

The image below shows non-negative values in the field when the subtraction results in a negative number.

The values in the field return a 0

Article ID: 000031849

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

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Download the Esri Support App

Go to download options