Get rankings of Excel rows in each group while retaining the existing order

We have an unordered Excel table, where the 1st column is the grouping column and the 2nd column contains dates.


A

B

1

Agent ID

Date of Sale

2

Agent1

07-12-2023

3

Agent1

05-12-2023

4

Agent2

09-12-2023

5

Agent3

13-12-2023

6

Agent2

14-12-2023

7

Agent2

22-12-2023

8

Agent1

15-12-2023

9

Agent1

17-12-2023

10

Agent2

13-12-2023

11

Agent1

20-12-2023

12

Agent1

18-12-2023

13

Agent3

20-12-2023

14

Agent3

09-12-2023

Task: Get rankings of rows in each group and write them in the 3rd column while retaining the existing order of the rows. The expected result is as follows:


A

B

C

1

Agent ID

Date of Sale

result

2

Agent1

07-12-2023

2

3

Agent1

05-12-2023

1

4

Agent2

09-12-2023

1

5

Agent3

13-12-2023

2

6

Agent2

14-12-2023

3

7

Agent2

22-12-2023

4

8

Agent1

15-12-2023

3

9

Agent1

17-12-2023

4

10

Agent2

13-12-2023

2

11

Agent1

20-12-2023

6

12

Agent1

18-12-2023

5

13

Agent3

20-12-2023

3

14

Agent3

09-12-2023

1

Use SPL XLL to do this:

=spl("=E(?).derive(#:id,rk).sort(#1,#2).run(rk=rank(#2;#1)).sort(id).(rk)",A1:B14)

Picture1png

E()function converts the Excel data range to a table. derive() function adds a new column. rank() function gets rankings of records in each sorted group. # is the row number and #1 represents the 1st column.

Source:https://stackoverflow.com/questions/78109251/how-to-create-a-sequence-number-based-on-sales-agent-name-and-sale-date-in-excel