Skip to main content

Notifications

Power Automate - General Discussion
Answered

Add total row in HTML Table

(1) ShareShare
ReportReport
Posted on by 33

Hi All,

 

I have flow to create HTML Table from Initialize Variable Output.

Is there any way to create total/sum/average at the end of HTML Row?

 Below is my "Initial Variable" output :

_________________________________________________________________________________

[
  {
    "Department""AEBG",
    "Yesterday""20",
    "CompletionRatio1""95.24",
    "Today""19",
    "CompletionRatio2""90.48"
  },
  {
    "Department""CBG",
    "Yesterday""921",
    "CompletionRatio1""87.55",
    "Today""718",
    "CompletionRatio2""68.25"
  },
  {
    "Department""CMC",
    "Yesterday""67",
    "CompletionRatio1""100.00",
    "Today""46",
    "CompletionRatio2""68.66"
  },
  {
    "Department""CRP. SALES",
    "Yesterday""12",
    "CompletionRatio1""92.31",
    "Today""5",
    "CompletionRatio2""38.46"
  },
  {
    "Department""FINANCE",
    "Yesterday""13",
    "CompletionRatio1""100.00",
    "Today""12",
    "CompletionRatio2""92.31"
  },
  {
    "Department""HMIBG",
    "Yesterday""392",
    "CompletionRatio1""85.22",
    "Today""285",
    "CompletionRatio2""61.96"
  },
  {
    "Department""MD",
    "Yesterday""2",
    "CompletionRatio1""100.00",
    "Today""1",
    "CompletionRatio2""50.00"
  },
  {
    "Department""MED",
    "Yesterday""39",
    "CompletionRatio1""100.00",
    "Today""33",
    "CompletionRatio2""84.62"
  },
  {
    "Department""PLD",
    "Yesterday""86",
    "CompletionRatio1""96.63",
    "Today""78",
    "CompletionRatio2""87.64"
  },
  {
    "Department""QEAD",
    "Yesterday""28",
    "CompletionRatio1""100.00",
    "Today""19",
    "CompletionRatio2""67.86"
  }
]

_________________________________________________________________________________

 

and HTML Output as below :

Falhuddin_0-1649987703088.png

Last row of this HTML Table is the result that i want. (Column2 - Sum, Column3 - Average, Column4 - Sum and Column5 - Average)

 

Thank You.

  • Verified answer
    Rainforss Profile Picture
    Rainforss 13 on at
    Re: Add total row in HTML Table

    Hi Falhuddin,

     

    We need to do a lot of data operations here so math content warning 😉

     

    Let me present the flow logic first:

    Rainforss_0-1650257953232.png

    I am not certain how your data is gathered by I initialized a variable to store the array of data you pasted here. The manual trigger is just for me to test the flow so don't worry about it.

     

    1. Initialize 4 more variables to store the sum of yesterday column, the sum of completionratio1 column, the sum of today column and the sum of completionratio2 column, the starting values are zeros.
    2. Applying to each dataset of the array, we need to add the corresponding data to each initialized variables.
    3. Once all values are added up, we need to get the total number of datasets of the array to calculate averages.
    4. For the average calculation, we divide the sum by the number of datasets. Then we append an additional dataset containing the sum and averaged values to the array.
    5. Using the finalized array, we create an HTML table out of it.

      Here is the test run result if you want to confirm:Rainforss_1-1650258350039.png

      Now for the details, the initialized data variable is simply the array you pasted:Rainforss_2-1650258412280.png

      For the other 4 variables, initialize the sum of yesterday and sum of today as integers of value 0. Initialize the sum of ratio1 and ratio2 as floats (meaning they are decimal numbers) of value 0Rainforss_5-1650258598727.png

      For the steps within 'Apply to each row', we need 4 calculation steps to aggregate the values for those 4 variables, the action we are using here is 'Increment variable':Rainforss_3-1650258513687.png

       

      When adding to the sum of yesterday or the sum of today, you need the following expressions, respectively:

    int(items('Apply_to_each_row')?['Yesterday'])

    int(items('Apply_to_each_row')?['Today'])

     

    Notice that we are using a math express 'int()'. Because the values we have in the datasets are all string values, we need to convert them into proper data formats to be able to do calculations. For values under "Yesterday" and "Today" columns, we need int(). For values under those two percentage columns, we need float()  to transform the string values into decimal values, as follows:

     

    float(items('Apply_to_each_row')?['CompletionRatio1'])

    float(items('Apply_to_each_row')?['CompletionRatio2']) 

     

    Rainforss_7-1650258947514.png

    Once the summations are complete, we need to get the total number of datasets available in the array. For this step, you can initialize another variable and store the value or simply use the action 'Compose':Rainforss_8-1650259062306.png

    The expression is pretty simple. We are using the 'length()' function to get the number of items in an array:

     

    length(variables('testArr'))

     

    Notice that variables('testArr') is pointing to the data variable I initialized, you might need to modify it for your case.

    Now with the length available, we can do a final calculation and append the calculated values to the original data array. Action used here is 'Append to array variable':Rainforss_9-1650259231288.png

    For the calculated variables, here are the expressions:

    string(variables('yesterdaySum'))

    concat(string(div(variables('ratio1'),outputs('get_total_number_of_rows'))),'%')

     

     

    string(variables('todaySum'))

    concat(string(div(variables('ratio2'),outputs('get_total_number_of_rows'))),'%')

     

    For sum of yesterday and today, we just need to convert the summed integer value back to string so it is consistent with the existing dataset values.

    For averaged percentages, first we need to use 'div()' to divide the summed percentage by the number of datasets. Then we convert the calculated decimal value to string. After that, we use 'concat()' function to concatenate the string value with the percentage sign.

     

    Once the array is finalized, we create an HTML table out of it in the last step:

    Rainforss_10-1650259560586.png

     

    The logic is pretty straightforward but we need to do some calculations and data type conversions here using some Power Automate functions. If you are interested in those functions, here is a document you can dig into (it is super long!): Power Automate Functions Reference 

     

    That was a lot of typing!

     

    Hope this helps 🙂

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Microsoft Kickstarter Events…

Register for Microsoft Kickstarter Events…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 145,636

#2
RandyHayes Profile Picture

RandyHayes 76,287

#3
Pstork1 Profile Picture

Pstork1 64,942

Leaderboard