Excel: re-structure multiple columns to two columns (for pivot tables) - excel

My data is like this:
Col1 | Col2
1 | a,b,c
2 | d,e
3 | f
And I want it to be like this:
Col1 | Col2
1 | a
1 | b
1 | c
2 | d
2 | e
3 | f
I've tried experimenting with offset and just general transpose, but haven't been able to figure out a way to do this. I have 1,000 rows (which in the finished product will be several thousand), so manually is not an option.
Thanks for any suggestions!

Or, assuming that data is in A1:B3, enter this array formula** in e.g. D1:
=IFERROR(INDEX($A$1:$A$3,MATCH(TRUE,MMULT(0+(ROW($A$1:$A$3)>=TRANSPOSE(ROW($A$1:$A$3))),1+LEN($B$1:$B$3)-LEN(SUBSTITUTE($B$1:$B$3,",","")))>=ROWS($1:1),0)),"")
Copy down until you start to get blanks for the results.
Then, in E1, this (non-array) formula:
=IF(D1="","",TRIM(MID(SUBSTITUTE(INDEX($B$1:$B$3,MATCH(D1,$A$1:$A$3,0)),",",REPT(" ",41)),41*(COUNTIF(D$1:D1,D1)-1)+1,41)))
Copy down as required.
Regards
**Array formulas are not entered in the same way as 'standard' formulas. Instead of pressing just ENTER, you first hold down CTRL and SHIFT, and only then press ENTER. If you've done it correctly, you'll notice Excel puts curly brackets {} around the formula (though do not attempt to manually insert these yourself).

Related

Using SUMIF on a range of columns or SUMPRODUCT to ignore text items

I have a range with various text and numbers in them. I wish to total the numbers based on the identifying first column.
I have tried
=SUMIF(A1:A20,"John",B1:E20)
which only returns the first columns number. I've also tried
=SUMPRODUCT((A1:A20="John")*(B1:E20))
but as there is text in column C, it returns #VALUE!.
A | B | C | D | E
John | 5 | Wine | 2 | 7
Sean | 6 | Beer | 5 | 2
I want all of the numeric values in columns B-E to be totalled together when "John" is in column A
Here is an array formula solution - use Ctrl, Shift and Enter to confirm:
=SUM(IF(A1:A20="John",IF(ISNUMBER(B1:E20),B1:E20)))
If you just want to exclude column C you could use this non-array formula:
=SUMPRODUCT((A1:A20="John")*(B1:B20))+SUMPRODUCT((A1:A20="John")*(D1:E20))
Alternatively, perhaps move C so it's not in the middle of your numbers.
Try this formula:
=IF(A1="John", SUMPRODUCT(--(ISNUMBER(B1:E1)),B1:E1), "")
and drag down to get sum on each row.

excel - transpose (rows to columns) with extra dimension?

is it possible to transpose rows to columns with extra dimensions??
imagine table:
dimension | data 1 | data 2 | data 2
---------------------------------------
test | a | b |
dummy | c | e | x
user | a | d |
john doe | v | |
I need to get this:
dimension | data
-------------------
test | a
test | b
dummy | c
dummy | e
dummy | x
user | a
user | d
john doe | v
Is there any built-in function? Or do I need to use macro?
Obviously this is more straightforward with VBA, though it can be done using worksheet formulas along, viz:
Assuming your source table is in A1:D5 (with headers in row 1), enter this array formula** in F2:
=IF(ROWS($1:1)>COUNTIF(B$2:D$5,"<>"),"",INDEX(A$2:A$5,MATCH(ROWS($1:1),1+MMULT(0+(ROW(B$2:D$5)>TRANSPOSE(ROW(B$2:D$5))),MMULT(0+(B$2:D$5<>""),TRANSPOSE(COLUMN(B$2:D$5)^0))))))
Copy down until you get blanks for the results.
Then enter this formula in G2:
=IF(F2="","",INDEX(INDEX(B$2:D$5,MATCH(F2,A$2:A$5,0),),COUNTIF(F$2:F2,F2)))
Copy down as required.
Note that this set-up assumes that it is not possible for a given dimension to have blank entries in between non-blank entries within the data columns, for example data1 and data3 are non-blank but data2 is blank. This is the case for the data you provided so seems like a fair assumption.
Regards
**Array formulas are not entered in the same way as 'standard' formulas. Instead of pressing just ENTER, you first hold down CTRL and SHIFT, and only then press ENTER. If you've done it correctly, you'll notice Excel puts curly brackets {} around the formula (though do not attempt to manually insert these yourself).

How can I conditionally VLOOKUP in Excel?

Suppose I have the table I need to VLOOKUP() through:
id | indicator | value
-----------------------
1 | a | abc
1 | b | def
1 | c | ghi
2 | a | bbc
2 | b | bef
3 | a | aef
There is a table where I need to attach only values with indicator equal to a:
id | value
----------
1 | abc
2 | bbc
3 | aef
Something like conditional VLOOKUP() is required. What is the elegant way to do it?
You can do multiple criteria INDEX-MATCH to achieve this:
=INDEX($C$1:$C$6,MATCH($E1&"a",$A$1:$A$6&$B$1:$B$6,0))
This is array formula and it works by hitting Ctrl+Shift+Enter.
Also, I assumed that your Lookup value is in cell E1, which is something you can change according to your needs.
Use an array formula:
I found this formula in the web a few years ago, cant remember where, but credit to them.
=VLOOKUP(CONCATENATE(D2,"a"), CHOOSE({1,2},A$1:A$10 & B$1:B$10, C$1:C$10 ),2,0)
For this example I used column D on the same sheet for the reference ID and column E for the result. Amend the references as required.
Due to this being an array formula, every time the formula is entered you will need to select the formula bar and hold CTRL and Shift and press enter on the keyboard.

Increment count in column based on value in column

I've 2 columns A and B. A contains names and B contains the count of those names till that record as shown below.
-----------------------------------
| A | B |
-----------------------------------
1 | Fruits | 1 |
2 | Flowers | 1 |
3 | Fruits | 2 |
So, want to have a formula for this. Expecting an array formula. Even if an array formula is not possible, a general formula
Attached a spreadsheet so that it can be explained better.
https://docs.google.com/spreadsheets/d/1wlWqdFwgv90s50iP-bXXBHciyualohj610qFiSatcmQ/edit#gid=1997586177
You do not need an array formula, and I would avoid them when possible. You can accomplish your task with
=COUNTIF(A$1:A1,A1)
Where A1 if the first value in the column of values you want to count. The $ allows you to anchor the top of your COUNTIF range while leaving the bottom dynamic.
In a google spreadsheet you may want to try:
=ArrayFormula(iferror(SORT(ROW(A1:A),SORT(ROW(A1:A),A1:A,1),1)-MATCH(A1:A,SORT(A1:A),0)-ROW()+2))
Example sheet

Excel Function Help - Compare 2 Cells in Worksheet to 2 Cells in another worksheet, if they match, return value from a different column

I'm wondering if someone would be able to offer some advice on the best way to do this please:
Data in Worksheet # 1
Col A | Col B | Col C
Part-1 | 8 | 2
Part-2 | 7 | 7
Part-7 | 9 | 4
Part-8 | 2 | 6
Data in Worksheet # 2
Col A | Col B | Col C
Part-1 | 8 | *Return value* (If Part-1 is found and 8 matches, return 2)
Part-2 | 7 | *Return value*
Part-3 | 8 | *Return value*
In Worksheet#2 in Cell C2 - I would like to check if the Part-1 in A1 is found in Col A in Worksheet#1. If it is, then I would also like to make sure the Number is B2 in Worksheet#2 matches the Qty in Col B next to the same part#, if both the Part# and Qty match, then i would like to copy across the value from the corresponding cell in Col C in Worksheet#1, to Col C in Worksheet#2. If it does not match, I would like it to return a 0.
Here is the a variation on Reinier's second approach in a form that will work in any version of Excel. You can use references to the specific data ranges, as I have done here, or to entire columns.
=SUM((A2=Sheet1!$A$2:$A$5)*(Sheet2!B2=Sheet1!$B$2:$B$5)*Sheet1!$C$2:$C$5)
This is an array formula, so it needs to be entered with the Control-Shift-Enter combination. It performs the same operation as the SUMPRODUCT. (There are several other ways to do this, such as using MATCH with INDEX or OFFSET.)
Essentially, what you are doing is a lookup based on values in two columns. Looking at it from that angle, you can use a the SUMPRODUCT function, which is supported in any version of Excel. Enter the following in Sheet2, cell C2:
=SUMPRODUCT((Sheet1!$A:$A=$A2)*(Sheet1!$B:$B=$B2)*Sheet1!$C:$C)
Select and drag down the right-bottom corner of C2 to complete column C.
This only works by virtue of the fact that the values in Sheet1, column C, are numbers. It breaks if value pairs in column A and B of Sheet1 occur multiple times, but you did not address that situation in your question in the first place.
For versions 2007 and up, you can use the more convenient function SUMIFS with basically the same approach:
=SUMIFS(Sheet1!$C:$C,Sheet1!$A:$A,$A1,Sheet1!$B:$B,$B1)
Alternatively, you can use a combination of IF and VLOOKUP functions. A formula that will work for Excel 2007 or newer is the following:
=IFERROR(IF(VLOOKUP($A1,Sheet1!$A:$C,2,FALSE)=$B1,VLOOKUP($A1,Sheet1!$A:$C,3,FALSE),0),0)
Older versions of Excel do not support IFERROR, but you can still use a similar approach as explained here.
I have uploaded an example workbook here, which includes an alternative method in column D of Sheet2 as well.

Resources