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
}