EXCEL: Sum columns based on common index - excel

I'm trying to sum columns that have a common index but the difficulty is that the index is not in the same row.
Here is an example
DayIndex2018 Value2018 DayIndex2017 Value 2017
empty empty 1 20
1 50 2 45
2 60 3 55
3 70 4 33
4 32 5 23
What I would like is to have the sum of "value 2017" for indexes that are in common with indexes for 2018: 20 + 45 + 55 + 33
I though of doing this with a sumif but rows need to be perfectly aligned.
Any idea of how I could do this?
Thanks

Use sumproduct to sum the array of sumif
Formula in cell F2:
=SUMPRODUCT(SUMIF(C2:C6,A2:A6,D2:D6))

Related

Excel MERGE two tables

I have SET 1
CLASS
Student
TEST
SCORE
A
1
1
46
A
1
2
50
A
1
3
45
A
2
1
45
A
2
2
47
A
2
3
31
A
3
1
34
A
3
2
45
B
1
1
36
B
2
1
31
B
2
2
41
B
3
1
50
C
1
1
42
C
3
1
31
and SET 2
CLASS
SIZE
YEARS
A
39
7
B
20
12
C
31
6
and wish to COMBINE to make SET 3
CLASS
STUDENT
TEST
SCORE
SIZE
YEARS
A
1
1
46
39
7
A
1
2
50
39
7
A
1
3
45
39
7
A
2
1
45
39
7
A
2
2
47
39
7
A
2
3
31
39
7
A
3
1
34
39
7
A
3
2
45
39
7
B
1
1
36
20
12
B
2
1
31
20
12
B
2
2
41
20
12
B
3
1
50
20
12
C
1
1
42
31
6
C
3
1
31
31
6
so basically add the SIZE and YEARS columns from SET 2 and merge on CLASS onto SET 1. In excel how you can do this? I need to match on CLASS
Define both sets as tables and “left join” in PowerQuery. There you can choose the columns of the resulting table.
https://learn.microsoft.com/en-us/power-query/merge-queries-left-outer
If you have Set 1 on the top left of a worksheet "Set1" and Set 2 on the top left of a worksheet "Set2", then you can use the formula
=VLOOKUP(A2;'Set2'!$A$2:$C$4;2;FALSE), where $A$2:$C$4 is the range of Set2, and A2 is the class value from Set1, which is what is used to do the lookup in Set2. The next argument, 2, means to take the second row from Set2, and the FALSE at the end means that you only want exact matches on the CLASS. You can do auto-fill with this formula, and do similar steps for the years. If you look up the help for VLOOKUP within Excel, that should help you to understand how it works.
Your first set of data is essentially your primary set of data that you just want to add attribute columns to. I built this example on Google Sheets which should help explain. Using spill formulas, only a few cells are needed with their own formulas. You can see them as they are highlighted in yellow. When you use in Excel, obviously make sure you change the column references, but this would get you the answer.
Note you have to have SpillRange in Excel for this to work. To test, see if you have the formula =unique()
This solution may work for you if both sets start in the same column. As example in my image, both of them start at column A. You can get all data with a single VLOOKUP formula:
Formula in cell E2 is:
=VLOOKUP($A2;$A$22:$R$25;COLUMN($B22);FALSE)
Notice the mixed references at first and third argument and absolute references in the second one. Third argument is critical, because is the relational position between both sets, that's the reason it's easier if both sets start at same column. If not, you'll need to adjust this argument substracting or adding, depending on the case.
Anyways, with a single formula, you can get any number of columns. The only disavantage of this formula is that you need to manually drag to right until you got all the columns (10, 30 or whatever). You'll notice you are done because the formula will raise an error:
This error means you are trying to get a referenced outside of your column area.

Replace and update columns in excel

Table
A B C D E
1 Jan Feb Mar Apr Jun
2 24 32 64 54 45
3 66 76 76 76 87
4 45 65 84 67 23
the last 3 columns should automatically update in another column as
Last 3 moths data
AN AO AP
1 Month1 Month2 Month3
2 64 54 45
3 76 76 87
4 84 67 23
If I add another column in the first table as july in columns F. Then it should automatically replace the data in Month3 in second table and month3 data sholud move to month2 and month2 to month1.
=IF(AN2="",AL2,IF(AO2="",AM2,IF(AP2="",AN2,IF(AQ2="",AO2,IF(AQ2<>"",AP2,IF(AP2<>"",AO2,IF(AO2<>"",AN2,IF(AN2<>"",AM2,""))))))))
The formula is simlar to the another columns.
With any version of Excel:
You can detect where the columns end using many methods - the simplest is using Count.
Enter the following into the top left of your result table:
=INDEX(2:2,,COUNT(2:2)-2)
This counts the number of cells with data in, and returns the data from that numbered column (offset to the left by 2). In the next cell to its right, change the -2 to -1. In the last cell, change the -1 to -0 or remove that part entirely.
Then copy the cells down to match your source table.
If you have Excel-365 then use below formula.
=INDEX(A1:Z4,SEQUENCE(ROWS(A1:A4)),SEQUENCE(1,3,MAX(IF(A1:Z1<>"",COLUMN(A1:Z1),""))-2))
If you don't have Excel-365 then use below formula then drag down and across as required.
=INDEX($A$1:$Z$4,ROW($A1),MAX(IF($A$1:$Z$1<>"",COLUMN($A$1:$Z$1),""))-3+COLUMN(A$1))
If you have ms365, try to use FILTER(), for example:
Formula in G1:
=FILTER(Table1[#All],COLUMN(Table1[#Headers])>COLUMNS(Table1[#Headers])-3)
If you add columns:
If you remove columns:

Counting combinations of values in table of values excel?

I have a table of values with each value in a different cell
e.g.
2 9 12 19 41 45 14 39
12 14 19 27 39 40 30 44
6 9 13 15 16 41 7 20
8 16 14 34 13 44 5 15
10 11 20 24 27 36 9 41
Is there a way to find which pairs of numbers are most common.
i.e.
13 & 15 appear 2 times
14 & 44 appear 2 times
24 & 27 appear 1 time
I was hoping for a formula as the table has over a hundred rows in it so would be time consuming to count manually.
The formula you would want would be the one for the row totals of a pair of numbers in A2 and B2:
=SUM(MMULT(--($E$2:$L$6=A2),TRANSPOSE(COLUMN(E:L))^0)*MMULT(--($E$2:$L$6=B2),TRANSPOSE(COLUMN(E:L))^0))
entered as an array formula using CtrlShiftEnter
There will be formulas for for listing distinct pairs up to a certain number out there - I used
=IF(ROW()=2,1,IF(B1<45,A1,A1+1))
for A2 and
=IF(B1<45,B1+1,2+COUNTIF(B$1:B1,45))
for B2.
If you scroll down this list you can see that 13,16 occurs twice as well as 13,15.
9,41 occurs three times.
Note - I am assuming the numbers are like lottery numbers and any one number can only occur once in a row. The formula could easily be modified to account for duplicates within a row.

AGGREGAT with critiera and duplicates in array

I have the following Excel spreadsheet:
A B C D E
1 ProdID Price Unique ProdID 1. Biggest 2. Biggest
2 2606639 40 2606639 50 50
3 2606639 50 4633523 45 35
4 2606639 20 3911436 25 25
5 2606639 50
6 4633523 45
7 4633523 20
8 4633523 35
9 3911436 20
10 3911436 25
11 3911436 25
12 3911436 15
In Cells D2:E4 I want to show the 1. biggest and 2. biggest price of each ProdID in Column A. Therefore, I use the following formula:
D2 =AGGREGAT(14,6,$B$2:$B$12/($A$2:$A$12=$C2),1)
E2 =AGGREGAT(14,6,$B$2:$B$12/($A$2:$A$12=$C2),2)
This formula works as long as the prices are unique in Column B as you can see on the second ProdID (4633523).
However, once the price is not unique in Column B (for example 50 for ProdID 26026639 and 25 for ProdID 3911436) the functions in Cells D2:E4 does not show the right results.
Do you have an idea if you can solve this issue with the AGGREGAT-Formula and wihtout using an ARRAY-Formula?
you could check number of occurences of the first ProdID-price combinations and use that in the last argument of the AGGREGAT function. So instead of
=AGGREGAT(14,6,$B$2:$B$12/($A$2:$A$12=$C2),2)
you would have
=AGGREGAT(14,6,$B$2:$B$12/($A$2:$A$12=$C2),2+COUNTIFS(A:A,C2,B:B,D2)-1)
of course you can just put "1+COUNTIFS..." but I put it this way so it can be better understood that it uses position 2 + number of occurences of the combination of ProdID with biggest number after the first occurence.

divide data in one column into more column in excel

i want to do this in Microsoft Excel 2007
This a one column. I have 20098 data in one column like below.
1
2
3
4
5
6
7
8
131
1
31
31
31
31
41
I want to rearrange those data like this how can i do it
1 4 7 1 31
2 5 8 31 31
3 6 131 31 41
If your data was in column A then in cell B1 put
=OFFSET($A1,3*(COLUMN()-COLUMN($B$1)),)
and copy down and right to split your data as desired
use this formula in all 3 rows and 6700 columns of the resulting range:
=INDEX($A:$A;(COLUMN()-first_column)*3 + ROW()-first_row+1)
where first_column is =column(..) and first_row is =row(..) of the cell where you want to have the 1st value
e.g. if you use B1:IWS3 range to list the results, the formula will be:
=INDEX($A:$A;(COLUMN()-2)*3 + ROW())

Resources