Post navigation

Identify Duplicates Using Power Query

Some time ago I got an email from Alex asking me if there was a way to identify duplicates using Power Query, but without removing non-duplicate records in the process. This post explores how to do that.

Background

Suppose someone has given you a list like the one shown below (which you can download here if you’d like to follow along):

While multiple brands are okay here, we need a list that shows only unique SKU numbers. While the list provided to you was supposed to be duplicate free, you’re not 100% sure that it actually is. While it would be easy to just hit the SKU column with the Remove Duplicates function, you don’t want to do that. Instead you’d like to indentify which records have duplicate entries in the list.

So how do we do this?

Naturally, there will be a few different ways to do this. I’m carving off one method that is the easiest to replicate via the user interface…

Step 1: Link to the Data

Of course we’ll start by pulling the data in to Power Query

Click anywhere in the Products Table

Create a new query –> From Table

The data will be loaded in to Power Query, and you’ll see two steps in the Applied Steps window:

Source (pointing to your source data)

Changed Type (setting the data types for the columns)

This might seem like an odd step right now, but we’re going to add a Index column to this table as well. The reason will become apparent later, but for now:

To to Add Column –> Add Index Column –> From 0

Your data should now look like this:

Now we need to figure out how to flag any repeating SKU as a duplicate.

Step 2: Indentify Duplicates via Grouping Rows

The trick here is to use the Group By feature in Power Query, while preserving the relevant matching records.

Next, click the + to the right of the “New Column Name” section to add another detail row. Set it up as follows:

New column name: Duplicates –> All Rows

When you’re done, the dialog should look like this:

And upon clicking OK, the results will show that there are, indeed, items that show up more than once:

Let’s tweak this a bit, and subtract 1 from each value. That would give us a truer representation as to how many duplicates there are.

Select the Duplicates column –> Transform –> Subtract –> 1

Resulting in the following:

Much better. We’re now seeing that SKU 510010 appears to have 1 duplicate entry in the data set.

But there is still an issue here. When we grouped our records, we lost both the Brand names column, but also any duplicate records. Since the whole point of this exercise was to Indentify Duplicates but not remove the duplicate records, we’re still not in a good place.

Step 3: Identify Duplicates and Show Duplicate Records

Let’s fix this. Remember how we added a new step to show “All Rows” for the ProductDetail column? That step gave us the ability to do something pretty cool… it gave us the ability to get back all the lost records and product detail information we’re currently missing.

Click the Expand button at the top right of the ProductDetail column

Uncheck the SKU Number option (as we already have it)

Uncheck the option to “Use original column name as prefix”

As you can see, this will bring back all the details we lost earlier.

Step 4: Final Cleanup

But hang on a second. Let’s look at this output a bit more closely…

Notice, that it re-sorted the data. That’s not exactly a desirable outcome, as we are trying to flag duplicates for a reason. Maybe we want to know where they exist in an inventory count or we have some other reason for wanting to preserve the original sort order of our data. It’s for this reason that we added the Index column earlier. That came through with the All Rows step, so let’s put our data back into its original order.

Click the drop down arrow on the Index column –> Sort Ascending

Right click the Index column –> Remove

And we can now finalize the query:

Rename the query to ShowDuplicates

Go to Home –> Close & Load

Step 5: Make the Duplicates Obvious

With the data now in an Excel table, we can make the duplicates even more obvious by applying some conditional formatting to the table. To do this:

Select all the values in the Duplicates column of the table

Go to Home –> Conditional Formatting –> Data Bars –> Choose a colour

I chose blue data bars, which makes the data look like this:

Conclusion

Our goal is now complete. We were able to identify duplicates and flag them without removing non-duplicate items. In addition, we have preserved the original order of the data in case that was important to us for any reason.

13 thoughts on “Identify Duplicates Using Power Query”

Ken,
Nice response. After leaving my question for you I had developed a Power Query solution for identifying duplicates which I was planning to share back. It meets my needs exactly, but is VERY clumsy - your solution is far more elegant (I had not yet learned about how to use "All Rows" in a Group column - but NOW I get it).
One of the things I was trying to do was to not only identify all the dupes, but to identify ONE of the dupes as the primary (e.g. the most recent record, or the oldest record, from a specific source, etc.) for subsequent analysis.

I hope to apply your approach to my specific problem and share-back the result. Thanks for a great post.

Ken, if you've tried the new Show Duplicates button in the March 2016 Power BI Desktop, it is just using a multistep Power Query in one line. Pretty neat, and could be modified as a custom function for regular PQ. It works for more than one column selected. Here is a prettified version:

Ken - I went and read the March 2016 release notes and found that it is a toggle button tied to the Remove Duplicates button - this is great and faster - thank you!

If you have a comment or question about the blog post content, please feel free to post it here. If you need help adapting this solution to your own needs, please post in our free help forum. Cancel reply

Your email address will not be published. Required fields are marked *