HOW TO

Classify and filter the most recent Survey123 records using Notebooks in ArcGIS Online

First Published: April 9, 2024
Last Published: May 28, 2026

Summary

In ArcGIS Online, when working with records collected using the ArcGIS Survey123, it is useful to display only the most recent records for reporting or decision-making purposes. By using ArcGIS Notebooks, records can be compared across the dataset based on a survey date field to identify and classify the latest entries by updating another field with custom values such as Recent or Outdated. Once updated, this field is used in Map Viewer to apply filters that display only the most recent records on the map.

This article provides the workflow to classify the most recent ArcGIS Survey123 records using ArcGIS Notebooks and filter them in Map Viewer.

Procedure

  1. In ArcGIS Online, create a copy of the survey's hosted feature layer. Refer to ArcGIS Online: Duplicate a layer for instructions.
  2. Add a new field with a text data type to the copied hosted feature layer. In this example, the field is named Date_Status.
  3. In ArcGIS Online, click Notebooks > My notebooks > New notebook > Standard.
  4. In the notebook, specify the script below to classify the most recent records based on the date field.
  1. Specify the code below to import the library and access to the hosted feature layer in ArcGIS Online.
from arcgis.gis import GIS

gis = GIS("home")

item = gis.content.get("<itemID>")  #Replace <itemID> with the portal item ID of the hosted feature layer
layer = item.layers[<layer_index>]  #Use the appropriate layer index based on the data
  1. Specify the date field and the target field.
date_field = "<fieldName1>"  #Replace <fieldName1> with the date field name 
target_field = "<fieldName2>"  #Replace <fieldName2> with the new field name


features = layer.query(
    where="1=1",
    out_fields="<fieldName1>,<fieldName2>",
    return_geometry=False
).features
  1. Identify the most recent date from the date field.
latest_date = None

for f in features:
    val = f.attributes.get(date_field)

    if val:
        if latest_date is None or val > latest_date:
            latest_date = val

print("Latest date:", latest_date)
  1. Specify the code below to update the preferred values in the target field.
updates = []

for f in features:
    val = f.attributes.get(date_field)

    if val == latest_date:
        f.attributes[target_field] = "<Value1>"  #Replace <Value1> with a value representing recent records
    else:
        f.attributes[target_field] = "<Value2>"  #Replace <Value2> with a value representing older records

    updates.append(f)
  1. Apply the updates to the hosted feature layer.
result = layer.edit_features(updates=updates)

print("Update completed")
print(result)

Below is the full working script for this example.

from arcgis.gis import GIS

gis = GIS("home")

item = gis.content.get("cc8ac240812f45bfafc757949fdcf740")
layer = item.layers[0]

date_field = "inspection_date"
target_field = "Date_Status"


features = layer.query(
    where="1=1",
    out_fields="inspection_date,Date_Status",
    return_geometry=False
).features

latest_date = None

for f in features:
    val = f.attributes.get(date_field)

    if val:
        if latest_date is None or val > latest_date:
            latest_date = val

print("Latest date:", latest_date)

updates = []

for f in features:
    val = f.attributes.get(date_field)

    if val == latest_date:
        f.attributes[target_field] = "Recent"
    else:
        f.attributes[target_field] = "Outdated"

    updates.append(f)

result = layer.edit_features(updates=updates)

print("Update completed")
print(result)
Note:
After pasting the code, click Run this cell and advance to execute the cell. Click Save As to save the notebook for future use if required.
This workflow must be re-run each time new Survey123 records are submitted. To automate the process, consider scheduling the notebook.
  1. In Map Viewer, open the web map containing the hosted feature layer used to run the Python code.
  2. On the Contents (dark) toolbar, on the Layers tab, click the feature layer.
  3. Apply a filter to the layer to display the most recent records.
  1. On the Settings (light) toolbar, click Filter The Filter icon.
  2. In the Filter pane, click + Add new and create a filter expression as follows:
  1. Under Condition, for field name selector, select the field. In this example, it is Date_Status.
  2. For operator selector, select the is operator.
  3. For value input box, select the value. In this example, it is Recent.
The Filter pane
  1. Click Save.

The map below displays the most recent ArcGIS Survey123 records.

The most recent records are displayed on the map

Article ID: 000032410

Software:
  • ArcGIS Online
  • ArcGIS Survey123

Get support with AI

Resolve your issue quickly with the Esri Support AI Chatbot.

Start chatting now

Related Information

Discover more on this topic

Get help from ArcGIS experts

Contact technical support

Start chatting now

Go to download options