cancel
Showing results for 
Search instead for 
Did you mean: 
rsaikrishna

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

Comments
Anonymous

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 

@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 🙂

I am facing the same issue

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:

AthenaPM_0-1619458354933.png

If you see this, would be great if you could let me know how to resolve this.

 

Thanks, Max

Following solution worked for me to get EACH ITEM from the UNION Array.

item()?['YOUR_KEY']

 

Sam_Dorathi_0-1621824334443.png

 

How would you filter for greater than 1?

 

inquisitor_0-1670965655130.png

 

About the Author
  • 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.
  • I am a Microsoft Business Applications MVP and a Senior Manager at EY. I am a technology enthusiast and problem solver. I work/speak/blog/Vlog on Microsoft technology, including Office 365, Power Apps, Power Automate, SharePoint, and Teams Etc. I am helping global clients on Power Platform adoption and empowering them with Power Platform possibilities, capabilities, and easiness. I am a leader of the Houston Power Platform User Group and Power Automate community superuser. I love traveling , exploring new places, and meeting people from different cultures.
  • Read more about me and my achievements at: https://ganeshsanapblogs.wordpress.com/about MCT | SharePoint, Microsoft 365 and Power Platform Consultant | Contributor on SharePoint StackExchange, MSFT Techcommunity
  • Encodian Owner / Founder - Ex Microsoft Consulting Services - Architect / Developer - 20 years in SharePoint - PowerPlatform Fan
  • Founder of SKILLFUL SARDINE, a company focused on productivity and the Power Platform. You can find me on LinkedIn: https://linkedin.com/in/manueltgomes and twitter http://twitter.com/manueltgomes. I also write at https://www.manueltgomes.com, so if you want some Power Automate, SharePoint or Power Apps content I'm your guy 🙂
  • I am the Owner/Principal Architect at Don't Pa..Panic Consulting. I've been working in the information technology industry for over 30 years, and have played key roles in several enterprise SharePoint architectural design review, Intranet deployment, application development, and migration projects. I've been a Microsoft Most Valuable Professional (MVP) 15 consecutive years and am also a Microsoft Certified SharePoint Masters (MCSM) since 2013.
  • Big fan of Power Platform technologies and implemented many solutions.
  • Passionate #Programmer #SharePoint #SPFx #M365 #Power Platform| Microsoft MVP | SharePoint StackOverflow, Github, PnP contributor
  • Web site – https://kamdaryash.wordpress.com Youtube channel - https://www.youtube.com/channel/UCM149rFkLNgerSvgDVeYTZQ/