EXCEL Averaging with multiple Criteria - excel

I have tried 2 approaches to my problem and can't quite figure out where I'm going wrong.
=AVERAGEIFS(CALC!L:L,CALC!C:C,Consignee!A2,CALC!K:K,CALC!A:A) and =IF(AND(Consignee!A2=CALC!C:C,CALC!K:K=CALC!A:A),AVERAGE(CALC!L:L),0)
Basically I need to start with CONSIGNEE!A2, then find it's match in CALC!C:C, which I then need to check against CALC!A:A to find the ones that go with it in the same row and see if that/those cell(s) match any in CALC!K:K and then take the average of the corresponding values in CALC!L:L and average them. I hope that makes sense. I feel like I've gotten close but am missing something.
Thank you in advance for your help!

I suspect that the formula below may not be the most efficient possible but it will do the job.
=SUMPRODUCT((CALC!A:A=CALC!K:K)*(CALC!C:C=CONSIGNEE!A2),CALC!L:L)/SUMPRODUCT((CALC!A:A=CALC!K:K)*(CALC!C:C=CONSIGNEE!A2))
Edit
In my original answer a reference to L1 was erroneously left over from the formula I tested where I had L1 taking the place of CONSIGNEE!A2. I have corrected this error and believe that the formula works fine for your requirement now.
Note that there are two separate SUMPRODUCT functions which you can test each by itself, one returning the count, the other the sum. If the count = 0 a #DIV/0 error will occur. You can prevent that by embedding the formula in and IFERROR() function.

Related

Combining IF and MAX

Please look at the formula below. Right now, depending on the dates the end user enters in C4 or C6, the result could come back negative, but I'd like for it to say "if negative, then 0%".
I've never been one for Nested If statements as they just seem to never work quite as well as they should and break pretty easy. I was wondering if anyone had any better ideas on how to keep this formula but maybe add MAX to it? Or maybe see if there's just a better way overall to do it?
The IF statement is basically saying "if these other cells are filled out (D column), then use those dates, if not, use the first dates (C column)."
Using Office 365.
Thanks for any help!
=IFERROR(IF(OR(D4="",D6=""),((TODAY()-$C$4)*(100/($C$6-$C$4)))/100,((TODAY()-$D$4)*(100/($D$6-$D$4)))/100),"0%")
You can just wrap the IF function in MAX. Essentially just return either the calculation or 0, whichever is higher.
=IFERROR(MAX(IF(OR(D4="",D6=""),((TODAY()-$C$4)*(100/($C$6-$C$4)))/100,((TODAY()-$D$4)*(100/($D$6-$D$4)))/100),0),"0%")

COUNTIF formula counts values that don't match

I'm using counting invoice numbers (text) in a table's column, but the Excel formula seems to be confusing some values.
I copied small sample of these - please refer to below:
The formulas are as follow:
=COUNTIFS(A1:A19,A1)
=COUNTIF(A1:A19,A1)
As you can see these invoice numbers differ and the results of these functions suggest as if all were the same.
I googled it for 1 hour but I didn't find such as issue as mine.
If anybody had any clue why could this behave in such way I'll be super grateful!
Rob
Each time you copy down this formula it will add 1 row to each. For example the second row of datas formula will be =COUNTIFS(A2:A20,A2). To lock these cells in the formula use $
Your formula should be =COUNTIFS(A$1:A$19,A1)
I've solved this myself:
ROOTCAUSE
Excel tried to be helpful and read these invoice numbers as actual numbers (despite these being defined already in Power Query as text)
Then, Excel fooled me and despite showing that it works on it as a string (I was evaluating the formula) it worked on it as number
Above means that it transformed exemplary "00100001010000018525" to 1.00001E+17, which cut down this to "100001010000018000" - that's the moment Excel stopped fooling around and showed that value in the formula bar.
I think I don't need to tell why countif perceived all these values as equal.
SOLUTION
I simply appended one letter after each invoice number to get e.g. "00100001010000018525a" what forces Excel to quit its gimmicks and games.
Case closed.
I suspect this is a bug in COUNTIF, or maybe by design.
However, to workaround this in the formula, without having to change your data, try adding a wild-card character:
=COUNTIF(A1:A19,"*"&A1)

Excel: If vlookup returns specific text, countifs number of instances (multiple)

First post, but I am stuck and was hoping for some help. What I am trying to do is use a nested IF formula to determine whether the value returned by a VLOOKUP matches specific text on another sheet in the same workbook. If it does, to then use the COUNTIFS function to determine the sum of instances that specific text contained in a specific cell appears on another sheet (also in the same workbook).
One part of these formulas seems to work, but when I combine it with others, it errors and I can't figure out why.
It's probably something simple, but I'd appreciate any help. Here's the part that seems to "work"
=IF(VLOOKUP($A3,'Sheet1'!$B:$D,3,FALSE)="All",
(COUNTIFS('Sheet2'!$U:$U,'Sheet3'!$A3,'Sheet2'!$C:$C,"MM/DD/YYYY")
+
(COUNTIFS('Sheet2'!$R:$R,'Sheet3'!$A3,'Sheet2'!$C:$C,"MM/DD/YYYY")))
If I put one more parenthesis, it works as is, but seems to omit some of the "All" Vlookup results... thus why I put works in quotations above. That might be an issue with my spreadsheet though, but felt I should include in case I'm just being idiotic. Regardless, when I input two additional IF statements, it errors.
The final formula I've crafted looks like this (double spaced for ease of reading only):
=IF(VLOOKUP($A3,'Sheet1'!$B:$D,3,FALSE)="All",
(COUNTIFS('Sheet2'!$U:$U,'Sheet3'!$A3,'Sheet2'!$C:$C,"MM/DD/YYYY")
+
(COUNTIFS('Sheet2'!$R:$R,'Sheet3'!$A3,'Sheet2'!$C:$C,"MM/DD/YYYY")))),
IF((VLOOKUP($A3,'Sheet1'!$B:$D,3,FALSE)="One",
(COUNTIFS('Sheet2'!$U:$U,'Sheet3'!$A3,'Sheet2'!$C:$C,"MM/DD/YYYY")),
IF((VLOOKUP($A3,'Sheet1'!$B:$D,3,FALSE)="Two",
(COUNTIFS('Sheet2'!$R:$R,'Sheet3'!$A3,'Sheet2'!$C:$C,"MM/DD/YYYY"),
IF((VLOOKUP($A3,'Sheet1'!$B:$D,3,FALSE)="Three",
(COUNTIFS('Sheet2'!$U:$U,'Sheet3'!$A3,'Sheet2'!$C:$C,"MM/DD/YYYY")),"0"))
Any ideas?
Thanks!
Ryan Olson
Just cleaned up some bracketing. Let me know how it goes as I didn't alter the formula otherwise.
=IF(VLOOKUP($A3,Sheet1!$B:$D,3,FALSE)="All",
COUNTIFS(Sheet2!$U:$U,Sheet3!$A3,Sheet2!$C:$C,"MM/DD/YYYY")+(COUNTIFS(Sheet2!$R:$R,Sheet3!$A3,Sheet2!$C:$C,"MM/DD/YYYY")),
IF(VLOOKUP($A3,Sheet1!$B:$D,3,FALSE)="One",
COUNTIFS(Sheet2!$U:$U,Sheet3!$A3,Sheet2!$C:$C,"MM/DD/YYYY"),
IF(VLOOKUP($A3,Sheet1!$B:$D,3,FALSE)="Two",
COUNTIFS(Sheet2!$R:$R,Sheet3!$A3,Sheet2!$C:$C,"MM/DD/YYYY"),
IF(VLOOKUP($A3,Sheet1!$B:$D,3,FALSE)="Three",
COUNTIFS(Sheet2!$U:$U,Sheet3!$A3,Sheet2!$C:$C,"MM/DD/YYYY"),"0"))))

How to calculate multiple cells and get parts of the formula to ignore "#DIV/0" as a value

I am struggling to find an answer for this because other examples I have seen they are summing together a range in the examples given, the code I have here is this:
=(((H8/F8)*K8)+((N8/F8)*Q8)+((T8/F8)*W8)+((Z8/F8)*AC8)+((AF8/F8)*AI8))
Just an overview, the equation above is meant to work out a percentage and add it with the next percentage in the formula.
This works if all the cells I'm calculating contain numbers because its just a basic calculation but each section in this equation e.g. ((h8/f8)*k8) - could equal "#DIV/0!" (because in some cases, there is no percentage value that needs to be calculated here), I know why it is doing this, but I am just wondering, is there a way to make each part of the equation ignore the #DIV/0 if there is one present?
With the idea given from an above (#Gary's Student) answer I have solved this now, the formula I used was this:
=SUM(IFERROR((H8/F8)*K8,0)+IFERROR((N8/F8)*Q8,0)+IFERROR((T8/F8)*W8,0)+
IFERROR((Z8/F8)*AC8,0)+IFERROR((AF8/F8)*AI8,0))
This has given me the exact answer that I wanted although it is a little messy.
Replace:
=(((H8/F8)*K8)+((N8/F8)*Q8)+((T8/F8)*W8)+((Z8/F8)*AC8)+((AF8/F8)*AI8))
with:
=IFERROR((((H8/F8)*K8)+((N8/F8)*Q8)+((T8/F8)*W8)+((Z8/F8)*AC8)+((AF8/F8)*AI8)),"")

Trying to improve efficiency of array formula

I have a SUM array formula that has multiple nested IF statements, making it very inefficient. My formula spans over 500 rows, but here is a simple version of it:
{=SUM(IF(IF(A1:A5>A7:A11,A1:A5,A7:A11)-A13:A17>0,
IF(A1:A5>A7:A11,A1:A5,A7:A11)-A13:A17,0))}
As you can see, the first half of the formula checks where the array is greater than zero, and if they are, it sums those in the second part of the formula.
You will notice that the same IF statement is repeated in there twice, which to me is inefficient, but is the only way I could get the correct answer.
The example data I have is as follows:
Sample Data in spreadsheet http://clients.estatemaster.net/SecureClientSite/Download/TempFiles/example.jpg
The answer should be 350 in this instance using the formula I mentioned above.
If I tried to put in a MAX statement within the array, therefore removing the test to find where it was greater than zero, so it was like this:
{=SUM(MAX(IF(B2:B6>B8:B12,B2:B6,B8:B12)-B14:B18,0))}
However, it seems like it only calculates the first row of data in each range, and it gave me the wrong answer of 70.
Does anyone know a away that I can reduce the size of the formula or make it more efficient by not needing to repeat an IF statement in there?
UPDATE
Jimmy
The MAX formula you suggested didnt actually work for all scenarios.
If i changed my sample data in rows 1 to 5 as below (showing that some of the numbers are greater than their respective cells in rows 7 to 11, while some of the numbers are lower)
Sample Data in spreadsheet http://clients.estatemaster.net/SecureClientSite/Download/TempFiles/example2.jpg
The correct answer im trying to achive is 310, however you suggested MAX formula gives an incorrect answer of 275.
Im guessing the formula needs to be an array function to give the correct answer.
Any other suggestions?
=MAX( MAX( sum(A1:A5), sum(A7:A11) ) - sum(A13:A17), 0)
A more calculation-efficient (and especially re-calculation efficient) way is to use helper columns instead of array formulae:
C1: =MAX(A1,A7)-A13
D1: =IF(C1>0,C1,0)
copy both these down 5 rows
E1: =SUM(D1:D5)
Excel will then only recalculate the formulae dependent on any changed value, rather than having to calculate all the virtual columns implied by the array formula every time any single number changes. And its doing less calculations even if you change all the numbers.
You may want to look into the VB Macro editor. In the Tools Menu, go to Macros and select Visual basic Editor. This gives a whole programming environment where you can write your own function.
VB is a simple programming language and google has all the guidebooks you need.
There, you can write a function like MySum() and have it do whatever math you really need it to, in a clear way written by yourself.
I pulled this off google, and it looks like a good guide to setting this all up.
http://office.microsoft.com/en-us/excel/HA011117011033.aspx
This seems to work:
{=SUM(IF(A1:A5>A7:A11,A1:A5-A13:A17,A7:A11-A13:A17))}
EDIT
- doesn't handle cases where subtraction ends up negative
This works - but is it more efficient???
{=SUM(IF(IF(A1:A5>A7:A11,A1:A5,A7:A11)>A13:A17,IF(A1:A5>A7:A11,A1:A5,A7:A11)-A13:A17,0))}
What about this?
=MAX(SUM(IF(A1:A5>A7:A11, A1:A5, A7:A11))-SUM(A13:A17), 0)
Edit:
Woops - Missed the throwing out negatives part. What about this? Not sure it it's faster...
=SUM((IF(A1:A5>A7:A11,IF(A1:A5>A13:A17,A1:A5,A13:A17),IF(A7:A11>A13:A17,A7:A11,A13:A17))-A13:A17))
Edit 2:
How does this perform for you?
=SUM((((A1:A5>A13:A17)+(A7:A11>A13:A17))>0)*(IF(A1:A5>A7:A11,A1:A5,A7:A11)-A13:A17))

Resources