Google Sheets - calculating range based based upon variable criteria, with added multiplication - excel

Working from this spreadsheet: https://docs.google.com/spreadsheets/d/1aiZzzOFzPDrw_siMhL8XiNIp3q7f8nz58HvwBKn14uA/edit?usp=sharing
Trying to calculate sum of a column where value in another col is true: this works well, like this:
=SUMIF($B$5:$B$17,"NY",G5:G17)
I'd like to be able to calculate this but, also multiply the range in question by another variable, in column C (i.e. the "rate"), so that I get the sum of each number of hours in column G done by the resources in Col A, multiplied by the rate charged for each resource.
this obviously doesn't work, but this is the type of thing I'm looking for: =SUMIF($B$5:$B$17,"NY",G5:G17*(Corresponding value in Col C))
Any ideas?

SUMIFS will only accept a range so I don't think you can do it that way. The alternative is to use SUMPRODUCT.
In Google sheets:
=sumproduct((B5:B17="NY"),C5:C17,G5:G17)
Excel is slightly more picky: you have to coerce the inner bracket to a number, either
=sumproduct(--(B5:B17="NY"),C5:C17,G5:G17)
or
=sumproduct((B5:B17="NY")*C5:C17*G5:G17)

Related

Excel SUMPRODUCT and dynamic text conditions

I am trying to do a summation of rows with certain dynamic conditions. I have rows like:
A can be only one value, K can have multiple OR-values. In the end M is to be summed.
I have tried to use SUMPRODUCT() which works for column A but not for K. What I am looking for is something like:
=SUMPRODUCT(--(!$A$2:$A$20000="AA")*--(!$K$2:$K$20000="AA" OR "BB")*$M$2:$M$20000)
I know I can do ="AA" and then ="BB" but I need "AA" and "BB" to be dynamic based on other cells. And the number of arguments is different. I tried {"AA";"BB"} but I know this will not work as the match then needs to be in the same row.
Can it at all be achieved?
Thanks a lot!
=SUMPRODUCT(($A$2:$A$20000="AA")*(($K$2:$K$20000="AA")+($K$2:$K$20000="BB"))*$M$2:$M$20000)
Note that:
Since you are multiplying/adding arrays, there's no need to include the double unary's
I don't know why you have a ! in your example formula.
To return an OR array of TRUE;FALSE, we add.
Your comments still do not provide a clear explanation of what you are making dynamic.
But to create a dynamic OR for column K, including testing for column A and summing column M, you can do the following:
For column K, let us assume that your possible OR's are entered separately in the range F2:F10
=SUMPRODUCT(MMULT(--($K$2:$K$20000=TRANSPOSE($F$2:$F$10)),--(ROW($F$2:$F$10)>0))*($A$2:$A$20000="AAA")*$M$2:$M$20000)
The matrix multiplication will produce a single column of 19,999 entries which will be a 1 for matches of any of the OR's and 0 if it does not match.
See How to do a row-wise sum in an array formula in Excel?
for information about the MMULT function in this application.
In the above formula, "blanks" in the OR range (F2:F10) will also match blank entries in column K. So it is conceivable that if there is a blank in K and F and a AAA in col A and a value in column M that a wrong result might be returned.
To avoid that possibility, we can use a dynamic formula to size column F where we are entering our OR values:
=INDEX($F$2:$F$10,1):INDEX($F$2:$F$10,COUNTA($F$2:$F$10))
will return only the values in col F that are not blank (assuming no blanks within the column)
So:
=SUMPRODUCT(MMULT(--($K$2:$K$20000=TRANSPOSE(INDEX($F$2:$F$10,1):INDEX($F$2:$F$10,COUNTA($F$2:$F$10)))),--(ROW(INDEX($F$2:$F$10,1):INDEX($F$2:$F$10,COUNTA($F$2:$F$10)))>0))*($A$2:$A$20000="AAA")*$M$2:$M$20000)
Given this data:
the last formula will return a value of 5 (sum of M2,M3,M7)
Use SUMIFS with SUMPRODUCT wrapper:
=SUMPRODUCT(SUMIFS($M$2:$M$20000,$A$2:$A$20000,"AA",$K$2:$K$20000,{"AA","BB"}))

Condensing nested if-statements with multiple criteria

The blue columns is the data given and the red columns is what is being calculated. Then the table to the right is what I am referencing. So, F2 will be calculated by the following steps:
Look at the Machinery column (D), if the cell contains LF, select column K, otherwise select column L
Look at the Grade column (E), if the cell contains RG, select rows 4:8, otherwise select rows 9:12.
Look at the Species column (A), if the cell contains MS, select rows 5 and 10, otherwise.......
Where every the most selected cell is in columns K and L, copy into column F.
Multiply column F by column C.
I don't want to make another column for my final result. I did in the picture to show the two steps separately. So column F should be the final answer (F2 = 107.33). The reference table can be formatted differently as well.
At first, I tried using nested-if statements, but realized that I would have like 20+ if statements for all the different outcomes. I think I would want to use the SEARCH function to find weather of not the cell contains a specific piece of information. Then I would probably use some sort of combination of match, if, v-lookup, index, search, but I am not sure how to condense these.
Any suggestion?
SUMPRODUCT is the function you need. I quickly created some test data on the lines of what you shared like this:
Then I entered the below formula in cell F2
=SUMPRODUCT(($I$4:$I$9=E2)*($J$4:$J$9=LEFT(A2,FIND(" ",A2)-1))*IF(ISERROR(FIND("LF",D2,1)),$L$4:$L$9,$K$4:$K$9))
The formula may look a little scary but is indeed very simple as each sub formula checks for a condition that you would want to evaluate. So, for example,
($I$4:$I$9=E2)
is looking for rows that match GRADE of the current row in range $I$4:$I$9 and so on. The * ensures that the arrays thus returned are multiplied and only the value where all conditions are true remains.
Since some of your conditions require looking for partial content like in Species and Machine, I have used Left and Find functions within Sumproduct
This formula simply returns the value from either column K or L based on the matching conditions and you may easily extend it or add more conditions.

Excel AVERAGEIFS with OR

Currently have an AVERAGEIFS formula where:
Col A is Arrival to Last Lab Results (POC Glu, POC INR) with the data being numbers
Col B is Activation Type in this case being "EMS (pre-hospital)"
Col C is poc_inr not needed which is either "Yes" or "No"
The formula is applied for a date range of 1 month.
The formula is pasted below:
=AVERAGEIFS(MM[Arrival to Last Lab Results (POC Glu, POC INR)], MM[Arrival Date/Time],">="&A30,MM[Arrival Date/Time],"<="&B30,MM[Activation Type],"EMS (pre-hospital)", MM[poc_inr not needed], "Yes")
Currently, the formula is only looking at cases where [poc_inr not needed] is a "Yes."I wanted a way to incorporate an OR statement where if the [poc_inr not needed] was a "Yes", then the formula would continue to take the values in Col A but if [poc_inr not needed]was a "No" the formula would instead take a look at the values in another column altogether (let's name it Col D for simplicity's sake) and provide me the average of all the numerical values taken from Col A and Col D. All the other criteria and criteria range stay as is.
I have heard that AVERAGEIFS does not allow for OR functions, so I was wondering if anyone had a solution/alternative way of approaching this?
Don't use OR, specify different criteria for each case and just add them together. You will be better off using SUMPRODUCT and named ranges/tables to make your formula easier to read:
You should be able to adjust it without many problems:
=(SUMPRODUCT((Table1[poc_inr]="Yes")*(Table1[Arrival]>=G2)*(Table1[Arrival]<=H2)*Table1[Last Lab Results])+SUMPRODUCT((Table1[poc_inr]="No")*(Table1[Arrival]>=G2)*(Table1[Arrival]<=H2)*Table1[Other result]))
/(SUMPRODUCT((Table1[poc_inr]="Yes")*(Table1[Arrival]>=G2)*(Table1[Arrival]<=H2))+SUMPRODUCT((Table1[poc_inr]="No")*(Table1[Arrival]>=G2)*(Table1[Arrival]<=H2)))

Want SUMIFS to compare two columns row by row

I wasn't sure how to explain it in the title, but here's my problem:
I want to sum the values of every row in column A when, in the same row, the value in col B is equal to the value in col C. For example =SUMIFS(A:A,B:B,"="&C2) seems to work, but I don't want to compare to any specific cell, but the C-cell on that specific row, where the potential A-value is.
Edit: I could of course make an additional column with "If B2=C2" and so on by dragging it down, but this is what I want to avoid.
I also need to know the number of rows that is included in the sum.
Try:
= SUMPRODUCT((A:A)*((B:B)=(C:C)))
But you should limit the full ranges to where ever your data ends, e.g.
= SUMPRODUCT((A1:A100)*((B1:B100)=(C1:C100)))
I would suggest to limit the ranges but this array formula works:
=SUMPRODUCT(A:A,IF(B:B=C2,B:B))
This must be applied with Ctrl+Shift+Enter.
But now that I read your question again it seems that you just need:
=SUMIFS(A:A,B:B,C2)

Need to find the closest greater number in a list filtered by a numeric value in another column

I need to create an Excel formula that would do the following operation:
Whenever there is a particular number in the column Number then then the formula should compare the corresponding value in Target column with the value in Code column and flag in the Expected Output all rows, where Code greater and nearest value in column Target.
What you describe could be easily done in SQL database lanuguage using the GROUP BY statement.
You need formula and Excel does not directly support this, because it is not an SQL database.
However it can be done quite easily with formulas. There are more ways how to achieve that, I can show you, how I would do it. I have used array formula MIN(IF(... which works in a similiar way as SELECT MIN(A) FROM range GROUP BY B
I would create 2 helper columns (D and E) - they could be eventually placed on a separate sheet, if necessary - see picture bellow.
In the column D place formula: =IF(B1-C1>0,B1-C1,65535) (65535 is a little hack for a relatively very high number)
In the column E place formula: =MIN(IF(A$1:A$12=A1,D$1:D$12))as array formula Ctrl+Shift+Enter A$1:A$12 and D$1:D$12 are ranges of your data
In the column F you get your Expected output: =IF(E1=D1,1,0)
So in the column E you get minimum difference between C and B that is > 0 filtered by A.
When working with Excel data in this way I recommend using named ranges instead of A$1:A$12 etc.

Resources