我正在尝试汇总一个公式,如果订单中包含特定项目,该公式将为我提供该订单的平均总数,我该如何计算呢?
例如,表格看起来像
Order# | Item | Total for Entire order
a Apple 50
a Juice 50
a Chicken 50
a Bread 50
b Bread 23
b fish 23
c Chicken 43
c Wine 43
c rice 43
我想至少一次获取所有包含Chicken的订单的平均合计?但不想在我的平均计算中计算两次一次订单的总数-感谢您查看
答案 0 :(得分:1)
如果有新的动态数组公式:
=AVERAGE(INDEX(UNIQUE(FILTER(A2:C10,B2:B10="Chicken")),,3))
如果不是:
=SUMPRODUCT(((B2:B10="Chicken")*(C2:C10))/(COUNTIFS(A2:A10,A2:A10,B2:B10,"Chicken")+(B2:B10<>"Chicken")))/SUMPRODUCT((B2:B10="Chicken")/(COUNTIFS(A2:A10,A2:A10,B2:B10,"Chicken")+(B2:B10<>"Chicken")))
答案 1 :(得分:0)
如果您不反对使用VBA,则此功能将满足您的要求。
=Average_Orders_Subset(range, criteria)
我从最初的答案中对此进行了更改,以改进功能并添加了注释,以便您可以看到它的作用。使用此功能,您可以添加任意数量的订单。我也对其进行了测试,如果将订购字母更改为数字,即a = 1,b = 2等,它将可以正常工作。希望这会有所帮助。随时询问您是否有任何疑问。
如果您的数据从单元格A1开始,则其工作方式如下:
示例:=Average_Orders_Subset(A1:C13,"Chicken")
使用此版本,您可以平均选择鸡肉,米饭,面包以及任何您想要的东西。 如果您不知道here's如何将代码添加到工作簿中。 如果您愿意,我可以向您发送已经内置的工作簿副本。
Option Explicit
' User Defined Function to Average totals for only orders that contain specific string
' Pass the range and string you are looking for to the function
' Note the criteria string is case senstive so "chicken" is not the same as "Chicken"
'
' Example: =Average_Orders_Subset(A1:C13,"Chicken")
'
Public Function Average_Orders_Subset(rng As Range, criteria As String) As Double
Dim ws As Worksheet
Dim arrData() As Variant
Dim arrOrder() As Variant
Dim i As Long
Dim j As Long
Dim cnt As Long
Dim sum As Double
Dim avg As Double
' The worksheet with data
Set ws = ThisWorkbook.ActiveSheet
' Counter and array to keep track of order letters
cnt = 0
ReDim arrOrder(cnt)
With ws
' Create an array with all the values
arrData = rng.Value2
' Iterate through the array looking for orders with the criteria e.g., "Chicken"
For i = 2 To UBound(arrData)
' If criteria is found
If arrData(i, 2) = criteria Then
If cnt > 0 Then
' If the array of order letters is less than 0
If arrData(i, 1) <> arrOrder(cnt - 1) Then
' Checking if the order letter is already in the array so orders with two Chicken
' or multipe of any criteria don't get double counted
' Add them to the order letter array
arrOrder(cnt) = arrData(i, 1)
cnt = cnt + 1
ReDim Preserve arrOrder(cnt)
End If
ElseIf cnt = 0 Then
' This is to add the first occurence of the critera to the order array
arrOrder(cnt) = arrData(i, 1)
cnt = cnt + 1
ReDim Preserve arrOrder(cnt)
End If
End If
Next i
' Remove the last empty value in the order array
' this is a result of the count expanding the array after the value is added
ReDim Preserve arrOrder(UBound(arrOrder) - 1)
' Reset counter
cnt = 0
sum = 0
' For all the values in the order array
For i = LBound(arrOrder) To UBound(arrOrder)
' For all the values in the range
For j = 2 To UBound(arrData)
' If a row in the range matches the order letter add that value to the sum
If arrData(j, 1) = arrOrder(i) Then
sum = sum + arrData(j, 3)
' Keep track of the number of summed values for an average
cnt = cnt + 1
End If
Next j
Next i
' Calculte the average
avg = (sum / cnt)
' Pass the average back to the formula
Average_Orders_Subset = avg
End With
Set rng = Nothing
Set ws = Nothing
End Function