[英]Calculate Average based on multiple condition in Excel
I am back with my new excel question. 我回来了我的新的Excel问题。 Lets say I have table like this.
可以说我有这样的桌子。
| A | B
------------------------------------------
1 | ENV | Value
------------------------------------------
2 | ABC - 10/1/2014 1:38:32 PM | 4
3 | XYZ - 10/1/2014 1:38:32 PM | 6
4 | ABC - 9/1/2014 1:38:32 PM | 1
5 | XYZ - 10/1/2014 1:38:32 PM | 10
6 | ABC - 10/1/2014 1:38:32 PM | 7
7 | XYZ - 9/1/2014 1:38:32 PM | 1
8 | ABC - 9/1/2014 1:38:32 PM | 10
9 | ABC - 10/1/2014 1:38:32 PM | 7
10 | XYZ - 10/1/2014 1:38:32 PM | 7
Now, in Cell C2, I've selected ABC. 现在,在单元格C2中,我选择了ABC。
So in cell D2, I want the average ( from col B ) of all the "ABC" ( col A ) where Month = 10 ( col A ) and in cell E2, Max ( from col B ) of all the "ABC" where Month = 10 ( col A ). 因此,在细胞D2,我希望所有的“ABC”(COL A)其中所有的“ABC”的月= 10(COL A)和在细胞E2,MAX( 从列B),其中的平均( 从列B)月= 10( col A )。
So, my result in cells D2 and E2 would be 6 and 7 respectively. 因此,我在单元格D2和E2中的结果分别为6和7。
I hope my question and example make sense. 我希望我的问题和榜样有意义。
UPDATE: 更新:
Thank you all for all your help. 谢谢大家的帮助。
Now let's say I am not sure how many rows I'll have on this spreadsheet, so I came up with this formula, but its not working, giving me #DIV/0! 现在让我们说我不确定该电子表格上将有多少行,所以我想出了这个公式,但是它不起作用,给了我#DIV / 0! error.
错误。
*Note: I am using formula to get "ABC" and "10" from cell C2. *注意:我正在使用公式从单元格C2中获取“ ABC”和“ 10”。
=AVERAGEIFS(
(OFFSET($A$1,1,1,COUNTA($B:$B)-1,1)),
OFFSET($A$1,1,0,COUNTA($A:$A)-1,1), (MID(C2,1,(FIND("-",C2))-2)),
OFFSET($A$1,1,0,COUNTA($A:$A)-1,1), (MID(C2,(FIND("-",C2)+1),(FIND("/",C2))-(FIND("-",C2)+1))))
Even tried this, but same error: 甚至尝试过此方法,但存在相同错误:
=SUMPRODUCT(((MID(A2:A10,1,(FIND("-",A2:A10))-1))=(MID(C2,(FIND("-",C2)+1),(FIND("/",C2))-(FIND("-",C2)+1))))*
(MONTH(DATEVALUE(MID(A2:A10,7,99)))=(MID(C2,(FIND("-",C2)+1),(FIND("/",C2))-(FIND("-",C2)+1))))*
(B2:B10))/SUMPRODUCT(((MID(A2:A10,1,(FIND("-",A2:A10))-1))=(MID(C2,(FIND("-",C2)+1),(FIND("/",C2))-(FIND("-",C2)+1))))*
(MONTH(DATEVALUE(MID(A2:A10,7,99)))=(MID(C2,(FIND("-",C2)+1),(FIND("/",C2))-(FIND("-",C2)+1)))))
Can you help me with this...? 你能帮我这个吗?
To solve the issue (I tested the average only) I first used 2 intermediary values: this solution is not optimal and there will be many smarter ways to address the issue (eg pivot tables). 为了解决该问题(我仅测试了平均值 ),我首先使用了2个中间值:此解决方案不是最佳解决方案,将有许多更智能的方法来解决该问题(例如数据透视表)。
ENV Value Intermediary 1 Intermediary 2
ABC - 10/1/2014 1:38:32 PM 4 ABC 10
XYZ - 10/1/2014 1:38:32 PM 6 XYZ 10
ABC - 9/1/2014 1:38:32 PM 1 ABC 9
XYZ - 10/1/2014 1:38:32 PM 10 XYZ 10
The first intermediary column contains the first 3 chars of ENV column ( =LEFT(A9,3)
), while the second intermediary column contains the month ( =MID(A9,7,2)
). 第一中间列包含ENV列的前三个字符(
=LEFT(A9,3)
),而第二中间列包含月份( =MID(A9,7,2)
)。 This works only if your ENV
records are fixed size and homogeneous (eg your env name has exactly 3 chars). 仅当您的
ENV
记录是固定大小且同质的(例如,您的环境名称恰好有3个字符)时,此方法才有效。
With this layout, you can compute the average putting in any cell the following formula: 使用此布局,您可以计算以下公式在任何单元格中的平均放置:
=AVERAGEIFS(D9:D12, F9:F12,"=ABC", G9:G12, "=10")
Where D9:D12
is the values interval, F9:F12
is the 1st intermediary column and G9:G12
the second intermediary column. 其中
D9:D12
是值间隔, F9:F12
是第一中间列, G9:G12
是第二中间列。
An optimized solution can be found relying on arrays
. 可以找到依赖于
arrays
的优化解决方案。 For instance, to calculate the average and the max of an interval based on 2 "vectorial" conditions you can write this one liners: 例如,要基于2个“矢量”条件计算间隔的平均值和最大值,可以编写此衬纸:
= MAX(IF((LEFT(A9:A12,3)="ABC")*(MID(A9:A12,7,2)="10"),D9:D12))
= AVERAGE(IF((LEFT(A9:A12,3)="ABC")*(MID(A9:A12,7,2)="10"),D9:D12))
With A9:A12
your original records, and D9:D12
is the values interval. 使用
A9:A12
您的原始记录,而D9:D12
是值间隔。
The advantages of this solution are that you don't need any intermediary column and that you can extend this approach to all the other formulas that don't have ' xxxxxIFS
' (it's the case for MAX
). 该解决方案的优点是您不需要任何中间列,并且可以将此方法扩展到所有其他没有'
xxxxxIFS
'的公式(对于MAX
就是这种情况)。
NOTE : you have to confirm this formula with CTRL + SHIFT + RETURN
or your formula will fail with #VALUE
error. 注意 :您必须使用
CTRL + SHIFT + RETURN
确认此公式,否则您的公式将因#VALUE
错误而失败。
Live demo available here . 现场演示可在此处获得 。
You can start by spiting column A into a date and letters using - Data > Text to Columns with the delimiter " - ". 您可以使用-数据>带有分隔符“-的列到文本”,将A列插入日期和字母开始。 after you have the new two columns (let say F and G) you can use the function "AVERAGEIF" with a condition that check is the value of the cell in "F" is ABC and the Moth(cell in "G") = 10. as for the max, you can do the same with MAX(IF....) for column E.
在有了新的两列(假设F和G)之后,可以使用函数“ AVERAGEIF”,条件是检查“ F”中单元格的值是ABC且Moth(“ G”中单元格)= 10.至于最大值,您可以对列E的MAX(IF ....)进行相同操作。
SUMPRODUCT
will allow you to parse the left-most and date characters from your combined string. SUMPRODUCT
将允许您分析组合字符串中最左边和日期的字符。 A pseudo-MAXIF() can be similarly constructed using MAX()
and INDEX()
. 可以使用
MAX()
和INDEX()
类似地构造伪MAXIF INDEX()
。
In D2 use =SUMPRODUCT((LEFT(A2:A10,3)="ABC")*(MONTH(DATEVALUE(MID(A2:A10,7,99)))=10)*(B2:B10))/SUMPRODUCT((LEFT(A2:A10,3)="ABC")*(MONTH(DATEVALUE(MID(A2:A10,7,99)))=10))
在D2中使用
=SUMPRODUCT((LEFT(A2:A10,3)="ABC")*(MONTH(DATEVALUE(MID(A2:A10,7,99)))=10)*(B2:B10))/SUMPRODUCT((LEFT(A2:A10,3)="ABC")*(MONTH(DATEVALUE(MID(A2:A10,7,99)))=10))
In E2 use =MAX(INDEX((LEFT(A2:A10,3)="ABC")*(MONTH(DATEVALUE(MID(A2:A10,7,99)))=10)*(B2:B10),,))
在E2中使用
=MAX(INDEX((LEFT(A2:A10,3)="ABC")*(MONTH(DATEVALUE(MID(A2:A10,7,99)))=10)*(B2:B10),,))
Both SUMPRODUCT
and INDEX
like to choke on anything remotely resembling an error when parsing text so keep the cell range references to what your actual data is and avoid blanks. SUMPRODUCT
和INDEX
喜欢在解析文本时对任何类似于错误的东西进行阻塞,因此使单元格范围引用始终与实际数据相同,并避免出现空白。
Your results should look like the following. 您的结果应如下所示。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.