Home > database >  Excel - getting a value based on the max value off another row in a Table
Excel - getting a value based on the max value off another row in a Table

Time:10-19

I'm looking for a solution for a problem I'm facing in Excel. This is my table simplified: Every sale has an unique ID, but more people can have contributed to a sale. the column "name" and "share of sales(%)" show how many people have contributed and what their percentage was.

Sale_ID Name Share of sales(%)
1 Person A 100
2 Person B 100
3 Person A 30
3 Person C 70

Now I want to add a column to my table that shows the name of the person that has the highest share of sales percentage per Sales_ID. Like this:

Sale_ID Name Share of sales(%) Highest sales
1 Person A 100 Person A
2 Person B 100 Person B
3 Person A 30 Person C
3 Person C 70 Person C

So when multiple people have contributed the new column shows only the one with the highest value.

I hope someone can help me, thanks in advance!

CodePudding user response:

You can try this on cell D2:

=LET(maxSales, MAXIFS(C2:C5,A2:A5,A2:A5), 
 INDEX(B2:B5, XMATCH(A2:A5&maxSales,A2:A5&C2:C5)))

or just removing the LET since maxSales is used only one time:

=INDEX(B2:B5, XMATCH(A2:A5&MAXIFS(C2:C5,A2:A5,A2:A5),A2:A5&C2:C5))

On cell E2 I provided another solution via MAP/XLOOKUP:

=LET(maxSales, MAXIFS(C2:C5,A2:A5,A2:A5),
MAP(A2:A5, maxSales, LAMBDA(a,b, XLOOKUP(a&b, A2:A5&C2:C5, B2:B5))))

similarly without LET:

=MAP(A2:A5, MAXIFS(C2:C5,A2:A5,A2:A5), 
 LAMBDA(a,b, XLOOKUP(a&b, A2:A5&C2:C5, B2:B5)))

and here is the output: sample output

Explanation

The trick here is to identify the max share of sales per each group and this can be done via MAXIFS(max_range, criteria_range1, criteria1, [criteria_range2, criteria2], ...). The size and shape of the max_range and criteria_rangeN arguments must be the same.

MAXIFS(C2:C5,A2:A5,A2:A5)

it produces the following output:

maxSales
100
100
70
70

MAXIFS will provide an output of the same size as criteria1, so it returns for each row the corresponding maximum sales for each Sale_ID column value.

It is the array version equivalent to the following formula expanding it down:

MAXIFS($C$2:$C$5,$A$2:$A$5,A2)

INDEX/XMATCH Solution

Having the array with the maximum Shares of sales, we just need to identify the row position via XMATCH to return the corresponding B2:B5 cell via INDEX. We use concatenation (&) to consider more than one criteria to find as part of the XMATCH input arguments.

MAP/XLOOKUP Solution

We use MAP to find for each pair of values (a,b) per row, of the first two MAP input arguments where is the maximum value found for that group and returns the corresponding Name column value. In order to make a lookup based on an additional criteria we use concatenation (&) in XLOOKUP first two input arguments.

  • Related