Select to view content in your preferred language

Indicator Arcade Assistance

76
6
Jump to solution
yesterday
GaryMorris
Regular Contributor

I am creating a Dashboard displaying data from my city's permits.  I have text fields of the application date and the issued date.  I am trying to cast them into date fields and then get the average days that it took to issue the permit.  When I run the script that I have so far, I get an error:  Test execution error: Execution error - Invalid parameter. Verify test data.

 

Here is the script I have so far:

var portal_ = Portal('https://arcgis.com');
var fs = FeatureSetByPortalItem(portal_,
 'Item ID', 0,['*'], false);

for (var f in fs) {
    var date_Applied = f["Applied_Date"];
    var date_Issued = f['Issued_Date'];
    var startDate = DateOnly(date_Applied);
    var endDate = DateOnly(date_Issued);
    var differences = DateDiff(startDate, endDate, 'days');
    var total = Sum(differences);
    var totalrows = Count(differences);
    var avg = total/totalrows;}

    return avg

 

 

Any help is greatly appreciated.

 

0 Kudos
1 Solution

Accepted Solutions
KenBuja
MVP Esteemed Contributor

This has some more error checking in it (making sure the dates aren't null and has three numbers separated by "/"). I also fixed an error in line 26 that used the wrong variable name. And I reversed the dates in the line 17.

function StringToDate(input) {
  if (IsEmpty(input)) return null;
  var arr = Split(input, "/");
  if (Count(arr) != 3) return null;
  for (var i of arr) {
    if (IsNan(Number(i))) return null;
  }
  var theDate = DateOnly(arr[2], arr[0] - 1, arr[1]);
  iif(IsNan(theDate), null, theDate);
}
var total;

for (var f in fs) {
  var date_Applied = StringToDate(f["Applied_Date"]);
  var date_Issued = StringToDate(f["Issued_Date"]);
  if (IsEmpty(date_Applied) || IsEmpty(date_Issued)) continue;
  var differences = DateDiff(date_Issued, date_Applied, "days");
  total += Sum(differences);
}
var totalrows = Count(fs);
var avg = iif(totalrows == 0, 0, Floor(total / totalrows));

return FeatureSet(
  {
    fields: [{ name: "Average", type: "esriFieldTypeInteger" }],
    features: [{ attributes: { Average: avg } }]
  }
);

View solution in original post

6 Replies
KenBuja
MVP Esteemed Contributor

When posting code, please use the "Insert/edit code sample" button. It make it easier to read your code and refer to specific lines.

There are a couple of things going on with your code. You have to convert the text to a valid date. Your current code will return a null. The Count function works on arrays, not values, which is what's throwing the error. You want to get the running total of differences and calculate the average outside the loop. You'll have to return a FeatureSet in a Data Expression for an Indicator.

Most of these were covered in my reply to your previous post.

Give this a try

var portal_ = Portal("https://arcgis.com");
var fs = FeatureSetByPortalItem(portal_, "Item ID", 0, ["*"], false);

function StringToDate(input) {
  var arr = Split(input, "/");
  return Date(arr[2], arr[0] - 1, arr[1]);
}
var total;

for (var f in fs) {
  var date_Applied = f["Applied_Date"];
  var date_Issued = f["Issued_Date"];
  var startDate = DateOnly(StringToDate(date_Applied));
  var endDate = DateOnly(StringToDate(date_Issued));
  var differences = DateDiff(startDate, endDate, "days");
  total += Sum(differences);
}
var totalrows = Count(fs);
var avg = iif(totalrows == 0, 0, Floor(total / totalrows))

return FeatureSet(
  {
    fields: [{ name: "Average", type: "esriFieldTypeInteger" }],
    features: [{ attributes: { Days: avg } }]
  }
);

 

GaryMorris
Regular Contributor

Thank you for your help and thank you for the tip, I didn't know about the "Insert/Edit Code Sample" function.  I think I am close to getting it, I'm now getting:  Test execution error: Compilation error - Line : 21, 14: Function not found. Verify test data.

var portal_ = Portal('https://arcgis.com');
var fs = FeatureSetByPortalItem(portal_,
 'Item ID', 0,['*'], false);

 function StringToDate(input)  {
  var arr = Split(input, "/");
  return Date(arr[2], arr[0] - 1, arr [1]);
 }

var total;

for (var f in fs) {
    var date_Applied = f["Applied_Date"];
    var date_Issued = f['Issued_Date'];
    var startDate = DateOnly(StringToDate(date_Applied));
    var endDate = DateOnly(StringToDate(date_Issued));
    var differences = DateDiff(startDate, endDate, "days");
    total += Sum(differences);
}
    var totalrows = Count(fs);
    var avg = iff(totalrows == 0, 0, Floor(total / totalrows))

return FeatureSet(
  {
    fields: [{name:  "Average", type: "esriFieldTypeInteger"}],
    features: [{ attributes:  {Days: avg} }]
  
  }
);

 

0 Kudos
KenBuja
MVP Esteemed Contributor

You have a typo in line 21 ("iff" instead of "iif")

var avg = iif(totalrows == 0, 0, Floor(total / totalrows))
GaryMorris
Regular Contributor

Thanks, now it is giving me:  Test execution error: Execution error - Out of bounds. Verify test data.

I thought that might be because some of rows in the field that contains the issued date are blank, so I filtered them out, but it is still giving me the same error.

Any idea why?

 
 
0 Kudos
KenBuja
MVP Esteemed Contributor

This has some more error checking in it (making sure the dates aren't null and has three numbers separated by "/"). I also fixed an error in line 26 that used the wrong variable name. And I reversed the dates in the line 17.

function StringToDate(input) {
  if (IsEmpty(input)) return null;
  var arr = Split(input, "/");
  if (Count(arr) != 3) return null;
  for (var i of arr) {
    if (IsNan(Number(i))) return null;
  }
  var theDate = DateOnly(arr[2], arr[0] - 1, arr[1]);
  iif(IsNan(theDate), null, theDate);
}
var total;

for (var f in fs) {
  var date_Applied = StringToDate(f["Applied_Date"]);
  var date_Issued = StringToDate(f["Issued_Date"]);
  if (IsEmpty(date_Applied) || IsEmpty(date_Issued)) continue;
  var differences = DateDiff(date_Issued, date_Applied, "days");
  total += Sum(differences);
}
var totalrows = Count(fs);
var avg = iif(totalrows == 0, 0, Floor(total / totalrows));

return FeatureSet(
  {
    fields: [{ name: "Average", type: "esriFieldTypeInteger" }],
    features: [{ attributes: { Average: avg } }]
  }
);
GaryMorris
Regular Contributor

Thank you so much for your help!  This works!

0 Kudos