Best way to auto-calculate a field?

894
5
02-25-2022 04:06 AM
JasonCyphers
Occasional Contributor III

In either the Web AppBuilder, Field maps, or in a Dashboard (Portal 10.8.1), is there a way to auto-populate a feature's "Response Time" attribute field, based on two other date fields ("Report Date/Time" and "On-Scene Date/Time") any time a new feature is added?

0 Kudos
5 Replies
jcarlson
MVP Esteemed Contributor

There are Attribute Rules, if your data source is the type that can utilize them (i.e., and SDE layer). But if it's just a hosted layer or something, there's not a way to do this currently.

That said, where do you need to use these calculated values? Arcade can fit the bill in most places.

- Josh Carlson
Kendall County GIS
0 Kudos
JasonCyphers
Occasional Contributor III

Need to use the "response time" attribute value (calculated from the difference between on-scene time and dispatched time) in a dashboard.  I used arcade to display it in the feature's pop-up, but I need a way to show "average response time" in a dashboard.

Points can either be added via a Web App or via Field Maps, so a solution in either/both of those formats would be great.

0 Kudos
jcarlson
MVP Esteemed Contributor

This is precisely the kind of situation that make Data Expressions so great in Dashboards, but you can't get at them in your version of Enterprise.

The next best thing would be a Python script that just evaluates frequently to update the field.

from arcgis import GIS

gis = GIS('your portal url', 'username')

# get layer
fl = gis.content.get('itemid of layer').layers[0] # or whatever layer index applies

# calculation expression
expr = {
    'field': 'response_time',
    'value': 'DATEDIFF(minute, on_scene_time, dispatch_time)'
}

# calculate field
fl.calculate(calc_expression=expr)

Note that the SQL expression used to calculate the field will vary depending on your server, but DATEDIFF is pretty standard.

A script like that would take very little time to run, and could be scheduled to fire every few minutes, or whatever interval makes sense for your users.

- Josh Carlson
Kendall County GIS
0 Kudos
JasonCyphers
Occasional Contributor III

Thanks!  I'm assuming this is something that runs on the server?  I'm just a "high-level" end user...I don't do much much on the server-side of things, but could get out GIS-IT dept. involved if need be.

0 Kudos
jcarlson
MVP Esteemed Contributor

Not so! That's one of the great things about the ArcGIS Python API. Any machine that has a Python environment with the module installed can run that script, provided the user who "signs in" in the script has the permissions necessary to edit the layer.

Check out Anaconda for tools to manage Python envs. If you do set up an automated process, it's good to have it running in a dedicated environment.

- Josh Carlson
Kendall County GIS