I am attempting to calculate the average completion time for a group of activities in an indicator element for a dashboard. I have a filtered dataset from our asset management system (Cityworks) in AGOL and have fields that report the start_time/completion_time. I want to calculate the average time it took to complete all the activities in the dataset. I referenced the solution found here, but keep getting errors that I cannot resolve. Here is what I have tested so far, unsuccessfully.
When I test this within the Dashboard element, I get the following error(s).
The post you referenced uses that code in a data expression to create a FeatureSet, which is used in the indicator. The profile for this functionality contains the Data access bundle, which includes the Portal and FeatureSet functions. It looks like there's a missing bundle in the documentation, since you can use the FeatureSetByPortalItem function in a data expression also.
However, you're using this code in the advanced formatting section, which doesn't include the Data access bundle in its formatting profile.
Can you post your code using the Insert/Edit code sample button? Trying to decipher code in an image is too difficult, especially when attempting to test your code.
Here is the code...
// Calculate how many seconds it took
var dateOpened = $datapoint["DateTimeInit"];
var dateCleared = $datapoint["DateTimeClosed"];
var secondsToRespond = DateDiff(dateCleared, dateOpened, 'seconds');
// Function to format elapsed time
function formatElapsedTime(numSeconds, type) {
if (IsEmpty(type)) { type = 1 };
if (TypeOf(numSeconds) != 'Number' || IsEmpty(numSeconds)) { return '' };
numSeconds = Floor(Abs(numSeconds),0);
var numMinutes = Floor(numSeconds/60,0);
var numHours = Floor(numSeconds/(60*60),0);
var numDays = Floor(numSeconds/(60*60*24),0);
//Type 1 will format the time to use short-hand labels
if (type == 1) {
var elapsedTime = When(
numMinutes < 1, numSeconds + 's',
numHours < 1, numMinutes + 'm',
numDays < 1, numHours + 'h ',
numDays + ' days'
);
return elapsedTime;
}
//Type 2 will format the time to a clock-format (H:mm:ss)
if (type == 2) {
var relativeDate = DateAdd(Date(0,0,0,0,0,0), numSeconds, 'seconds');
var elapsedTime = When(
numHours < 1, Text(relativeDate,'mm:ss'),
numDays < 1, Text(relativeDate,'HH:mm:ss'),
numDays + ' days ' + Text(relativeDate,'HH:ss')
);
return elapsedTime;
}
}
var responseTime = formatElapsedTime(secondsToRespond, 1);
return {
textColor: '',
backgroundColor: '',
separatorColor:'',
selectionColor: '',
selectionTextColor: '',
attributes: {
responseTime: responseTime
}
}
The advanced formatting tool is designed to format individual values (an indicator result or an item in a list), not calculating values from an entire featureset. What you'll need to do write a data expression for the indicator that will calculate that average. You would use your formatting function in the indicator's advanced formatting section. Your data expression would look something like this
var fs = FeatureSetByPortalItem(
Portal("yourPortalUrl"),
"yourItemId",
0,
["DateTimeInit", "DateTimeClosed"],
false
);
var responseTime = 0;
var counter = 0;
for (var f in fs) {
var dateOpened = f.DateTimeInit;
var dateCleared = f.DateTimeClosed;
if (!IsEmpty(dateOpened) && !IsEmpty(dateCleared)) {
responseTime += DateDiff(dateCleared, dateOpened, "seconds");
counter += 1;
}
}
return FeatureSet(
{
fields: [{ name: "Average", type: "esriFieldTypeInteger" }],
features: [{ attributes: { Average: Round(responseTime / counter) } }]
}
);