cancel
Showing results for 
Search instead for 
Did you mean: 

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.

rsaikrishna_1-1594927723119.png

What you will learn from this blog: Power Automate Data Operations to get unique values and grouping items by specific column

 

SharePoint List Schema:

4.JPG

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.

 

 

1.JPG

 

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

 

rsaikrishna_0-1594926087268.png

 

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.

 

rsaikrishna_1-1594926340174.png

 

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.

 

rsaikrishna_2-1594926559042.png

 

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.

 

rsaikrishna_1-1594927398663.png

 

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:

 

rsaikrishna_2-1594927495620.png

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:

rsaikrishna_0-1594927683107.png

Regards

Krishna Rachakonda

Meet Our Blog Authors
  • Experienced Consultant with a demonstrated history of working in the information technology and services industry. Skilled in Office 365, Azure, SharePoint Online, PowerShell, Nintex, K2, SharePoint Designer workflow automation, PowerApps, Microsoft Flow, PowerShell, Active Directory, Operating Systems, Networking, and JavaScript. Strong consulting professional with a Bachelor of Engineering (B.E.) focused in Information Technology from Mumbai University.
  • Encodian Owner / Founder - Ex Microsoft Consulting Services - Architect / Developer - 20 years in SharePoint - PowerPlatform Fan
  • Cambridge UK Power Platform User Group Leader, Technical evangelist and speaker. Always says yes to coffee! #LetsGetCoffee
  • Passionate #Programmer #SharePoint #SPFx #Office365 #MSFlow | C-sharpCorner MVP | SharePoint StackOverflow, Github, PnP contributor
  • I am building business processes and applications that are easy for users' to stick to, so they can follow and understand them. In overall I transform processes to be more reliable and effortless. I am a proud co-organizer of SharePoint Saturday Warsaw and active community member, blogger and international speaker.