Learn How to Add Up Values in a For-Each Loop

preview_player
Показать описание
In this video we are going through a couple of concepts.

First we will learn how to count how many items are returning when you do a get items or send a get request over the http connector to SharePoint. This is usefull to validate if the request is sending any items at all and if yes to see how many. It can be used in varius occasions.

Second we will learn how to add values from each loop run with each other and get a total sum value ate the end. For example if the loop is going through 9 items and you want to add a value that is in each of those items with each other. This is super usefull for many use cases.

To make this video a bit more helpful, I am showcasing this with help of Microsoft Forms and am trying to figure out how to calculate the average time needed for completing this form.

Before moving to the calculations we initialize 2 variables of type float, one for the duration of each form and one for the total. This is very important for saving the subtotal after each run of the loop.

Expression for calculating the number of items:
length(outputs('Parse_JSON_-_Extract_data_from_HTTP_Request')?['body']?['value'])

Then the difference between the start and end time for the submition of the form are calculated and the timestamp is converted into a float number so that it can be used in the calculation later in the flow.

Expression for the time difference:
dateDifference(item()?['startDate'],item()?['submitDate'])

Expression for the conversion from timestamp (string) to seconds (float):
add(mul(3600, int(substring(outputs('Compose_-_Calculate_the_time_for_each_response'), 0, 2))), add(mul(60, int(substring(outputs('Compose_-_Calculate_the_time_for_each_response'), 3, 2))), float(substring(outputs('Compose_-_Calculate_the_time_for_each_response'), 6, 9))))

Then we set the calculated value from the previous expression in the variable for each run and add it to the variable with the total seconds.

Expression for adding the values in the variable:
add(variables('varTotalSeconds'),outputs('Compose_-_Convert_from_timestamp_to_seconds'))

At the end, outside of the loop the outcome of the total seconds is divided in a compose action by 60 to get how many minutes it took in total and then divided by the number of items returned:

div(div(variables('varTotalSeconds'),60),outputs('Compose_-_Calculate_the_number_of_items_in_the_HTTP_Response'))

Thanks for watching!

Feel Free to follow me on other social media platforms as well:

Рекомендации по теме
Комментарии
Автор

Hi. What is the difference between "For each" and "Apply for each"? And from where we can get this "For each" option?

KomailButt