# How to get sum of previous 2 (or any number) values based on another column(s)?

Dataset looks something like this:

I would like to get the sum of the values based on the ranking of column RR descending. For example - I would like the sum of column PD based on the previous 2 distinct values in column W and column PS ranked by RR desc. So, for PS 1, I would want 500 + 900 = 1400 ( the values from W.A and W.B because W.D does not have a PS.1). For PS.2, I would want 1250 + 1100 = 2350 because W.A has a PS.2 and W.D has a PS.2. So I need to rank everything by RR. Then look at the PS to make sure it exists, if not move on to the next one. The next step for this would be to sum up the column AH for each corresponding PD that was summed up. So for PS.1, the sum for AH would be 8.5.

The Column PS is currently my X axis.

Thanks in advance for the help!