#51 - Get Rankings of Excel Rows in Each Group While Retaining the Existing Order
Problem description & analysis:
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 |
Solution:
Use SPL XLL to do this:
=spl("=E(?).derive(#:id,rk).sort(#1,#2).run(rk=rank(#2;#1)).sort(id).(rk)",A1:B14)
As shown in the picture below:
Explanation:
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.
Feel free to download esProc Desktop and explore the data processing journey ⬇️
SPL download address: esProc Desktop Download
Plugin Installation Method: SPL XLL Installation and Configuration
References to other rich Excel operation cases: Desktop and Excel Data Processing Cases
SPL Programming (YouTube FREE courses): https://www.youtube.com/playlist?list=PLQeR-IhHo7qNCw6o7PW8YfHvRx8pgzZso