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.
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 identify 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.
NOTE: We cover the Grouping feature in Chapter 14 of M is for Data Monkey.
Here’s how we do this:
- Go to Transform –> Group By
- Set your Group By Options as follows:
- Group By: SKU Number
- New column name: Duplicates –> Count Rows
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 Identify 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:
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.