Select to view content in your preferred language

Calculate average time to complete a task

156
5
Tuesday
Labels (1)
JoshThompson
Occasional Contributor

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.

JoshThompson_0-1749591647954.png

When I test this within the Dashboard element, I get the following error(s).

JoshThompson_1-1749591682997.png

 

0 Kudos
5 Replies
KenBuja
MVP Esteemed Contributor

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.

JoshThompson
Occasional Contributor
Thank you for the feedback. As an Arcade novice, that did help my overall understanding. 
 
I am restarting the process using the advanced formatting section in the Indicator because I continue to get source errors when attempting to call the feature service in a Data Expression.
I have created a List element that displays each individual activity and the time it took to complete the process using the guidance from this article and attached the code and screenshot. I wanted to extend that functionality to average those individual completion times in an Indicator using the advanced formatting section. Is this possible using this method? 
0 Kudos
KenBuja
MVP Esteemed Contributor

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.

0 Kudos
JoshThompson
Occasional Contributor

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
  }
}
0 Kudos
KenBuja
MVP Esteemed Contributor

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) } }]
  }
);

 

0 Kudos