SUMIF of multiple columns with INDIRECT - excel

I have a Excel sheet which is used as database, let's call that MyDB in the following Example. The first column A consists of some strings.
A | B | C
-----------------|--------------|------------------------------------------
Turnover 2014 | 1 | 2
Something | 2 | 0
Something | |
Turnover 2014 | 3 | 1
Something | |
Something | 0 | 2
What I want to do is look for the string Turnover 2014 and sum all values in that row from B:C (C is just an example in my case it will be variable and can be F or M).
What I have:
=SUMIF(INDIRECT("'MyDB'!A"&Helper!D2&":"&"A"&Helper!D8),"=Turnover 2014",INDIRECT("'MyDB'!$B"&Helper!D2&":"&"B"&Helper!D8))
The Helper!D2 and Helper!D8 contain the variable range, which is one of the reasons I have to use INDIRECT. For this example lets assume D2 = 1 and D8 = 6 (the full table)
Simple version:
=SUMIF(INDIRECT("'MyDB'!A1:A6"),"=Turnover 2014",INDIRECT("'MyDB'!B1:B6"))
This sums all values in B where A = Turnover 2014, so no problem here. Now I will show you my attempts to do the same with multi-columns:
=SUMIF(INDIRECT("'MyDB'!A1:A6"),"=Turnover 2014",INDIRECT("'MyDB'!B1:C6"))
=SUMPRODUCT((INDIRECT("'MyDB'!A1:A6") = "Turnover 2014")*(INDIRECT("'MyDB'!B1:C6")))
Both didn't work in my case (IMPORTANT I'm not talking about the simplified version I'm talking about the original version with all the variables).
In all cases I only get the sum of 4 where I need 7

Check whether your column A contains Turnover 2014 without leading/trailing spaces.
And try:
=SUMPRODUCT(
(TRIM(INDIRECT("'MyDB'!A"&Helper!D2&":"&"A"&Helper!D8)) = "Turnover 2014")*
(INDIRECT("'MyDB'!B"&Helper!D2&":"&"C"&Helper!D8))
)
also I suggest you to take a look at alternative formula without INDIRECT which is much better because it's not volatile formula:
=SUMPRODUCT(
(TRIM(INDEX(MyDB!$A:$A,Helper!D2):INDEX(MyDB!$A:$A,Helper!D8))="Turnover 2014")*
(INDEX(MyDB!$B:$B,Helper!D2):INDEX(MyDB!$C:$C,Helper!D8))
)

Related

Countif criteria with IF/OR logic across multiple sheets and columns

I've been struggling between the SUMPRODUCT and COUNTIFS formulas as there are a lot of specific dependencies in my data. Wondering if anyone can shed a bit more light on this issue.
Have tried SUMPRODUCT and COUNTIFS which give me calculations based on 1 set, but I need to include additional if/or statements.
I have the following:
| ID | Size | Dead/Alive | Duration | Days | Pass/Fil | Reason |
|----|---------|------------|-----------|------|----------|----------|
| 1 | Full | Dead | Permanent | 125 | Pass | Comments |
| 2 | Partial | Alive | Permanent | 500 | Pass | |
| 3 | Other | Dead | Temporary | 180 | Fail | Comments |
| 4 | No | Dead | Temporary | 225 | Fail | Comments |
| 5 | Yes | Alive | Permanent | 200 | Pass | |
with the following rules:
Only Count the ID/ROW if:
1) Values in column A = Full, Partial or Other
OR...
2) Values in column A = No AND values in column B = Dead
OR...
3) If values in column C = Permanent AND values in column D = >=100 or <=200
OR
4) If values in column C = Temporary AND values in column E = Pass, Fail AND column F=not blank
By my calculations, the total should be 5, but this is just a small sampling of my total data. Just not sure how to get that in Excel with either Sumproduct, Countifs or even someone suggested a Lookup function, although Ive never used that one.
Given that you have so many different conditions, I have to break it down one by one and create a few helper columns to account for each condition.
In my solution I created 10 helper columns as shown below, and I have added some sample data (ID 6 to 29) to test the solution.
I also named 7 conditions in my solution:
Cond_1 Values in column A = Full, Partial or Other
Cond_2 Values in column A = No AND values in column B = Dead
Cond_3A Values in column C = Permanent
Cond_3B Values in column D >=100
Cond_3C Values in column D <=200
Cond_3A, Cond_3B and Cond_3C must be TRUE at the same time
Cond_4 Values in column C = Temporary AND values in column E = Pass
Cond_5A Values in column C = Temporary AND values in column E = Fail
Cond_5B Column F is not blank (I did not give a name to this condition)
Cond_5A and Cond_5B must be TRUE at the same time
Please note my Cond_4, Cond_5A and Cond_5B are all related to your original condition 4), which reads a bit odd, and I am not 100% sure if my interpretation of the condition is correct. If not please re-state your last condition and I can amend my answer accordingly.
As shown in my screen-shot, the formulas in I2 to Q2 are listed in Column U. I only used MAX, AND, SUM, =, &, and/or <> to interpret each condition. Please note some of the formulas are Array Formula so you need to press Ctrl+Shift+Enter to make it work.
The To Count column is simply asking whether the SUM of the previous 9 columns is greater than 1, which means at least one of the conditions is met. If so returns 1 otherwise 0.
Then you just need to work out the total of To Count column. In my example it is 22. I have highlighted the entries that did not meet any of the given condition.
You can use only one helper column to capture all conditions in one formula, but I would not recommend it as it would be too long to be easily understood and modified in future.
{=--(SUM(MAX(--(A2=Cond_1)),MAX(--(A2&B2=Cond_2)),--(SUM(--(C2=Cond_3A),--(AND(D2>=Cond_3B,D2<=Cond_3C)))=2),MAX(--((C2&E2)=Cond_4)),--(SUM(MAX(--((C2&E2)=Cond_5)),--(F2<>""))=2))>0)}
Ps. I would also wonder if there is a formula-based solution without using any helper column...? :)

Excel: Average of parts of a column based on another column

Using Excel 2011 for mac, I have three columns: ID, Start date, end date and time to completion. (Date format: dd/mm/yyyy)
ID | Start | End | Time
1 | 01/01/2016 | 05/01/2016 | 4
2 | 04/01/2016 | 08/01/2016 | 4
3 | 01/02/2016 | 14/02/2016 | 13
4 | 02/02/2016 | 20/02/2016 | 18
5 | 01/03/2016 | 05/03/2016 | 4
6 | 06/03/2016 | 08/03/2016 | 2
7 | 12/03/2016 | 15/03/2016 | 3
Column D is basically the difference between column C and column B.
Now I have the total average, which is easy to calculate but I'd also like to have the averages for the different months.
And this is where my knowledge falls apart. I've tried several things but I can't seem to figure out how to calculate an average using only the cells in a column that match a certain value in another column. I could sort the tickets by date and do it manually by doing the average for only a certain range but as this list constantly changes this is definitely not a nice option.
Check out the AVERAGEIF function
AVERAGEIF(selection_range, criteria, averaging_range)
It uses the values in the selection_range to filter which values in the averaging_range will be averaged.
In your case you could say AVERAGEIF(B1:B8, "01/01/2016", D1:D8)
There are multiple ways. I would personally use an array formula, but that may be a bit advanced and overly complicated.
I suggest adding a column E "Month" - into E2 add:
=MONTH(B2)
Then copy cell E2 to E3:E8.
Now you can easily get a monthly average by applying the AVERAGEIF command:
=AVERAGEIF(E2:E8,2,D2:D8)
The second argument, 2, indicates February, but may be exchanged with any number from 1-12.

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 the number of older siblings in an Excel spreadsheet

I have a longitudinal spreadsheet of adolescent growth.
ID | CollectionDate | DOB | MOTHER ID | Sex
1 | 1Aug03 | 3Apr90 | 12 | 1
1 | 4Sept04 | 3Apr90 | 12 | 1
1 | 1Sept05 | 3Apr90 | 12 | 1
2 | 1Aug03 | 21Dec91 | 12 | 0
2 | 4Sept04 | 21Dec91 | 12 | 0
2 | 1Sept05 | 21Dec91 | 12 | 0
3 | 1Aug03 | 30Jan89 | 23 | 0
3 | 4Sept04 | 30Jan89 | 23 | 0
This is a sample of how my data is formatted and some of the variables that I have. As you can see, since it is longitudinal, each individual has multiple measurements. In the actual database there are over 10 measurements per individual and over 250 individuals.
What I am wanting to do is input a value signifying the number of older brothers and older sisters each individual has. That is why I have included the Mother ID (because it represents genetic relatedness) and sex. These new variable columns would just say how many older siblings of each sex each individual has. Is there a formula that I could use to do this quickly?
=COUNTIFS($B:$B,"<>"&$B2,$H:$H,$H2,$AI:$AI,$AI2,$J:$J,"<"&$J2)
Create a column named Distinct with this formula
=1/COUNTIF([ID],[#ID])
Then you can find all the older 0-sexed siblings like this
=SUMPRODUCT(([DOB]>[#DOB])*([MOTHERID]=[#MOTHERID])*([Sex]=0)*([Distinct]))
Note that I made the data a Table and used table notation. If you're not familiar [COLUMNNAME] refers to the whole column and [#COLUMNNAME] refers to the value in that column on the current row. It's similar to saying $A:$A and A2 if you're dealing with column A.
The first formula gives you a value to count that will always result in 1 for a particular ID. So ID=1 has three lines and Distinct will result in .33333 for each line. When you add up the three lines you get 1. This is similar to a SELECT DISTINCT in Sql parlance.
The SUMPRODUCT formula sums [Distinct] for every row where the DOB is greater than the current DOB, the Mother is the same as the current Mother, and the Sex is zero.
I have a possible solution. It involves adding two columns -- One for "# older siblings" and one for "unique?". So here are all the headings I have currently:
A -- ID
B -- CollectionDate
C -- DOB
D -- MOTHER ID
E -- Sex
F -- # older siblings
G -- unique?
In G2, I added the following formula:
=IF(A2=A1,0,1)
And dragged down. As long as the data is sorted by ID, this will only display "1" once for each unique person.
In F2, I added the following formula:
=COUNTIFS(G:G,"=1",D:D,"="&D2,C:C,"<"&C2)
And dragged down. It seemed to work correctly for the sample data you provided.
The stipulations are:
You would need the two columns.
The data would need to be sorted by ID
I hope this helps.
You need a formula like this (for example, for row 2):
=COUNTIFS($A:$A,"<>"&$A2,$E:$E,$E2,$D:$D,$D2,$C:$C,"<"&$C2)
Assuming E:E is column for sex, D:D is column for mother ID and C:C is column for DOB.
Write this formula in H2 cell for example and drag it down.

Conditional sum

I need a quick macro/formula. I have two columns:
A | B
|
| 10 | 9 |
| 11 | |
| 8 | 10 |
| N/A |
| 4 | |
| 7 | 8 |
total | 40 | 27 |
Now I want to add a sum to a column that adds up all the values in column A BUT replaces that value with the value in column B if it exists.
So I would expect the sum to end up being 9 + 11 + 10 + 4 + 8 = 42
I'm not entirely sure about the power of Excel's calculations or how to go about performing a for loop within a formula.
There are MANY ways to do this, but I would do it as two sums added together....
Assuming you had the data you supplied in your question in cells A1 to B6, your formula could look as follows:
=SUMIF(B1:B6,"",A1:A6)+SUM(B1:B6)
In essence, you're saying:
Sum A1:A6 anywhere B1:B6 is blank
PLUS
Sum B1:B6
Hope this makes sense and helps!
=SUM(IF(ISNUMBER(B1:B5),B1:B5,A1:A5))
will do this. Note that the formula is an array formula though; once you've finished entering it in a cell, press Ctrl + Shift + Return, rather than Return. That tells Excel that the formula is an array type.
I've set this up for 5 rows; just extend as you need.
One of many other possibilities could be a helper column containing:
=IF(ISBLANK(B3),A3,B3)
copied down (and up, if necessary!)
Not necessarily better than many alternatives but perhaps easier to see 'what is going on' and =IF may already be familiar.

Resources