In Excel, get unique members in order and number them

In the Excel table below, there are duplicate values in column A:


A

1

Cow

2

Chicken

3

Horse

4

Butterfly

5

Cow

The task is to number column A in order with natural numbers while making duplicate values take the same number. Write the corresponding numbers in the next column on the right:


A

B

1

Cow

1

2

Chicken

2

3

Horse

3

4

Butterfly

4

5

Cow

1

Use SPL XLL to enter the formula below and drag the first cell down all the target cells:

=spl("=?2.conj().id@u().pos(?1)",A1,A$1:A$5)

Picture1png

Or we can use a dynamic array formula to get the results at a time:

=spl("=(y=(x=?1.conj()).id@u()),x.(y.pos(~))",A1:A5)

In the above formula, conj()function concatenates values in the specified range of cells into a one-dimensional arrary; id() function get the unique members and works with @u option to not to re-sort the array; pos() function get the position of each member.

Source:https://stackoverflow.com/questions/78400711/excel-transforming-automatically-a-cell-based-on-the-input-in-another-one