#51 - Get Rankings of Excel Rows in Each Group While Retaining the Existing Order

#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.

AB
1Agent IDDate of Sale
2Agent107-12-2023
3Agent105-12-2023
4Agent209-12-2023
5Agent313-12-2023
6Agent214-12-2023
7Agent222-12-2023
8Agent115-12-2023
9Agent117-12-2023
10Agent213-12-2023
11Agent120-12-2023
12Agent118-12-2023
13Agent320-12-2023
14Agent309-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:

ABC
1Agent IDDate of Saleresult
2Agent107-12-20232
3Agent105-12-20231
4Agent209-12-20231
5Agent313-12-20232
6Agent214-12-20233
7Agent222-12-20234
8Agent115-12-20233
9Agent117-12-20234
10Agent213-12-20232
11Agent120-12-20236
12Agent118-12-20235
13Agent320-12-20233
14Agent309-12-20231

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:

Picture1png

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