Negative time values being outputted

274
4
11-30-2023 12:41 PM
Labels (1)
Jabousaw
New Contributor II

I have this code written in Arcade for a data expression to display the maximum, minimum and average waiting times of the most recent 4 years in 3 separate indicators. However, the time values that are outputted as a result of the code are negative. I'm not sure what's causing the negative values as I've looked through the code several times and there's nothing that looks wrong to me. This is what it's outputting. 

Jabousaw_0-1701376467003.png

I've even tried flipping date received with the well abandonment date in line 41 of the code and still got negative values.

Jabousaw_1-1701376696383.png

Any suggestions to fix this?

// Define the feature set
var Dates = FeatureSetByPortalItem(
    Portal('https://regionofpeel.maps.arcgis.com'), 
    '5d0f25a7a3314686b526bab55653befb', 
    0
);

// Function to check if a string represents a valid date
function IsDate(dateString) {
    // Check if the date string is not null, undefined, or an empty string
    if (dateString == null || dateString == "") {
        return false;
    }

    // Attempt to create a Date object from the string using DateTime function
    var dateObject = Date(dateString);

    // Check if the created Date object is a valid date
    return !IsEmpty(dateObject);
}

var i = 0;
var totalWaitingTime = 0;
var maxWaitingTime = -Infinity;
var minWaitingTime = Infinity;

var features = [];

// Calculate the date 4 years ago
var currentDate = Date();
var fourYearsAgo = DateAdd(currentDate, -4, 'years');

// Loop through records in the feature set
for (var record in Dates) {
    var wellAbandonmentDate = record['Date_abandonment'];
    var dateReceived = record['date_recieved'];

    // Check if both dates are valid and within the last 4 years
    if (IsDate(wellAbandonmentDate) && IsDate(dateReceived) && wellAbandonmentDate >= fourYearsAgo && dateReceived >= fourYearsAgo) {
        // Calculate waiting time in days using the DateDiff function
        var waitingTime = DateDiff(dateReceived, wellAbandonmentDate, 'days');

        // Update total, max, min waiting times
        totalWaitingTime += waitingTime;
        
        // Update max and min waiting times without using operators
        maxWaitingTime = Max(maxWaitingTime, waitingTime);
        minWaitingTime = Min(minWaitingTime, waitingTime);

        // Add feature to Dict
        var feat = {
            'attributes': {
                'WellAbandonmentDate': wellAbandonmentDate,
                'DateReceived': dateReceived,
                'WaitingTime': waitingTime
            }
        };

        // Replace the features array with the current feature if it has a greater waiting time
        if (waitingTime == maxWaitingTime) {
            features = [feat];
        }

        i++;
    }
}

// Calculate average waiting time
var averageWaitingTime;
if (i > 0) {
    averageWaitingTime = totalWaitingTime / i;
} else {
    averageWaitingTime = null;
}

// Loop through features
for (var j in features) {
    var featureData = features[j];
    // Add calculated values to the features
    featureData.attributes.AverageWaitingTime = averageWaitingTime;
    featureData.attributes.MaxWaitingTime = maxWaitingTime;
    featureData.attributes.MinWaitingTime = minWaitingTime;
}

var Dict = { 
    'fields': [ 
        {'name': 'AverageWaitingTime', 'type': 'esriFieldTypeDouble'},
        {'name': 'MaxWaitingTime', 'type': 'esriFieldTypeDouble'},
        {'name': 'MinWaitingTime', 'type': 'esriFieldTypeDouble'}
    ],   
    'geometryType': '',    
    'features': features
};

return FeatureSet(Dict);
0 Kudos
4 Replies
KenBuja
MVP Esteemed Contributor

Have you examined the data itself? You probably have dates that were added erroneously (wrong month or year), leading to those negative numbers. You could put in a statement like this to record in the console any dates that have a negative waiting time when you run the script in the editor

if (waitingTime < 0) console(`Feature: ${record.ID}, Date Received: ${Text(dateReceived, 'MM-DD-YYYY')}, Date Abandoned: ${Text(wellAbandonmentDate, 'MM-DD-YYYY')}`)

 

0 Kudos
Jabousaw
New Contributor II

The data being entered is off a calendar option within a survey. The only way for the data to be wrong is if the date of abandonment is before the request date but from skimming through data it looks normal. I'll take a better look at the data to see if this is the case or not.

0 Kudos
KenBuja
MVP Esteemed Contributor

I updated my reply with some code that will write those negative wait times to the console.

0 Kudos
Jabousaw
New Contributor II

I got an error when adding the line of code but I found the issue causing negative outputs. There was some bad data. Thanks again for all the help!

0 Kudos