简体   繁体   中英

Excel Formula - How to get the highest 2 values of a cell based on another cell

I need some help with the following requirement:

Raw Data

            A            B            C
      -------------------------------------
   1 |     Opp     |   Vendor    |  Amount |
     |-------------|-------------|---------|
   2 |     101     |   Vendor1   | 100000  |
   3 |     101     |   Vendor2   |   5000  |
   4 |     103     |   Vendor1   |  30000  |
   5 |     103     |   Vendor2   |   5000  |
   6 |     103     |   Vendor3   |  50000  |

Output Table

            A             B           C         D         E
      ---------------------------------------------------------
   1 |     Opp     |  MainVendor | Amount1 | 2Vendor | Amount2 |
     |-------------|-------------|---------|---------|---------|
   2 |     101     |   Vendor1   | 100000  | Vendor2 |  5000   |
   3 |     103     |   Vendor3   |  50000  | Vendor1 |  30000  |
  • MainVendor : Vendor with the highest Amount
  • Amount1 : Amount for MainVendor
  • 2Vendor : Second highest vendor
  • Amount2 : Amount for 2Vendor

I was only able to get Amount1 value using the following array formula:

{=MAX(IF(A:A=[@[Opp]];C:C))} 

in column C.

I'm failing to get the values for column B , D & E .

Here's a solution - may be a bit convoluted so happy to hear constructive criticism if people can refactor the formulas.

To get the Amount1 , Amount2 values you can use:

=INDEX(LARGE($C$2:$C$10*--($A$2:$A$10=$A13),1),1)

Where:

  • $C$2:$C$10 are the values from the Amount column
  • multiplied by --($A$2:$A$10=$A13) - meaning {1;1;0;0;0;0;0;0;0} * {100000;5000;30000;5000;50000;45000;50000;40000;51000} - this has the impact of eliminating values in the Amount column that do not correspond to the Opp of the row.
  • The LARGE function then just picks 1st largest of 100000 and 5000 as the other values in the array are now 0 .
  • to get 2nd largest, 3rd largest etc you suppy 2 , 3 etc into the LARGE function
  • LARGE function is wrapped in INDEX to get the actual value out as I believe its returning it as an 1-element array (although this isn't clear with the formula inspector)

Sample:

在此处输入图片说明

To get MainVendor and Vendor2 values you can use:

=INDEX($B$2:$B$10,INDEX(MATCH(LARGE($C$2:$C$10*--($A$2:$A$10=$A13),1),$C$2:$C$10*--($A$2:$A$10=$A13),0),1))

Which is:

  • $B$2:$B$10 are the values from the Vendor column
  • the index is retrieved by using MATCH on the same formula used for AmountX against its own source array
  • the MATCH is wrapped in another INDEX to get the actual value out as, once again, I think the value is being returned as a 1-element array

Sample:

在此处输入图片说明

I created another set of Opp values and the formula works with some duplicate values in the Amount column but for different Opp s.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM