How to merge and sum 2 arrays using excel? - excel

Given a .csv data file as follows
12,green orange
1,good egg
...
5,green orange
I want to sum-up the first column elements if the second column element is the same. Given the example above, now we should have
17,green orange
1,good egg
...
What is the easiest way to do this?

A good option would be to use a Pivot Table (this is practically what they are designed for).
In your case, look into the SUMIF formula - link as it doesn't seem you will need such advanced functionlaity.
Copy the list of "keys" (the items) into a separate worksheet and remove duplicates, then just setup SUMIF like:
key | total sum
green | = SUMIF(dataSheet!B:B, A2, dataSheet!A:A)
where you have this in a sheet, and dataSheet represents the CSV values you just imported.

Assuming you don't want to work with variables.
Count |Color
12 |Green
1 |Blue
5 |Green
7 |Green
3 |Blue
Sort the file by the value "Color"
Count |Color
1 |Blue
3 |Blue
12 |Green
7 |Green
5 |Green
Put Formula in cell C2 and paste down
=IF(B2=B1,(C1+A2),A2)
Harvest the sums at the end of each color (i.e. Blue = 4, Green = 24)

Related

SUM columns on the same sheet based on conditions or SUMIFS from another sheet

Here is a small sample table
+--------+-------+--------+
| COL 1 | COL 2 | COL 3 |
+--------+-------+--------+
| abc123 | Total | |
+--------+-------+--------+
| abc123 | cat1 | 100.00 |
+--------+-------+--------+
| abc123 | cat2 | 200.00 |
+--------+-------+--------+
| def123 | Total | |
+--------+-------+--------+
| def123 | cat1 | 100.00 |
+--------+-------+--------+
| def123 | cat2 | 200.00 |
+--------+-------+--------+
In COL 3, IF COL 2 is "Total" I need to SUM everything in COL 3 for each row in COL1 that is the same. (EG. COL3 Total row should be 300.00 for abc123 and then 300.00 for def123) Otherwise if COL 2 is NOT "Total" I need to do SUMIFS('Sheet3'!N:N,'Sheet3'!A:A,Sheet2!A473,'Sheet3'!Q:Q,Sheet2!Q473)*Sheet4!$U$2)
How can I can I accomplish the first part of the SUM?
Edit:
I think my example is too rigid and appears like it is set.
Let me see if I can explain in more fluid terms. I will have to describe this some what in database terms. All of the columns are on one sheet for the purposes of the "Total" portion.
COL 1 is my partition. Each of the "ID's" in COL 1 consists of 57 rows. Within 1 of those 57 rows is "Total" in another column, in the example that is COL 2.
So I have a large table that in COL 1 there are say 5 different ID's with 57 rows for each ID resulting in 285 rows.
Now I had a sorting function that would likely make this whole thing easier, but that function is crashing excel and not sorting both required sorts ( https://techcommunity.microsoft.com/t5/excel/sort-function-causes-a-crash-and-does-not-perform-secondary-sort/m-p/1477123#M66205 )
I suppose if I can get the sorting function to stop crashing excel this becomes slightly easier as then "Total" is consistently placed in row 2, 58, 116, etc. and I can add up everything below it. Right now, because that sort doesn't work, I have to add up everything from COL 3 that is NOT assigned to "Total" in COL 2 and has the same ID in COL1.
So in the table above abc123 is 3 rows and I need to add up the two rows that are not total for abc123 and have the formula spit out 300 into COL 3 for total.
Then def123 needs the same treatment.
Here is the tough part: the sorting is inconsistent because the data comes from a Redshift query so it is random for each ID. The IDs themselves are in random order. I think I can get the sort for COL 1 to work without crashing excel, but the secondary sort with the custom order is crashing it.
One way to avoid the Circular Reference error when trying to Total a column is to use two Sums, one above and one below.
So, assuming that your Columns 1, 2 and 3 are A, B and C, and that data starts in Row 2 (Row 1 being a header), you need the Sum of cells above the current row:
SUMIFS(C$1:C1, A$1:A1, A2)
Plus the Sum of the cells below the current row:
SUMIFS(C3:INDEX(C:C, 1+COUNTA(A:A)), A3:INDEX(A:A, 1+COUNTA(A:A)), A2)
(Note that this will actually terminate one row above and below the dataset)
Put this together with an IF statement:
=IF(B2="Total", SUMIFS(C$1:C1, A$1:A1, A2) + SUMIFS(C3:INDEX(C:C, 1+COUNTA(A:A)), A3:INDEX(A:A, 1+COUNTA(A:A)), A2), EXISTING_FORMULA_HERE)
Alternatively, you could try writing an Array Formula to calculate the SUM directly, a bit like when using multiple conditions in a MATCH, something like this: (not enough information in the question to do this exactly)
=SUMPRODUCT('Sheet3'!N:N*(COUNTIFS(A:A,'Sheet3'!$A:A)>0)*(COUNTIFS(B:B,'Sheet3'!$Q:Q)>0))
(Sum of Sheet3!N:N when a row exists in the current sheet that matches columns Sheet3!A:A in Column A and Sheet3!Q:Q in Column B)
Note that working on Entire Columns with Array Formulae is quite slow, so you may want to limit those just to the Used Range

Sum values if multiple conditions met in different sheet

I have one sheet that creates a mapping of names to values (Map_Sheet). In another sheet there are values for each name in the mapping table (Data_Sheet). What I am trying to do is add values based on certain conditions in the mapping table. For example: I want to add all counts of dog by bread and color. So in the mapping table I would look for all dogs that are brown and of a certain bread and get their names and manually add them together. I want to have a formula that does the addition based upon multiple conditions from Map_Sheet.
Here is an example of the data:
Map_Sheet-
name|bread|color|age
a x b 2
b y w 3
c x b 2
d z f 4
Data_Sheet -
id|a|b|c|d
0 3 4 2 1
1 1 2 4 2
2 3 5 7 2
3 1 2 6 9
4 1 3 5 7
And for each ID in the data sheet I want a count of bread X with color B. So I would add for ID0 values for A and C, (3+2) - so ID0 = 5, etc for each id.
I cannot use VBA so I was looking into using INDEX and MATCH but I cannot wrap my head around it. Any ideas? Thanks!
If the row headers in the first sheet match the column headers in the second sheet, you can put this formula in (say) G2 of the second sheet.
=SUM(TRANSPOSE(Map!$C$2:$C$5="b")*C2:F2)
If the column headers in the second sheet were in a different order, you would have to use something like:-
=SUM(C2:F2*NOT(ISERROR(MATCH($C$1:$F$1,IF(Map!$C$2:$C$5="b",Map!$A$2:$A$5),0))))
Both of these are array formulae. You can add extra conditions to select breed as well as colour using the same basic pattern:-
=SUM(TRANSPOSE((Map!$C$2:$C$5="b")*(Map!$B$2:$B$5="x"))*C2:F2)
or
=SUM(C2:F2*NOT(ISERROR(MATCH($C$1:$F$1,IF((Map!$C$2:$C$5="b")*(Map!$B$2:$B$5="x"),Map!$A$2:$A$5),0))))

Excel - Replace blank if only 1 contiguous value

Okay this one is a bit strange to explain, but I have a file like this:
A B
1 | Person
2 | Person test
3 | Record
4 | Record test
5 | Tiger
6 | Scott
7 | Scott test
8 | Scott test
9 | Scott test
As you can see, in column A the first row where a new value starts, column B is blank. What I need is to fill in a placeholder (NULL) value into column B if there is only one contiguous value in column A. So for the above example row 5 only has 1 contiguous value so the result should look like this:
A B
1 | Person
2 | Person test
3 | Record
4 | Record test
5 | Tiger (NULL)
6 | Scott
7 | Scott test
8 | Scott test
9 | Scott test
I tried something like this but doesn't work:
IF(EXACT($A5,$A4)=FALSE AND EXACT($A5,$A6)=FALSE, "(NULL)", $B5)
I just want a formula I can paste all the way down column B. Any suggestions?
=IF(OR(A2=A1,A2=A3),"Test","(NULL)")
Replace Test and (NULL) with the values you want to display.
This checks to see if the AX = A(X-1) or A(X+1). If either of those is true, then it means that AX is contiguous with another row.
I should note that I tested this with Excel 2013.
Also, if you want the formula to use the value already in BX when the rows are contiguous, then you can't do that in the same column.
I'm not sure if that's what you're asking to do, but the formula you tried would have been attempting to do that, creating a circular reference.
You can easily do this in column C. Replace "Test" with BX. Then, if you only want 2 rows in the end, you can copy and paste the values of column C into column B, then delete column C. But, this way you'll lose your formula.
Suppose you have your data like this:
This formula seem to work:
=IF(OR(A2=A1,A2=A3),IF(B2="","",B2),"(NULL)")

Counting word "pairs" in excel between different columns

I'm wondering if it's possible to do this in excel:
Let's say I have these columns:
Column A | Column B
Apple | Red
Apple | Green
Pear | Green
Pear | Green
Is it possible for me to count how many times a set of "pairs" appears.
For example -
I want to count how many times "pear" and "green" appeared in my spreadsheet next to each other (not individually).
So for the above I should get:
Apples + Red appears 1 time
Apples + Green appears 1 time
Pear + Green appears 2 times
I tried using the COUNTIF function, but I don't think it can count a set of pairs like this...
If the above is possible... is it further possible to break down the data by time? For example, we have these columns:
Column A | Column B | Column C
3/7/2013 | Apple | Red
3/7/2013 | Apple | Red
3/8/2013 | Apple | Red
3/8/2013 | Pear | Green
3/8/2013 | Pear | Green
3/9/2013 | Apple | Red
3/9/2013 | Pear Green
If I want to organize the data by day, how can I do this? For example:
I want to display that on 3/7/2013, Apple + Red appeared 2 times, but on 3/8/2013, Pear + Green appeared 2 times and Apple + Red only appeared once.
Is it also possible to organize data by weeks or months this way? (During the month of January, I got 20 Apple + Red "pairs"
Let me know if anything was unclear in what I was asking
Thanks for your help!!!!
Add a new column to your data, which concatenates your data (ideally with delimiters to avoid value clashes) e.g. In Col D1 put formula
=A1&"|"&B1&"|"&C1
Give it a heading of Vals, and then create your pivot table from all the data
As an example I created this sample data which has 2 employees, Fred & Bill and a number of rows for each on 2 days. For 3-Jul, both Fred and Bill appear 3 times, but on 4-Jul Fred appears once and Bill 5 times.
My pivot table shows this result (which is hopefully what you're after)
EDIT :
For your situation, you probably need two extra columns, one called 'Pair Name' which concatenates B & C, and another called 'Pairs' which concatenates A, B & C.
Then in your pivot table you can drag 'Pair Name' into the Column Labels, and 'Pairs' into the Sum of Values.

Comparing 2 different rows in 2 different sheets - Excel

I am wondering is there is any formula/tool to compare 2 different rows in 2 different sheets in excel.
Example:
Sheet 1 I want to compare cells A14 to A24
with Sheet 2 Cells B14 to B24
Thanks.
You could try something like:
=Sheet1!A1&Sheet1!B1&Sheet1!C1 = Sheet2!A1&Sheet2!B1&Sheet2!C1
This basically just compares the concatenation of 3 cells with 3 other cells. But as Zombian said, we could help more if you were more specific.
What kind of data are you expecting in the cells?
What is the overall goal? If you are running a test to see if they are exactly the same then it may be different than to see if they all add up to the same number or something like that.
Let's assume your data looks like this way:
On Sheet1 (old data)
Col A | Col B
Id | Value
1 | My Val 1
2 | My Val 2
3 | My Val 3
On Sheet2 (new data)
Col A | Col B
Id | Value
123 | My Val 1
3 | My new Val 3
2 | My Val 2
1 | My new Val 1
On Sheet2, put in column C the following formula:
=INDEX(Sheet1!B:B,MATCH(Sheet2!A2,Sheet1!A:A,0))=Sheet2!B2
This will return TRUE or FALSE whether the value matches or not the old one. We need to assume the Id would not change between the two Sheets (or else, please tell us how we could deal with it).
... and =INDEX(Sheet1!B:B,MATCH(Sheet2!A2,Sheet1!A:A,0))=Sheet2!B2
can be adapted for use in conditional formatting across all the required columns in the second worksheet

Resources