Get Unique Values from SharePoint List, group by the items and send notification by using Power Automate
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
Comments
-
Get Unique Values from SharePoint List, group by the items and send notification by using Power Auto
How would you filter for greater than 1?
-
Get Unique Values from SharePoint List, group by the items and send notification by using Power Auto
Following solution worked for me to get EACH ITEM from the UNION Array.
item()?['YOUR_KEY']
-
Get Unique Values from SharePoint List, group by the items and send notification by using Power Auto
Hi @rsaikrishna ,
Same as the problem listed by the people above, I cannot seem to get the Apply to Each action working to compare the unique list of values to get 'DeptName' or the equivalent:
If you see this, would be great if you could let me know how to resolve this.
Thanks, Max
-
Get Unique Values from SharePoint List, group by the items and send notification by using Power Auto
I am facing the same issue
-
Get Unique Values from SharePoint List, group by the items and send notification by using Power Auto
@Anonymous
Hi Josiah,
I've been doing battle with that bit myself. Finally cracked it (for me anyway). Try setting the Inputs for the Compose in Step 4 to the following expression
items('Apply_to_each')?['DeptName']
I can't remember exactly (it was a few hours ago and it's now 3am here) but I think instead of ['DeptName'] I had to do [Value] and it then picked it up.
@rsaikrishna Thank you so much for putting this out there! It's saved me a lot of time not having to develop an Azure Function or similar to do this sort of thing (even with the quirk in Step 4). I now have an adapted version which parses a large list of "active" and "inactive" items and, using a second Apply_to_each, filters a huge list sending reminder emails to respective users (dynamically based on the items) containing only the currently active list items that belong to them 🙂
-
Get Unique Values from SharePoint List, group by the items and send notification by using Power Auto
Hi Krishna,
Currently ran into a problem trying to follow your guide. At step 4, I'm unable to get DeptName using the compose component in the apply to each loop. Instead, I'm only getting output which returns the entire union array. Would like to check if I'm missing anything.
Regards,
Josiah
*This post is locked for comments