Determine the max value of a group, and display that row - excel

I've got a few thousand rows in an excel spreadsheet, which (simplified) looks like this:
ID Category Animal Version Value
100 A Dog 1 20
100 B Cat 2 50
100 C Dog 3 50
200 A Dog 1 100
200 A Cat 2 100
300 B Cat 1 80
400 C Dog 1 80
I need to have the row with the highest/max version for each group of ids listed.
So in other words, I'd want these showing:
ID Category Animal Version Value
100 C Dog 3 50
200 A Cat 2 100
300 B Cat 1 80
400 C Dog 1 80
Is this possible?

Finding and showing the maxima/minima for grouped data with a single formula in only one cell can be done with the following formula:
=UNIQUE(FILTER(MyArray,MMULT(((ValueRange>TRANSPOSE(ValueRange))+(ValueRange=TRANSPOSE(ValueRange))-(GroupRange=TRANSPOSE(GroupRange)))*(GroupRange=TRANSPOSE(GroupRange)),SEQUENCE(ROWS(GroupRange),1,1,0))=0),FALSE,FALSE)
For an example, see this screenshot: Link
The output automatically adjusts for any number of groups in the data array.
With the operator > used in the formula, it will return the maximum. By using < it will return the minimum.
Note that the UNIQUE() function will only show distinct rows for each group maximum (see group 'Alpha' in screenshot).
If there is more than one maximum in a group and more than just the group and value column, the UNIQUE() function will show all distinct rows taking into account all columns (as can be seen for group 'Alpha' and 'Gamma' here: Link).

You can use the Advanced Filter with a formula criteria:
=D9=AGGREGATE(14,6,1/(A9=Table1[ID])*Table1[Version],1)
where D9 is the location of the first entry in the Value Column
Before applying Filter
After applying Filter

Suppose your data is in range A1:E8,
In cell A11, put in the following formula to find unique ID, drag it down until there is a #N/A error:
=INDEX($A$2:$A$8,MATCH(0,INDEX(COUNTIF($A$10:A10,$A$2:$A$8),0),0))
In cell B11, put in the following formula and drag it down to find the latest Version:
=AGGREGATE(14,6,$D$2:$D$8/($A$2:$A$8=A11),1)
In cell C11, D11 and E11, put in the following formulas respectively and drag them down to find the corresponding Category, Animal and Value:
=INDEX($B$2:$B$8,MATCH(1,INDEX(($A$2:$A$8=A11)/($D$2:$D$8=B11),0),0))
=INDEX($C$2:$C$8,MATCH(1,INDEX(($A$2:$A$8=A11)/($D$2:$D$8=B11),0),0))
=INDEX($E$2:$E$8,MATCH(1,INDEX(($A$2:$A$8=A11)/($D$2:$D$8=B11),0),0))
Let me know if there is any question. Cheers :)

For a list without duplicates you can put this in cell G2 ARRAY-FORMULA: CTRL + SHIFT + ENTER
=IFERROR(INDEX(A:A,MATCH(1,(COUNTIF(G$1:G2,A$1:A$99)=0)*(A$1:A$99<>""),0)),"")
This gives you a list with unique ID's. Now you can use the max formula to get the max version number of each ID. ARRAY-FORMULA: CTRL + SHIFT + ENTER
=MAX(IF($A$2:$A$2000=G3,$D$2:$D$2000,0))
The rest can be done with INDEX/MATCH formulas.

Related

Choosing minimum value and corresponding columns in Excel

I have an Excel Spreadsheet containing elements in 3 columns
Name Number Time
A 1 0.425
A 2 0.123
B 1 1.0256
B 2 0.564
B 3 0.7895
C 2 0.256
C 5 0.458
I want to choose minimum Time and corresponding Name and Number. I have tried it with pivot table and retrieved Name and minimum Time but could not get the corresponding Number column value.
EDIT :
Just to explain further. For each corresponding Name i want to choose minimum Time and then corresponding Number against that minimum Time. E.g. Output for above table should be like this :
Name Number Time
A 2 0.123
B 2 0.564
C 2 0.458
You need three formulas
Extract list of unique Name
This formula is an array formula so needs to be confirmed by holding down ctrl + shift while holding down enter
E2: =IFERROR(INDEX($A$2:$A$8,MATCH(0,COUNTIF($E$1:E1,$A$2:$A$8),0)),"")
minimum time for the Name
G2: =AGGREGATE(15,6,1/(E2=$A$2:$A$8)*$C$2:$C$8,1)
matching number for minimum time for name
F2: =AGGREGATE(14,4,(E2=$A$2:$A$8)*(G2=$C$2:$C$8)*$B$2:$B$8,1)
The screenshot below should clarify where the formulas go and the referred ranges:
Assuming your data is in A2:C8, you can use the formula:
For Name:
=INDEX(A2:C8, MATCH(MIN(C2:C8), C2:C8, 0), 1)
For Number:
=INDEX(A2:C8, MATCH(MIN(C2:C8), C2:C8, 0), 2)
A PivotTable can indeed do that for you:
This is accomplished by setting a Top 1 Values filter as follows:

Getting the next higher value with VLOOKUP or INDEX/MATCH

I have the following Excel spreadsheet:
A B C D
1 0 Product 1 7.500 Product 4
2 1.000 Product 2
3 5.000 Product 3
4 10.000 Product 4
5
In Cell C1 I type a random number (in this case 7.500). Now I want that in Cell D1 the corresponding Product is shown to the value in Cell C1. Since 7.500 does not exist in Column A the next higher value should be used. In this case 10.000 which belongs to Product 4.
I tried to go with the following formula in Cell D2 but instead of getting Product 4 I get #NV as a result.
=INDEX(A1:B4;MATCH(C1;A1:A4;-1);2)
The only solution I found so far was changing the values in Column A from ascending to descending. However, I would prefer to have a solution which does not require a change of the order in the list.
Do you have any idea how to solve this issue without changing the order in the list?
For unsorted data you can use below formula::
=INDEX(B1:B4,MATCH(SMALL($A$1:$A$4,COUNTIF($A$1:$A$4,"<"&C1)+1),A1:A4,0))
See image for reference

Getting the next lower value with VLOOKUP

I have the following Excel spreadsheet:
A B C D
1 15.000 Product 1 7.500 Product 2
2 5.000 Product 2
3 1.000 Product 3
4 0 Product 4
5
In Cell C1 I type a random number (in this case 7.500). Now I want that in Cell D1 the corresponding Product is shown to the value in Cell C1.
Since 7.500 does not exist in Column A the next lower value should be used.
In this case 5.000 which belongs to Product 2.
I tried to go with the following formulas in Cell D2 but instead of getting Product 2 I always get Product 4 as a result.
=VLOOKUP(F16,$A$1:$B$4,2,TRUE)
=VLOOKUP(F16,$A$1:$B$4,2)
Do you have any idea how to solve this issue?
For unsorted data you can use below formula:
=INDEX(B1:B4,MATCH(LARGE($A$1:$A$4,COUNTIF($A$1:$A$4,">"&C1)+1),A1:A4,0))
See image for reference
i will try it with addition columns, which indicates the difference between given 7.5 and col A like this: =IF($C$1-A1<0,999999,$C$1-A1) and then get the mininum value like =MIN(E1:E4). then get the according value in column B.
If the order is not guaranteed to be ascending then you can use an array formula (use ctrl+shift+enter instead of just enter to create an array formula)
=INDEX(B2:B5,MATCH((C2-MIN(IF((C2-A2:A5)<0,MAX(A2:A5),C2-A2:A5))),A2:A5,0))

Excel IF statments

I am trying to come up with a conventional system in a way where I can do an if statement to fulfill a list of data filled names without using a VBA macro. This would use an IF statement to see if Column D has a value to a name, and if it does, puts it in the fulfilled list, and if it does not, does nothing and places the next name there if it has a value. There will always be 8 items in unfulfilled list as it is fixed/permanent names(values in E will change), and 4 or less in the fulfilled list.
EXPECTED OUTPUT:
Fulfilled List UnFulfilled List
A B D E
Apples 50 Apples 50
Oranges 75 Peanuts
Grapes 60 Oranges 75
Avacados 100 Grapes 60
Carrots
Avacados 100
Here is sort of my thought. It would check the value, if value is there, place value and then place name, if value is not there, check the next cell down. However this would fail as it would take e2 no matter what, when we only want it to take valid items.
Cell B1 = =if(e1="",e2,e1)
Cell A1 = =if(b1="Apples,d1,d2) nested if statement possibly?
Any help would be appreciated how I could go about this. Thanks!
If I understand correctly, you could either use a pivot table or use an array formula such as this in A1:
=IFERROR(INDEX($D$1:$D$3,SMALL(IF($E$1:$E$3>0,ROW($E$1:$E$3)),ROW())),"")
Note: Hit Ctrl + Shift + Enter and then drag down as needed
Note2: In this case were using row 1, if the data was below that then (e.g. a tbale with a header) where ROW() is becomes --> ROW()-ROW(reference to header cell)
You need to change:
$D$1:$D$3 - this should be the full list of items in column D
$E$1:$E$3 - similarly this should be the full list of the counts in column E ( change in both places - next to the IF and next to the ROWS)
Then in column B you can have a vlookup formula such as:
=IF(LEN(A1)>0,VLOOKUP(A1,$D$1:$E$3,2,FALSE),"")
Note: this assumes you have no duplicates in column D
Again you will need to change $D$1:$E$3 so that it has all the content in those columns
Hope that helps

Sum the values in Excel cells depending on changing criteria

In an Excel spread sheet I have three columns of data, the first column A is a unique identifier. Column B is a number and column C is either a tick or a space:
A B C
1 d-45 150 √
2 d-46 200
3 d-45 80
4 d-46 20 √
5 d-45 70 √
Now, I wish to sum the values in column B depending on a tick being present and also relative to the unique ID in column A. In this case rows 1 and 5. Identifying the tick I use
=IF(ISTEXT(C1),CONCATENATE(A1))
&
=IF(ISTEXT(C1),CONCATENATE(B1)).
This leaves me with two arrays of data:
D E
1 d-45 150
4 d-46 20
5 d-45 70
I now want to sum the values in column E depending on the ID in column D, in this case row 1 and 5. I can use a straight forward SUMIFS statement to specify d-45 as the criteria however this unique ID will always change. Is there a variation of SUMIFS I can use?
I also wish to put each new variation of ID number into a separate header with the summed totals underneath, that is:
A B
1 d-45 d-46
2 220 20
etc...
You can try this:
To get the distinct ID's write (in H1 then copy right):
This one is an array formula so you need Ctrl Shift Enter to enter the formula
=INDEX($A$1:$A$5;SMALL(IF(ROW($A$1:$A$5)-ROW($A$1)+1=MATCH($A$1:$A$5;$A$1:$A$5;0);ROW($A$1:$A$5)-ROW($A$1)+1;"");COLUMNS($A$1:A1)))
Now to get the sum (H2 and copy right)
=SUMPRODUCT(($A$1:$A$5=H1)*ISTEXT($C$1:$C$5)*$B$1:$B$5)
Data in the example is in A1:C5
Depending on your regional settings you may need to replace ";" field separator by ","
Try this,
SUMIFS
=SUMIFS(B1:B5,A1:A5,"=d-45",C1:C5,"<>")
where "<>" means that the cell is not empty...

Resources