2

I have a calculated column for ticket response time Want to get the average response time for all tickets

average response time = (sum of #response time)/ #of tickets

call: https://siteurl/_api/web/lists/getbytitle('listName')Items/?$select=Response_x0020_Time

function successCall(data){
  var myListData = data.d.results.length

  $('#divAction').html(myListData)
}

I am not sure how to extract the data to compute the average

Any help would be greatly appreciated!

3

First, you're missing a slash between getbytitle('listname') and Items, should be:

<siteurl>/_api/web/lists/getbytitle('<listname>')/Items/?$select=Title,Response_x0020_Time

Now I'm going to have to make some assumptions, because you didn't provide any information about the field types upon which the calculation is based, or the expected type of the calculation.

I'm going to assume that Response_x0020_Time is the calculation of an end date minus a start data. That would make the result a float (where 1 equals one day). But it won't come back as a float in the response JSON, because it's a calculated field which the REST api will just return it as a string, so we'll need to convert that to a float.

Also, because of the way you've written your success function, I assume you're using jQuery's $.ajax to perform the REST service call.

Given that, you would process the response something like:

var sum = 0;

// data.responseJSON.d.results is an array of items returned
for(var i=0; i<data.responseJSON.d.results.length; i++) {

    // get the current item
    var current = data.responseJSON.d.results[i];

    // convert it's response time to a float and add it to sum
    sum += parseFloat(current["Response_x0020_Time"]);
}

// now divide sum by the length of the array
var average = sum / data.responseJSON.d.results.length;

// shove it into the div
$('#divAction').html(average)
  • Thanks for the help.....for some reason it wont display on my page and I get no console.log errors. Here is my code: – Ben Segarra Feb 15 at 15:12
  • function retrieveListItems() { var fullUrl = "https:/site/_api/web/lists/getbytitle('listTitle')/Items?$select=Title,Response_x0020_Time"; $.ajax({ url: fullUrl, type: "GET", headers: { "accept": "application/json;odata=verbose", "content-type": "application/json;odata=verbose", }, success: onQuerySucceeded, error: onQueryFailed }); } – Ben Segarra Feb 15 at 15:17
  • function onQuerySucceeded(data){ var sum = 0; // data.responseJSON.d.results is an array of items returned for(var i=0; i<data.responseJSON.d.results.length; i++) { var current = data.responseJSON.d.results[i]; // convert it's response time to a float and add it to sum sum += parseFloat(current["Response_x0020_Time"]); } var average = sum / data.responseJSON.d.results.length; // shove it into the div $('#divListItems').html(average) function onQueryFailed(sender, args) { alert('Error!'); } – Ben Segarra Feb 15 at 15:17
  • Formula of my Cal column =24*([Resolved Date/Time]-[Received Date/Time]) – Ben Segarra Feb 15 at 15:21
  • Try doing console.log(data) on the first line of the success callback and see what the structure looks like. – Joe McShea Feb 15 at 15:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.