Make duplicate values unique

Glass of Gin

New Member
Joined
Nov 30, 2022
Messages
1
Office Version
  1. 365
Platform
  1. Windows
I have a column with tens of thousands of alphanumeric values. A few hundred of those are duplicates. Some are listed twice, others - more than twice. I need to make those duplicate values unique e.g. by adding -1, -2, etc. to the end of the value. How can I do that?
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
Welcome to the board!

The easiest way: Add a helper column where you count how many times the value has appeared at that point. The formula for this would be something like =COUNTIF(A$2:A2,A2). Make sure the first row of you range is locked ( = the dollar sign before the row number) so that the range expands when you send it down. Then add another column where you join the COUNTIF value with the original value if it's greater than 1. The formula for that would be something like =IF(B2>1;A2&"-"&B2,A2).
If you don't want to use two helper columns you can nest both formulas into a single one. Just replace both B2's in the second formula with the first formula.
 
Upvote 0
With tens of thousands of rows, you might find Power Query a bit quicker and easier - a LOT easier if the data changes regularly - then it would be a simple Refresh All.
Book1
ABCD
1ItemMergedIndexMergedIntDiv
2AB-1-xyzAB-1-xyz-1AB-1-xyz0
3AB-1-xyzAB-1-xyz-2AB-1-xyz0
4AB-1-xyzAB-1-xyz-3AB-1-xyz0
5AB-1-xyzAB-1-xyz-4AB-1-xyz1
6AB-2-xyzAB-2-xyz-5AB-2-xyz1
7AB-2-xyzAB-2-xyz-6AB-2-xyz1
8AB-2-xyzAB-2-xyz-7AB-2-xyz1
9AB-2-xyzAB-2-xyz-8AB-2-xyz2
10AB-3-xyzAB-3-xyz-9AB-3-xyz2
11AB-3-xyzAB-3-xyz-10AB-3-xyz2
12AB-3-xyzAB-3-xyz-11AB-3-xyz2
13AB-3-xyzAB-3-xyz-12AB-3-xyz3
14AB-4-xyzAB-4-xyz-13AB-4-xyz3
15AB-4-xyzAB-4-xyz-14AB-4-xyz3
16AB-4-xyzAB-4-xyz-15AB-4-xyz3
17AB-4-xyzAB-4-xyz-16AB-4-xyz4
18AB-5-xyzAB-5-xyz-17AB-5-xyz4
19AB-5-xyzAB-5-xyz-18AB-5-xyz4
20AB-5-xyzAB-5-xyz-19AB-5-xyz4
21AB-5-xyzAB-5-xyz-20AB-5-xyz5
22AB-6-xyzAB-6-xyz-21AB-6-xyz5
23AB-6-xyzAB-6-xyz-22AB-6-xyz5
24AB-6-xyzAB-6-xyz-23AB-6-xyz5
25AB-6-xyzAB-6-xyz-24AB-6-xyz6
26AB-7-xyzAB-7-xyz-25AB-7-xyz6
27AB-7-xyzAB-7-xyz-26AB-7-xyz6
28AB-7-xyzAB-7-xyz-27AB-7-xyz6
29AB-7-xyzAB-7-xyz-28AB-7-xyz7
30AB-8-xyzAB-8-xyz-29AB-8-xyz7
31AB-8-xyzAB-8-xyz-30AB-8-xyz7
32AB-8-xyzAB-8-xyz-31AB-8-xyz7
33AB-8-xyzAB-8-xyz-32AB-8-xyz8
34AB-9-xyzAB-9-xyz-33AB-9-xyz8
35AB-9-xyzAB-9-xyz-34AB-9-xyz8
36AB-9-xyzAB-9-xyz-35AB-9-xyz8
37AB-9-xyzAB-9-xyz-36AB-9-xyz9
38AB-10-xyzAB-10-xyz-37AB-10-xyz9
39AB-10-xyzAB-10-xyz-38AB-10-xyz9
40AB-10-xyzAB-10-xyz-39AB-10-xyz9
41AB-10-xyzAB-10-xyz-40AB-10-xyz10
Sheet1

Blue table is original data transformed by PQ to generate the green table.
Here's the Query:
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Added Index" = Table.AddIndexColumn(Source, "Index", 1, 1, Int64.Type),
    #"Duplicated Column" = Table.DuplicateColumn(#"Added Index", "Item", "Item - Copy"),
    #"Inserted Integer-Division" = Table.AddColumn(#"Duplicated Column", "Integer-Division", each Number.IntegerDivide([Index], 4), Int64.Type),
    #"Changed Type" = Table.TransformColumnTypes(#"Inserted Integer-Division",{{"Index", type text}, {"Integer-Division", type text}}),
    #"Merged Columns" = Table.CombineColumns(#"Changed Type",{"Item", "Index"},Combiner.CombineTextByDelimiter("-", QuoteStyle.None),"MergedIndex"),
    #"Merged Columns1" = Table.CombineColumns(#"Merged Columns",{"Item - Copy", "Integer-Division"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"MergedIntDiv")
in
    #"Merged Columns1"
I used SEQUENCE with STEP 0.25 to generate the original items which is why I did the Integer Divide by 4. Adapt as needed!
If I were going with a formula, I'd use something like
Excel Formula:
=[@Item]&"-"&TEXT(INT(RAND()*10)+1,"#")
That generates a random number between 1 and 10. The "+1" is to prevent a blank number.
 
Upvote 0

Forum statistics

Threads
1,223,989
Messages
6,175,812
Members
452,671
Latest member
jowalker82

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top