Excel中的数组公式计算非结构化数据的加权平均值
Calculating weighted-average for unstructured data by array formula in Excel
我正在尝试计算 Excel 中非结构化数据的加权平均值并保持紧凑(不添加要使用的查找 table)。数据结构如下,我不能更改它。
我确信数据集中的成交量(加权因子)总是早于价格(平均价值)。我也确定数据已按图片所示排序
您认为有一种方法可以计算一行中一种产品的加权平均值吗?我已经尝试过数组公式,但无法匹配应在数组内相乘的值:带价格的数量。
文字版资料如下:
product;customer;parameter;Jan;Feb;Mar
product 1;customer 1;volume;69;75;98
product 1;customer 1;price;97;32;96
product 1;customer 1;other parameter;71;31;50
product 1;customer 1;other parameter;2;1;79
product 1;customer 1;other parameter;74;93;67
product 1;customer 2;volume;59;74;94
product 1;customer 2;other parameter;0;73;67
product 1;customer 2;price;40;38;89
product 1;customer 2;other parameter;29;44;86
product 1;customer 3;other parameter;85;30;7
product 1;customer 3;volume;34;72;7
product 1;customer 3;price;35;46;33
product 2;customer 4;volume;87;81;64
product 2;customer 4;price;21;65;94
product 2;customer 4;other parameter;78;68;36
product 2;customer 4;other parameter;33;27;47
product 2;customer 4;other parameter;66;57;53
product 2;customer 1;volume;42;70;22
product 2;customer 1;other parameter;7;88;21
product 2;customer 1;price;43;58;80
product 2;customer 1;other parameter;27;51;48
product 1;;weighted-average price;63.2283950617284;38.5701357466063;90.4773869346734
product 2;;weighted-average price;28.1627906976744;61.7549668874172;90.4186046511628
您可以使用 Power Query(获取和转换数据):
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed type" = Table.TransformColumnTypes(Source,{{"product", type text}, {"customer", type text}, {"parameter", type text}, {"Jan", type number}, {"Feb", type number}, {"Mar", type number}}),
#"Filtered volume price" = Table.SelectRows(#"Changed type", each ([parameter] = "volume" or [parameter] = "price")),
#"Unpivoted months" = Table.UnpivotOtherColumns(#"Filtered volume price", {"product", "customer", "parameter"}, "month", "value"),
#"Pivoted parameter" = Table.Pivot(#"Unpivoted months", List.Distinct(#"Unpivoted months"[parameter]), "parameter", "value", List.Sum),
#"Added revenue" = Table.AddColumn(#"Pivoted parameter", "revenue", each [volume] * [price]),
#"Grouped products months" = Table.Group(#"Added revenue", {"product", "month"}, {{"total revenue", each List.Sum([revenue]), type number}, {"total volume", each List.Sum([volume]), type number}}),
#"Added average price" = Table.AddColumn(#"Grouped products months", "average price", each [total revenue] / [total volume]),
#"Removed totals" = Table.RemoveColumns(#"Added average price",{"total revenue", "total volume"}),
#"Pivoted months" = Table.Pivot(#"Removed totals", List.Distinct(#"Removed totals"[month]), "month", "average price", List.Sum),
#"Reordered months" = Table.ReorderColumns(#"Pivoted months",{"product", "Jan", "Feb", "Mar"})
in
#"Reordered months"
更精确的方法是省略最后两行,并将查询加载到数据模型 - 然后您可以使用 CUBEVALUE
公式来 return 您的平均价格:
=CUBEVALUE("ThisWorkbookDataModel",
CUBEMEMBER("ThisWorkbookDataModel","[Measures].[Sum of average price]"),
CUBEMEMBER("ThisWorkbookDataModel","[Table1].[product].&["&$A24&"]"),
CUBEMEMBER("ThisWorkbookDataModel","[Table1].[month].&["&D&"]"))
在D24
中作为数组公式输入并填写over/down:
=SUM(SUMIFS(D:D,$A:$A,$A24,$B:$B,IF(($A:$A=$A24)+($C:$C="price")=2,$B:$B),$C:$C,"volume")*IF(($A:$A=$A24)+($C:$C="price")=2,D:D))/SUMIFS(D:D,$A:$A,$A24,$C:$C,"volume")
这假设数据每个月每个客户只有一个 volume/price 条目。
我正在尝试计算 Excel 中非结构化数据的加权平均值并保持紧凑(不添加要使用的查找 table)。数据结构如下,我不能更改它。
我确信数据集中的成交量(加权因子)总是早于价格(平均价值)。我也确定数据已按图片所示排序
您认为有一种方法可以计算一行中一种产品的加权平均值吗?我已经尝试过数组公式,但无法匹配应在数组内相乘的值:带价格的数量。
文字版资料如下:
product;customer;parameter;Jan;Feb;Mar
product 1;customer 1;volume;69;75;98
product 1;customer 1;price;97;32;96
product 1;customer 1;other parameter;71;31;50
product 1;customer 1;other parameter;2;1;79
product 1;customer 1;other parameter;74;93;67
product 1;customer 2;volume;59;74;94
product 1;customer 2;other parameter;0;73;67
product 1;customer 2;price;40;38;89
product 1;customer 2;other parameter;29;44;86
product 1;customer 3;other parameter;85;30;7
product 1;customer 3;volume;34;72;7
product 1;customer 3;price;35;46;33
product 2;customer 4;volume;87;81;64
product 2;customer 4;price;21;65;94
product 2;customer 4;other parameter;78;68;36
product 2;customer 4;other parameter;33;27;47
product 2;customer 4;other parameter;66;57;53
product 2;customer 1;volume;42;70;22
product 2;customer 1;other parameter;7;88;21
product 2;customer 1;price;43;58;80
product 2;customer 1;other parameter;27;51;48
product 1;;weighted-average price;63.2283950617284;38.5701357466063;90.4773869346734
product 2;;weighted-average price;28.1627906976744;61.7549668874172;90.4186046511628
您可以使用 Power Query(获取和转换数据):
let
Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
#"Changed type" = Table.TransformColumnTypes(Source,{{"product", type text}, {"customer", type text}, {"parameter", type text}, {"Jan", type number}, {"Feb", type number}, {"Mar", type number}}),
#"Filtered volume price" = Table.SelectRows(#"Changed type", each ([parameter] = "volume" or [parameter] = "price")),
#"Unpivoted months" = Table.UnpivotOtherColumns(#"Filtered volume price", {"product", "customer", "parameter"}, "month", "value"),
#"Pivoted parameter" = Table.Pivot(#"Unpivoted months", List.Distinct(#"Unpivoted months"[parameter]), "parameter", "value", List.Sum),
#"Added revenue" = Table.AddColumn(#"Pivoted parameter", "revenue", each [volume] * [price]),
#"Grouped products months" = Table.Group(#"Added revenue", {"product", "month"}, {{"total revenue", each List.Sum([revenue]), type number}, {"total volume", each List.Sum([volume]), type number}}),
#"Added average price" = Table.AddColumn(#"Grouped products months", "average price", each [total revenue] / [total volume]),
#"Removed totals" = Table.RemoveColumns(#"Added average price",{"total revenue", "total volume"}),
#"Pivoted months" = Table.Pivot(#"Removed totals", List.Distinct(#"Removed totals"[month]), "month", "average price", List.Sum),
#"Reordered months" = Table.ReorderColumns(#"Pivoted months",{"product", "Jan", "Feb", "Mar"})
in
#"Reordered months"
更精确的方法是省略最后两行,并将查询加载到数据模型 - 然后您可以使用 CUBEVALUE
公式来 return 您的平均价格:
=CUBEVALUE("ThisWorkbookDataModel",
CUBEMEMBER("ThisWorkbookDataModel","[Measures].[Sum of average price]"),
CUBEMEMBER("ThisWorkbookDataModel","[Table1].[product].&["&$A24&"]"),
CUBEMEMBER("ThisWorkbookDataModel","[Table1].[month].&["&D&"]"))
在D24
中作为数组公式输入并填写over/down:
=SUM(SUMIFS(D:D,$A:$A,$A24,$B:$B,IF(($A:$A=$A24)+($C:$C="price")=2,$B:$B),$C:$C,"volume")*IF(($A:$A=$A24)+($C:$C="price")=2,D:D))/SUMIFS(D:D,$A:$A,$A24,$C:$C,"volume")
这假设数据每个月每个客户只有一个 volume/price 条目。