Data - How to enforce unique values (or prevent duplicate values) in one or more columns

A common task is to enforce unique values (or to prevent duplicate records). To demonstrate, this post takes the example of a SharePoint list of employee details. The details in this list include include name, surname, email address, and other employee details.

As a starting point for this post, we'll build an auto generated app that's based on this SharePoint list. As this post progresses, we'll walk through how to enforce unique email addresses, and a unique combination of firstname and surname values for each record in the list.

Best practice - Enforcing uniqueness at the data source

Whenever we want to avoid duplicates, a best practice is to enforce uniqueness at the data source level, in addition to any checks that we make in Power Apps. This prevents users from creating duplicating records outside of Power Apps.

To carry out this task in SharePoint, we select the settings of a column and click the 'enforce unqiue values' radio button.



A notable limitation of SharePoint is that it's not possible to define a unique constraint that spans multiple columns. However, SQL Server and Dataverse databases do not have this limitation. In SQL Server, we can create unique constraints and with Dataverse, we can define alternate keys to specify uniqueness across multiple columns.

How Power Apps deals with duplicate values  

When we define unique columns at the data source, Power Apps displays an error message when a user attempts to add a duplicate record.

As an example, here's the error that appears when a user attempts to create or update a record that contains a duplicate email address.


Although the SharePoint has prevented the user from creating a duplicate value, there are two issues with this built in behaviour. The first is the error message is not user friendly, and the second is that the form clears the duplicate value that the user enters before displaying the error.

Detecting duplicate values from Power Apps when adding a record

To better handle this behaviour, we can write formula to detect the presense of a duplicate record before the call to SubmitForm (or Patch).

The formula beneath highlights the basic syntax that we can add to the submit button to detect a duplicate email address when a user attempts to create a new record.:

If(IsBlank(LookUp(Employee, Email=DataCardValue17.Text)),
SubmitForm(EditForm1),
Notify("Email must be unique",NotificationType.Error);
SetFocus(DataCardValue17)
)

This formula calls the LookUp function to look up record in the Employee list that matches the email address that the user enters. Here, DataCardValue17 refers to the text input control in the edit form. If LookUp returns blank, there is no duplicate record and the formula calls the SubmitForm function to add the record.

Otherwise, the formula calls the Notify function to alert the user. Next, it calls the SetFocus function to set the focus to the email text input control.

The screenshot beneath highlights the notification that appears when the user enters a duplicate email address.


Enforcing unqiueness accross multiple columns 

To enforce uniqueness across multiple columns, we can adapt our formula to reference the multiple columns in the call to LookUp. Here's the syntax to enforce a unique combination of firstname and surname for each record in the employee list.

If(IsBlank(LookUp(Employee, Firstname=DataCardValue11.Text
And Surname=DataCardValue19.Text))
,
SubmitForm(EditForm1),
Notify("Error - duplicate values detected",NotificationType.Error)
)

Similar to our previous formula, DataCardValue11 refers to the text input control for the first name, and DataCardValue11 refers to the text input control for the surname.

To include multiple rules, we can extend the If condition. As an example, here's the syntax to enforce both a unique email address, and a unique combination of firstname and surname for each record.

If(IsBlank(LookUp(Employee, Email=DataCardValue17.Text))
And
IsBlank(LookUp(Employee, Firstname=DataCardValue11.Text
And Surname=DataCardValue19.Text))
,
SubmitForm(EditForm1),
Notify("Error - duplicate values detected",NotificationType.Error)
)

Detecting duplicate values from Power Apps when creating and editing a record

Finally, it's important to note that when we edit a record, we must prevent the formula from detecting the current record as a duplicate.

With a SharePoint list, the ID field uniquely identifies each record. We can use this to identify whether any duplicate record is genuinely a duplicate, rather than the record that matches the existing record.

To access the SharePoint ID value on a form, we can add a label to the form and set the text property to ThisItem.ID. To hide this label from user, we can set the visible property to false.


To check whether the user is entering a duplicate record, we can retrieve all records from the SharePoint list that match the first name, surname, and email address that user enters. If no matches are found, the record is valid. If a single match is found and the ID matches the ID of the current record, the record is also valid.

Here's the formula that carries out this logic.

With({matchingRecords:Filter(Employee, Email=EditForm1.Updates.Email Or
(Firstname = EditForm1.Updates.Firstname And
Surname = EditForm1.Updates.Surname))}
,
If(IsEmpty(matchingRecords) Or
(CountRows(matchingRecords) = 1 And
First(matchingRecords).ID = Value(lblID.Text)),
SubmitForm(EditForm1),
Notify("Error - duplicate values detected",NotificationType.Error)
)
)


To simplify the formula, note how we refer to the values that the user enters by referencing EditForm1.Updates, as opposed to the text input controls.

When a user now runs our app, this formula will prevent the user from entering a duplicate email address, and a duplicate combination of firstname and surname whilst creating and editing existing records.

Conclusion

A common requirement is to enforce unique values, or to prevent duplicate values in a column (or set of columns).
This post walked through how to accomplish this task by demonstrating how to enforce unique email address values in an employee table, and how to enforce a unique combination of firstname and surname for each record.

Related posts

Data - How to remove trailing comma all rows in a table
February 20, 2025
Data - How to find the common rows from 3 or more collections
October 06, 2024
Data - How to show the distinct rows from 2 data sources or collections
February 26, 2024
Data - How to implement circular rotational date sorting
February 21, 2024
Bug - What to do when the data section of the Power Apps Maker portal doesn't work
June 18, 2023
Data - Combine columns from separate tables into a single table
October 13, 2022
Formula - Transposing/converting rows to columns- an almost impossible task?
September 23, 2021
Data - How to rename field names in a record
July 14, 2021
Data - How to hide duplicate rows in a gallery / show distinct multiple columns in a gallery
July 09, 2021
Data - Retrieving news/forum/blog articles with RSS
June 26, 2021
Data - How to sort by partial numbers in a text field
June 23, 2021
Data - How to return the last record from a table
June 19, 2021
Data - How to create bulk test/dummy records with random values
June 18, 2021
Data - 3 things you should know before using the MySQL or PostgreSQL connectors
May 11, 2021
Data - A walkthrough of how to migrate the data source of an app from Excel to Sharepoint
April 26, 2021
Data - How much mobile data does Power Apps consume? What ways can we minimise this?
March 28, 2021
Data - How to save and retrieve Google calendar entries
March 14, 2021
Data - How to save and retrieve Google contacts
March 10, 2021
SQL - Caution! This is how users can hack shared SQL connections
January 23, 2021
SharePoint – 2 Mistakes to avoid when importing Excel data
January 10, 2021
SQL - Don't let this DateTime bug catch you out!
January 05, 2021
Settings - What's the purpose of the "Explicit Column Selection" Setting?
January 04, 2021
SQL Server for Beginners Part 3 - Installing On-Premises Gateway
January 24, 2019
SQL Server for Beginners Part 2 - Installing Management Studio
January 14, 2019
SQL Server for Beginners Part 1 - Installing SQL Server
January 04, 2019
Searching data–What you need to know about case sensitivity
December 27, 2018
Images - How to create images that can change depending on data
November 09, 2018
Excel - Reasons NOT to use Excel as a data source
September 25, 2018
SharePoint - What you need to know about Filtering Data
September 16, 2018
Formulas - Generating Row Numbers
April 05, 2018