excel - sumifs formula with range on criteria - excel

I have these data
A B C D
1 lvl1 lvl2 lvl3 value
2 1 1.1 1.1.1 3
3 1 1.1 1.1.2 2
4 1 1.2 1.1.3 7
5 2 2.1 2.1.1 2
6 2 2.1 2.1.2 3
and i want the output of the formula to be like this
7 Type LEVEL value
8 1 level1 12
9 1.1 level2 5
10 1.2 level2 7
11 2 level1 5
12 2.1.1 level3 2
I have already implemented this request with sumifs (cause i have more than one criteria in the original case) by putting in "criteria range" nested if
sumifs(D2:D6,IF(B8="level1",A2:A6),IF(B8="level2",B2:B6),IF(B8="level3",C2:C6))))
Is there any other way (perhaps with index & match?) to have the same result?

If your Levels are exactly the same name as your column headers (so lvl1 instead of level1), then you can use this formula in cell C8 and copied down:
=SUMIFS($D$2:$D$6,INDEX($A$2:$C$6,0,MATCH(B8,$A$1:$C$1,0)),A8)

Put these formulas into H2:I2.
="level"&LEN(G2)-LEN(SUBSTITUTE(G2, ".", ""))+1
=SUMIFS(D:D, INDEX(A:C, 0, LEN(G2)-LEN(SUBSTITUTE(G2, ".", ""))+1), G2)
Fill down.

Related

Excel Lag Two Group

STUDENT
TIME
CLASS
SCORE
WANT
1
1
A
13
NULL
1
1
B
4
NULL
1
2
A
11
-2
1
2
B
9
5
1
3
A
8
-3
2
2
B
16
NULL
2
3
B
6
-10
2
4
A
7
NULL
2
4
B
6
0
I have XLSX file with STUDENT, TIME, CLASS, SCORE. I wish to calculate WANT which does this:
For every STUDENT and CLASS, calculate the difference in SCORE from TIME(X) TO TIME(X-1).
for STUDENT=1, TIME=2,CLASS=B equals to 5 because it is (9-4)
I try this with no success:
=IF(A3=A2 & C3=C2, OFFSET(D3, -1, 0), "")
I think you can try:
Formula in E2:
=IF(COUNTIFS(A$2:A2,A2,C$2:C2,C2)>1,D2-SUMIFS(D:D,A:A,A2,B:B,B2-1,C:C,C2),"Null")
It is far from the best approach, but it works.
If using helper column is not a problem, you can make additional column for VLOOKUP (see column "Helper1") with formula =TEXTJOIN("-",,A2:C2).
Now use VLOOKUP to find value TEXTJOIN("-",,A2,B2-1,C2) in that column. Formula in "WANT" column: IFNA(E2-VLOOKUP(TEXTJOIN("-",,A2,B2-1,C2),$D$2:$E$10,2,FALSE),"NULL")
Result:

Deleting a row with VBA in Excel gives invalid reference in another table

I have an Excel Workbook with 2 sheets. Each sheet contains a formatted table.
The tables start at row2 and feature an internal numbering in columnA achieved by the formulas
=Row()-Row(talble1)+1 and =Row()-Row(talble2)+1
sheet2:
In table2/columnB I concatenate 2 of the internal row indices from table1 using the formulas
table2/internal row1: =CONCATENATE(sheet1!A2;";";sheet1!A3;";")
table2/internal row2: =CONCATENATE(sheet1!A4;";";sheet1!A5;";")
table2/internal row3: =CONCATENATE(sheet1!A6;";";sheet1!A7;";")
table2/internal row4: (...)
It looks like this:
sheet1/table1: sheet2/table2:
A B C A B C
1 1
2 1 2 1 1;2
3 2 3 2 3;4
4 3 4 3 5;6
5 4 5
6 5 6
7 6 7
8 8
9 9
If I "manually" delete the 2 rows with internal indices 3 and 4 in table1, the numbering automatically adjusts. Also, the references in internal row2/columnB of table2 become invalid, which makes total sense. The formula of internal row3 in table2 automatically adjusts to the deletion in table1 and remains valid.
It then looks like this:
sheet1/table1: sheet2/table2:
A B C A B C
1 1
2 1 2 1 1;2
3 2 3 2 invalid
4 3 4 3 3;4
5 4 5 4
6 6 5
7 7 6
8 8
9 9
Now comes the issue:
If I do the deletion described above using a VBA macro, the references in internal row3 of table2 become invalid as well! When I check the formula I see that it still references cells A6 and A7 which don't exist anymore in table1.
I used the code
sheet1.ListObjects("table1").DataBodyRange.Rows(4).Delete Shift:=xlUp
sheet1.ListObjects("table1").DataBodyRange.Rows(3).Delete Shift:=xlUp
It looks like this:
sheet1/table1: sheet2/table2:
A B C A B C
1 1
2 1 2 1 1;2
3 2 3 2 invalid
4 3 4 3 invalid
5 4 5 4
6 6 5
7 7 6
8 8
9 9
What is the explanation for the different behaviour of deleting a row in table1 manually or by VBA code?
I also recorded macros when deleting the rows manually and I could not detect anything that explains why my macro code leads to the invalid references in internal row3 of table2.

Sumif with only first value of each group in column

I have a dataset similar to this, but really extensive:
Row
Levels
Level 1
Size
Department
1
1
AA
2.0
Dept 1
2
2
AA
0.8
Dept 1
3
3
AA
1.5
Dept 1
4
2
BB
3.0
Dept 1
5
3
BB
2.0
Dept 1
6
3
BB
2.5
Dept 2
7
2
CC
5.0
Dept 2
8
3
CC
1.5
Dept 2
9
3
DD
0.5
Dept 2
10
3
DD
3.0
Dept 2
11
2
EE
4.0
Dept 2
12
3
EE
2.0
Dept 2
What I need is to achieve a total size per Department, however I want to sum only the first match per Level 1, i.e.:
Department 1 would be 2.0 (row 1) + 3.0 (row 4) = 5.0
Department 2 would be 2.5 (row 6) + 5.0 (row 7) + 0.5 (row 9) + 4.0 (row 11) = 12.0
Does anyone have any idea how to accomplish this in Excel?
Alternate solution to the same formula:
=SUM(XLOOKUP(UNIQUE(FILTER(C:C,(ROW(C:C)>1)*(E:E=#$F$2#))&#$F$2#),C:C&E:E,D:D))
Where F2 holds =UNIQUE(FILTER(E:E,(ROW(E:E)>1)*(E:E<>"")))
If you have Excel 365, you could try something like this:
=LET(FilteredLevel,FILTER(C$2:C$13,E$2:E$13=H2),
SUM(XLOOKUP(UNIQUE(FilteredLevel),FilteredLevel,FILTER(D$2:D$13,E$2:E$13=H2))))
Note
You can also use full-column references if you wish
=LET(FilteredLevel,FILTER(C:C,E:E=H2),
SUM(XLOOKUP(UNIQUE(FilteredLevel),FilteredLevel,FILTER(D:D,E:E=H2))))
SUMIFS() will not do what you want. Use SUMPRODUCT() with some boolean:
=SUMPRODUCT($C$2:$C$13*($D$2:$D$13=F2)*(COUNTIFS(OFFSET($B$2,0,0,ROW($B$2:$B$13)-1),$B$2:$B$13,OFFSET($D$2,0,0,ROW($B$2:$B$13)-1),F2)=1))
One note, the use of OFFSET() makes this a volatile function, meaning that it will recalc with every change made to excel. If there are too many then it will slow down the responsiveness in Excel.
To do it without the volatility we need a helper column. In E2 put:
=COUNTIFS($D$2:D2,D2,$B$2:B2,B2)=1
And copy down. Then we can use SUMIFS():
=SUMIFS(C:C,D:D,F2,E:E,TRUE)

Formula to transpose horizontal numeric data vertical in excel

My input data in column A
1
2
3
4
5
6
7
8
9
If I want the above in Column B C D like
1 2 3
4 5 6
7 8 9
Use INDEX with some math:
=INDEX($A:$A,(ROW($A1)-1)*3+COLUMN(A$1))
Put in B1 copy over 3 columns and down 3 rows.
The *3 is the number of columns desired.

vlookup with 2 columns in excel

I am having an issue with vlookup using 2 columns. I have the following 2 columns in my base excel sheet
ID_1 ID2
2 4
7 9
and my target sheet has these values
ID spend
2 20
4 30
7 10
1 5
9 10
When I run my vlookup, I would like to get this result
ID_1 ID2 Total_Spend
2 4 50
7 9 20
I am using something like this for Total_Spend, but it is not working..
=iferror(vlookup,0)+iferror(vlookup,0)
In this case you can use SUMIF()
Here the formula in D4 is:
=SUMIF($B$9:$B$13,B4,$C$9:$C$13)+SUMIF($B$9:$B$13,C4,$C$9:$C$13)

Resources