How to retrieve Top 2 rows from each group in excel sheet? - excel

The problem is simple to understand. I just need to know a formula that will help find a way to fetch the top 2 rows of each group in an excel sheet.
The below example is grouped by column 1.
Example
Given Table:
Column 1
Column 2
Apple
A102012
Apple
A102013
Apple
A102014
Banana
A102015
Banana
A102016
Banana
A102017
Coconut
A102017
Result:
Column 1
Column 2
Apple
A102012
Apple
A102013
Banana
A102015
Banana
A102016
Coconut
A102017

Try:
Formula in D1:
=REDUCE(A1:B1,UNIQUE(A2:A8),LAMBDA(a,b,VSTACK(a,TAKE(FILTER(A2:B8,A2:A8=b),2))))
To mimic this for Google Sheets:
=REDUCE(A1:B1,UNIQUE(A2:A8),LAMBDA(a,b,{a;QUERY(A2:B8,"Where A='"&b&"' limit 2")}))
A much slower alternative is:
=FILTER(A:B,INDEX(COUNTIFS(A:A,A:A,ROW(A:A),"<="&ROW(A:A)))<3)

A faster method than countifs not using most recent additions to Excel, if it can be assumed that data are pre-sorted:
=LET(count,COUNTA(A:A),Column1,A2:INDEX(A:A,count),Column12,A2:INDEX(B:B,count),FILTER(Column12,SCAN(0,SEQUENCE(count-1),LAMBDA(a,c,IF(c=1,1,IF(INDEX(Column1,c)=INDEX(Column1,c-1),a+1,1))))<=2))
or in Google Sheets:
=ArrayFormula(lambda(Column12,filter(Column12,SCAN(0,SEQUENCE(rows(Column12)),LAMBDA(a,c,IF(c=1,1,IF(INDEX(Column12,c,1)=INDEX(Column12,c-1,1),a+1,1))))<=2))(filter(A2:B,A2:A<>"")))

If you have Excel 365 you can also use this formula
=LET(rank,MAP(tblData[value],tblData[fruit],
LAMBDA(v,f,SUMPRODUCT((tblData[fruit]=f)*(v<tblData[value]))+1)),
FILTER(tblData,rank<=2))
The MAP function calculates the rank of each row within its group.
Then we can filter by that list.

Related

Excel: Grouping and restructuring data in Excel

In Excel, I want to convert the table on the top (Initial data) into the one at the bottom (Desired output).
I want to group the items in the second row by the first row, and then generate one column per unique value of the first row and list the items of the corresponding group in that column.
Is there a way to do that without manually copying cells?
Table: Initial data
Fruit
Banana
Fruit
Apple
Fruit
Grape
Vegetable
Spinach
Vegetable
Eggplant
Table: Desired output
Fruit
Vegetable
Banane
Spinach
Apple
Eggplant
Grape
If you have access to the UNIQUE and FILTER functions (Excel 365), you could accomplish this as follows.
Assuming your data is in A1:B5. In D1 enter:
=TRANSPOSE(UNIQUE($A$1:$A$5))
This will get you the unique values from the cells in A in D1:E1. Then in D2 enter:
=FILTER($B$1:$B$5,$A$1:$A$5=D1)
And drag to the right.
I.e. you do this:
Result:

Google sheets, adding a value to a cell based on the contents of another

This may be delving in to scripting territory rather than formula, but I was wondering if it's possible to use google sheets to add values to a cell based on the contents of another? For example, If I had a sheet arranged like the following:
Column A|Column B|Column C|Column D
Apples Oranges Grapes
Tomatoes Grapes Oranges
Melons Apples Tomatoes
Grapes Lemons Apples
And then I had another section that had
Column G|Column H
Apples 1
Tomates 2
Oranges 3
Grapes 4
Melons 5
Lemons 6
Is there a formula that will let me populate the contents of column D by reading columns A - C on each row and adding the values set on column H? Making Column D read something like 8, 9, 8 etc?
I hope this question makes sense, thanks and apologies for the shoddy formatting!
=SUMPRODUCT(IFERROR(VLOOKUP(A2:C2,G:H,2,0)))
For google-spreadsheets
Please try this single-formula solution:
=mmult(filter(VLOOKUP(A:C,G:H,2,0),A:A<>""),ArrayFormula(transpose(sign(column(A:C)))))
Paste it in D1.
Here's a sample file of sum with arrayFormula.

Excel - Find if a product changed price

How can I find if a product changed it's price in excel? For example:
Yesterday's prices:
Potatoes 5,50
Tomatoes 7.40
Apples 5.60
Cucumber 4.30
Today's prices:
Potatoes 5,50
Tomatoes 5.20
Apples 5.50
Cucumber 4.30
Tomatoes and apples changed their price. How is it possible to highlight those changes based on "A" column product description?
This formula will return TRUE if an item in the list has changed price:
=COUNTIFS($A$1:$A$8,A1,$B$1:$B$8,"<>"&B1)>0
Enter it in C1 (in this example) and drag down
Here is what you can try. I also attached some pics for your reference.
Columns A and B are for yesterday's prices, columns C and D are for today's prices, and column E is to see the price changes (you don't need column E but I just want to show you so you know what is happening). Also, the formula for column E is =INDEX($A$3:$B$7,MATCH($C3,$A$3:$A$7,0),2)-$D3.
To highlight whichever product had price changed. We will use Conditional Formatting which is under Home > Conditional Formatting > New Rule > Use a formula.... Under cell D3, you enter the same formula like in cell E3 which is =INDEX($A$3:$B$7,MATCH($C3,$A$3:$A$7,0),2)-$D3. Pick the color you want as the pic sample below.
Then applies to =$D$3:$D$6 just like the pic shows below.
This should work for you. Try and let me know.
Use the conditional formatting on another column. E.g. have a column of Yesterday-Today and highlight if <> 0.
This will work, use the formula
=IF((AND(A1=C1,B1<>D1)),"yes","no")
where A1, C1 have fruite and B1, D1 have prices.
Below is the result obtained.
Potatoes 5,50 Potatoes 5,50 no
Tomatoes 7.4 Tomatoes 5.2 yes
Apples 5.6 Apples 5.5 yes
Cucumber 4.3 Cucumber 4.3 no
Then you can filter/highlight based on whether it is a yes/no.

Excel SumProduct with if Statement in a cell.

I have a basic problem in Excel. I have a row with names and one with numbers. I try to find what is the sum of these numbers for a special name.
ex.
A B
Apple 12
Apple 12
Kiwi 9
Apple 4
Banana 51
Kiwi 12
Banana 4
Kiwi
So far I just use a basic sumproduct which works well. Like
=Sumproduct((A1:A8=A1)*(B1:B8)
This formula gives me back my total number of Apples
(12+12+4).
The problem is, if a cell contain some formula, then I have #VALUE! result.
Let say the last cell called Kiwi contain a code like
=if(A64="", "", 12)
Then it makes Kiwi empty if A64 is empty. Great.
But sumproduct don't work anymore.
I can't sort the name... any ideas?
Thank you
You can simply use =SUMIF() formula to get sum of these numbers for a special name.
=SUMIF($A$1:$B$64,A1,$B$1:$B$64)
or
=SUMIF($A$1:$B$64,"kiwi",$B$1:$B$64)
You can change these ranges based on your list. (You can even define dynamic name in Name Manager and then you can use that Name as your range.)
It's not the fact that your cell is calculated (contains a formula), it's because the result of the formula is a text.
Maybe you could use à 0 instead of the 0-length text and apply conditional formatting to your cells (font colour white if 0 value)
=if(A64="", 0, 12)

Multiple vlookups in one cell

Ok I have two vlookups that both search from the same array but with different criterias.The first vlookup would search for the Price category. The second vlookup would seek the type of product.
This is an example array:
ID Price Type
1 Banana Fruit
2 Apple Fruit
3 Orange Fruit
4 Corn Flakes Cereal
5 Monster Energy Drink
The syntax would be:
Search for the first vlookup, if there is no results, try to search for the second vlookup.
If the first or second vlookup is true, then return value ID.
I have already made the vlookups but I have no idea how to combine both in one cell
Edit: Vlookups
=Vlookup(A2,E4:G8,2,0)
=Vlookup(B2,E4:G8,2,0)
And the lists:
A column B column
List 1 List 2
Banana Hardware
Carrot Vegetable
Orange Chocolate
Mango Candy
Fruit
It may be as simple as this:
=IFERROR(Vlookup(A2,E4:G8,2,0),Vlookup(B2,E4:G8,2,0))
Try to find one, if it fails try the other.
This formula returns corressponding ID if found value from A2 or B2 in ranges F4:F8 or G4:G8 respectively, or "not found in both columns" if both values not found:
=IFERROR(INDEX(E4:E8,IFERROR(MATCH(A2,F4:F8,0),MATCH(B2,G4:G8,0))),"not found in both columns")

Resources