Reputation: 4179
I have data in excel such as:
ID | Fee
123456789 | 100
987654321 | 100
987654321 | 75
987654321 | 50
I need to calculate a fee reduction for the items that are not the max price. The spreadsheet is sorted by ID, then Fee in the fashion needed. What I do not know how to do is use a similar row_number() over(partition by) in excel that I would normally do in SQL
Desired output would be
ID | Fee | rn
123456789 | 100 | 1
987654321 | 100 | 1
987654321 | 75 | 2
987654321 | 50 | 3
Upvotes: 6
Views: 49158
Reputation: 11
For anyone who needs more performance, you can adjust the above query slightly to work better on large data sets.
Step one is count the maximum number of entries of a specific value in the column you wish to partition by. I did this with a pivot table.
For example, '987654321' shows up 3 times. With that information you can now limit the number of rows which the countif statement is applied to.
Then sort your data on column A. And use the below formula.
=COUNTIF(A2:INDIRECT("A"&ROW(A2)+3),A2)
This creates a rolling window for your countif function to apply too.
Upvotes: 1
Reputation: 11
Upvotes: 1
Reputation: 61
you can use =COUNTIF($A$2:A2,A2)
; note that only the first $A$2 will not move.
Upvotes: 6
Reputation: 119
This formula will do the job:
=COUNTIF($A$2:INDIRECT("A"&ROW(A2)),A2)
There is no need for sorting the data and you won't fall out of the range.
ROW()
is used to make the range dynamic, so if we drag the formula down, ROW()
will always give us ending point:
Upvotes: 9
Reputation: 50034
There's probably a more complex formula one could just throw at the data without having to monkey with the data, but I think this may be an easier solution:
Sort the data by ID (smallest to largest) and Fee (Smallest to largest)
Use formula =Countif(A2:A5, A2)
to count how many times the same id
appears in the data for the current cell and every cell below it. Copying this down to fill out the missing column.
Upvotes: 4