<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Negative time values being outputted in ArcGIS Dashboards Questions</title>
    <link>https://community.esri.com/t5/arcgis-dashboards-questions/negative-time-values-being-outputted/m-p/1355392#M8811</link>
    <description>&lt;P&gt;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.&lt;/P&gt;</description>
    <pubDate>Thu, 30 Nov 2023 20:58:27 GMT</pubDate>
    <dc:creator>Jabousaw</dc:creator>
    <dc:date>2023-11-30T20:58:27Z</dc:date>
    <item>
      <title>Negative time values being outputted</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/negative-time-values-being-outputted/m-p/1355381#M8808</link>
      <description>&lt;P&gt;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.&amp;nbsp;This is what it's outputting.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Jabousaw_0-1701376467003.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/87642i10F85F122637E01C/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Jabousaw_0-1701376467003.png" alt="Jabousaw_0-1701376467003.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;I've even tried flipping date received with the well abandonment date in line 41 of the code and still got negative values.&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="Jabousaw_1-1701376696383.png" style="width: 400px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/87643iDA9EA11BBF8586C0/image-size/medium?v=v2&amp;amp;px=400" role="button" title="Jabousaw_1-1701376696383.png" alt="Jabousaw_1-1701376696383.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Any suggestions to fix this?&lt;/P&gt;&lt;LI-CODE lang="c"&gt;// 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) &amp;amp;&amp;amp; IsDate(dateReceived) &amp;amp;&amp;amp; wellAbandonmentDate &amp;gt;= fourYearsAgo &amp;amp;&amp;amp; dateReceived &amp;gt;= 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 &amp;gt; 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);
&lt;/LI-CODE&gt;</description>
      <pubDate>Thu, 30 Nov 2023 20:41:57 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/negative-time-values-being-outputted/m-p/1355381#M8808</guid>
      <dc:creator>Jabousaw</dc:creator>
      <dc:date>2023-11-30T20:41:57Z</dc:date>
    </item>
    <item>
      <title>Re: Negative time values being outputted</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/negative-time-values-being-outputted/m-p/1355384#M8809</link>
      <description>&lt;P&gt;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&lt;/P&gt;&lt;LI-CODE lang="javascript"&gt;if (waitingTime &amp;lt; 0) console(`Feature: ${record.ID}, Date Received: ${Text(dateReceived, 'MM-DD-YYYY')}, Date Abandoned: ${Text(wellAbandonmentDate, 'MM-DD-YYYY')}`)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 30 Nov 2023 21:00:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/negative-time-values-being-outputted/m-p/1355384#M8809</guid>
      <dc:creator>KenBuja</dc:creator>
      <dc:date>2023-11-30T21:00:15Z</dc:date>
    </item>
    <item>
      <title>Re: Negative time values being outputted</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/negative-time-values-being-outputted/m-p/1355392#M8811</link>
      <description>&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Thu, 30 Nov 2023 20:58:27 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/negative-time-values-being-outputted/m-p/1355392#M8811</guid>
      <dc:creator>Jabousaw</dc:creator>
      <dc:date>2023-11-30T20:58:27Z</dc:date>
    </item>
    <item>
      <title>Re: Negative time values being outputted</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/negative-time-values-being-outputted/m-p/1355395#M8813</link>
      <description>&lt;P&gt;I updated my reply with some code that will write those negative wait times to the console.&lt;/P&gt;</description>
      <pubDate>Thu, 30 Nov 2023 21:01:19 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/negative-time-values-being-outputted/m-p/1355395#M8813</guid>
      <dc:creator>KenBuja</dc:creator>
      <dc:date>2023-11-30T21:01:19Z</dc:date>
    </item>
    <item>
      <title>Re: Negative time values being outputted</title>
      <link>https://community.esri.com/t5/arcgis-dashboards-questions/negative-time-values-being-outputted/m-p/1355806#M8824</link>
      <description>&lt;P&gt;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!&lt;/P&gt;</description>
      <pubDate>Fri, 01 Dec 2023 17:27:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-dashboards-questions/negative-time-values-being-outputted/m-p/1355806#M8824</guid>
      <dc:creator>Jabousaw</dc:creator>
      <dc:date>2023-12-01T17:27:46Z</dc:date>
    </item>
  </channel>
</rss>

