Use Case: There is a SharePoint list "Visitors Information" which tracks the users, department, time in and time out. We would like to send a weekly report on the number users visited each department.
What you will learn from this blog: Power Automate Data Operations to get unique values and grouping items by specific column
SharePoint List Schema:
The above list contains:
Visitor Name : Single line of Text
Department Visiting: Single line of Text
TimeIn: Date and Time
TimeOut : Date and Time
Power Automate
Step 1: Created a Schedule flow which runs every Saturday.
Get Items : Retrieves the visitor information from "VisitorInfo" list who visited during last 7 days.
In the Filter Query, you can see an expression:
TimeIn ge '@{addDays(utcNow(),-7)}' and TimeIn le '@{utcNow()}'
Step 2: Get unique department names
Select Departments: This is Select data operation.
From: value property from Get Items
Map: Mapping the value "Department Visiting" to "DeptName"
Compose Union of Departments: To get unique department names
Use following expression:
union(body('Select_Departments'),body('Select_Departments'))
Step 3: Create string variable
varHtmlTableRows is a string variable to store all the department name and no. of visitors in HTML tags.
Step 4: Loop thru each department to get no. of visitors
Note: We are not reading the SharePoint list for every department name. If we do that, it is a performance hit. We already have all the visitor information from Get Items action in Step 1.
Select an output from previous steps: Outputs is the value from "Compose union of departments" action created in Step 3.
Filter array:
a. value is the value from Get Items action created in Step 1.
b. Comparing "Department Visiting" is equal to Output of "Current Department Name" action.
Append to string variable:
We are creating HTML table row for each department and appending it to the string variable varHtmlTableRows.
Outputs is the value from "Current Department Name" action.
<tr><td>@{outputs('Current_Department_Name')}</td><td align='center'>@{length(body('Filter_array'))}</td></tr>
Step 5:
Checking if varHtmlTableRows is null or empty. If this variable is empty or null, then there are no visitor items.
Compose HTML Style: Using this style while sending email for table.
<style>
table {
border-collapse: collapse;
width: 100%;
}
th, td {
text-align: left;
padding: 8px;
}
tr:nth-child(even){background-color: #f2f2f2}
th {
background-color: #4CAF50;
color: white;
}
</style>
Expanded "Send an email (V2)" action screen shot:
Note: In the body field, the first Outputs is from compose action named as "Compose HTML Style" and second Outputs is from compose action named as "Construct HTML table".
Here is the output email:
Regards
Krishna Rachakonda