Create 3 separate indicators that show the average, maximum and minimum waiting times based on 2 date entries

309
0
11-21-2023 04:06 PM
Labels (1)
by
New Contributor II
I'm working on an ArcGIS dashboard. I am trying to create 3 separate indicators that show the average, maximum and minimum waiting times it takes to complete their request. The only data being collected are  2 date fields, "Request Date" and "Date of Abandonment". In order to compute the times I need, I need to calculate the difference in time between the "Request Date" and "Date of Abandonment". Since I'm new to coding and using Arcade/dashboards, I used ChatGPT for help with writing the code and then edited it from there. The code given to me by ChatGPT is below but I keep receiving errors stating that "
'\$data', '\$IsDate', and '\$feature' are not defined. Also, 'FeatureSetByName("Well Abandonment Database")'
expects at least 2 argument(s) but I don't know what the second argument would be.

Any help with figuring this out is greatly appreciated.

var requestDate = \$data["date_recieved"];
var abandonmentDate = \$data["Date_abandonment"];

// Check if both dates exist
if (IsDate(requestDate) && IsDate(abandonmentDate)) {
// Calculate waiting time in days
var waitingTime = DateDiff(requestDate, abandonmentDate, "days");

// Retrieve related records using a spatial query (assumes point features)
var relatedRecords = Intersects(FeatureSetByName("Well Abandonment Database"), \$feature);

// Initialize variables for calculations
var sum = waitingTime;
var count = 1;
var max = waitingTime;
var min = waitingTime;

// Loop through related records
for (var i in relatedRecords) {
var relatedRecord = relatedRecords[i];

if (relatedRecord["status"] == "Abandoned") {
var relatedRequestDate = relatedRecord["date_recieved"];
var relatedAbandonmentDate = relatedRecord["Date_abandonment"];

if (IsDate(relatedRequestDate) && IsDate(relatedAbandonmentDate)) {
// Calculate waiting time in days for related records
var relatedWaitingTime = DateDiff(relatedRequestDate, relatedAbandonmentDate, "days");

// Update sum, count, max, and min
sum += relatedWaitingTime;
count++;
max = Max(max, relatedWaitingTime);
min = Min(min, relatedWaitingTime);
}
}
}

// Calculate average waiting time
var average;
if (count > 1) {
average = sum / count;
} else {
average = null;
}

// Return results
{
"MaxWaitingTime": max,
"MinWaitingTime": min,
"AverageWaitingTime": average
}
} else {
null; // If either date is missing, return null
}

0 Replies