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

Поділитися
Вставка
  • Опубліковано 15 вер 2024
  • 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.
    After getting the data from all the submitted forms at once by using another solution I showcased a while ago ( • 🧾Microsoft Forms - Get... ) we then count the items in the JSON response to see how many forms have been submitted in total. This is necessary for the calculation of the average time needed.
    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!
    If you want to learn more about Power Apps in cooperation with SharePoint, you are welcome to follow the channel and subscribe to the playlist here: / @enea
    Feel Free to follow me on other social media platforms as well:
    linktr.ee/Enea...
    Buy me a pizza 🍕- bmc.link/eneal...

КОМЕНТАРІ • 3

  • @KomailButt
    @KomailButt 2 місяці тому +1

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

    • @Enea
      @Enea  2 місяці тому

      Its the same mate

    • @KomailButt
      @KomailButt 2 місяці тому

      @@Enea I observed it different in one of my project. I was reading pdf files using AI builder (reading table from them) and storing the result like (adding a row into a table). When I used, Apply for each, it was just mixing the rows from different pdfs table, and when I used For each, it literally consider one document at a time, extract all rows from it and add all rows in excel table and then move to next pdf.
      The problem is now I forgot like from where I took For each in Power Automate.