GlideAggregate AVG not calculating as expected.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2024 08:07 AM - edited 03-05-2024 08:13 AM
Hello,
I'm trying to (in the PDI), calculate the average of 3 SAFe Sprint's (sn_safe_sprint) Completed Points. However, when I use getAggregate('AVG', 'actual_points'), it only prints the value of the completed points, not the average. For example,
I want to calculate the average of the first three sprints. I'm expecting an output of (6+16+16)/3 = 12.66. However, I'm only getting the values 6, 16, 16; the average is not calculating.
Code:
(function executeRule(current, previous /*null when async*/) {
var ga = new GlideAggregate('sn_safe_sprint');
ga.addQuery('state', 3); //Complete
ga.orderByDesc('sys_updated_on');
ga.setLimit(2); //Select three completed sprints during query.
ga.addAggregate('AVG', 'actual_points'); //Calculate the 'Completed Points' column.
ga.query();
while(ga.next()){
gs.info(ga.getAggregate('AVG', 'actual_points')); // <-- Here, I'm expecting the average of the three sprints' completed points.
}
})(current, previous);
How may I get the average to print?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2024 11:30 PM
This is not working either. I'm running into my initial issue. It prints the values, but does not calculate the average of the values.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-05-2024 11:40 PM
Hello @TerryC03 ,
Please give a try to the modified code below and see how it works for you.
(function executeRule(current, previous /*null when async*/) {
var ga = new GlideAggregate('sn_safe_sprint');
ga.addQuery('state', 3); // Complete
ga.orderBy('sprint_number'); // Order by an appropriate field representing sprint order
ga.setLimit(3); // Select the first three completed sprints during the query
ga.addAggregate('AVG', 'actual_points'); // Calculate the 'Completed Points' column
ga.query();
if (ga.next()) {
gs.info('Average Completed Points: ' + ga.getAggregate('AVG', 'actual_points'));
}
})(current, previous);
Please Mark ✅Correct if this solves your query and also mark 👍Helpful if you find my response worthy based on the impact.
Thanks & Regards,
Aniket.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2024 12:05 AM
Hi @TerryC03 ,
This below script will definitely work. Try it
You should add SetGroup(false) and SetOrder(false) in the Query.
ga.addQuery('state', 3); // Complete
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
03-06-2024 01:12 AM
Hi @TerryC03
You should be facing the below error.
Skip invalid GroupBy condition: ORDERBYsys_update_on
Since you're calculating Average, it requires a group of records to be able to calculate it (groupBy).
Your script could look like below. The script will order (A - Z) on the actual_points field (aggregate field).
(function executeRule(current, previous /*null when async*/) {
var ga = new GlideAggregate('sn_safe_sprint');
ga.addQuery('state', 3);
ga.orderBy('state'); //group by state as your query by state
ga.setAggregateWindow(0,2); //limit 2
ga.addAggregate('AVG', 'actual_points');
ga.query();
while(ga.next()){
gs.info(ga.getAggregate('AVG', 'actual_points'));
}
})(current, previous);
If your case is required orderByDesc on updated time like below, you can consider to build a function to collect the record's sys_id with orderByDesc first. Then use the sys_id in result for the query of the GlideAggregate.
ga.orderByDesc('sys_updated_on');
Cheers,
Tai Vu