tags:

views:

67

answers:

2

i have this data set:

92127
96001-1
94533-1
95630
95677
95630

i need this output

92127-1
95630-1
95677-1
95630-2

here is the logic:

if 92127 does not exist in this column, then it should be 92127-1; however, if it DOES exist, then it should be 92127-(what ever the largest number is here +1), so if the largest one is 92127-5, then it should make it 92127-6

is it possible to make this into a formula?

+1  A: 

Since you want to keep the '-1' on whatever is there (as per comment), this should work:

Assuming 'Existing' is in cells A2:An, this formula goes in B2, and can be copied down to Bn.

=LEFT(A2,5)&TEXT(COUNTIF(A$2:A2,LEFT(A2,5))+IF(ISERROR(VALUE(MID(A2,7,1))),0,VALUE(MID(A2,7,1))),"-0;;")

My results are this:

Existing

92127
96001-1 
94533-1 
95630
95677
95630

New

92127-1
96001-1
94533-1
95630-1
95677-1
95630-2
Coldnorth
if it has one already then keep it
I__
It seems that numbers that only occur once should have '-1' appended.
mouche
...I missed that; you're right. That means the other answer actually makes more sense than mine, as I was trying to deal with removing it if it wasn't necessary.
Coldnorth
+3  A: 

Here's mine:

=IF(LEN(A2)>5,A2,A2&"-"&SUMPRODUCT(--(LEFT($A$2:$A2,5)=LEFT(A2,5))))

A2:A7

92127
96001-1
94533-1
95630
95677
95630

B2:B7 (where you put the formula)

92127-1
96001-1
94533-1
95630-1
95677-1
95630-2

Edit: The above assumes that first instance of the five digit leading number will have no -n and uses the count of that five digit leading number to append the -n to subsequent instances. However, if the first instance of, say, 95630, is 95630-2 then the above fails. String manipulation does not work well with array formulas, so I think the only option is to use helper columns. With your data in A2:A7,

B2: =IF(ISERR(FIND("-",A2)),A2,LEFT(A2,5))
C2: =IF(ISERR(FIND("-",A2)),0,MID(A2,FIND("-",A2)+1,LEN(A2)))
D2: =B2&"-"&IF(ISERR(FIND("-",A2)),MAX(($B$2:$B$7=B2)*($C$2:$C$7))+1,C2)

and fill down to row 7. B2 and C2 split the number into the five leading digits and the -n suffix, if any. If there is no -n, it returns a 0. D2 is entered with Control+Shift+Enter because it's an array formula. It finds the maximum -n for the current five leading digits, adds one, and appends it to the five leading digits.

Helper columns make it more cumbersome, but I couldn't find a better way.

Dick Kusleika
this is actually not working correctly, for example if i already have in the B column a number *XXXXX-Y*, and i have a formula in the A column *XXXXX-Y* already, it does not put *XXXXX-(Y+1)* in B, instead it just returns *XXXXX-Y*
I__