简体   繁体   English

Excel-有没有一种方法可以基于2个相关标准求和,然后找到最大和值?

[英]Excel - Is there a way to SUM values based on 2 associated criteria and then find the MAX summed value?

I have a list of buildings by city and building name which all have 5 items associated to them and each have a monetary value. 我有一个按城市和建筑物名称列出的建筑物列表,所有建筑物都有5个与之相关的项目,每个项目都有货币价值。 I am trying the sum these 5 values based on the city and building name and then of those sums I want to output the maximum value. 我正在尝试根据城市和建筑物名称将这5个值相加,然后在这些和中我想输出最大值。

在此处输入图片说明

I have tried using this code in the Value Cell F4 from the image but it only returns the sum of the first 5 items from the table. 我尝试在图像的值单元格F4中使用此代码,但它仅返回表中前5个项目的总和。

=MAX(SUMIFS(Table1[Value],Table1[Location],INDEX(Table1[Location],,1)))

I am not sure how to store a list of all the summed values of the different buildings and then output the max value. 我不确定如何存储不同建筑物的所有求和值的列表,然后输出最大值。

I would also like the City and Building name associated to the maximum summed value be output in the other 2 cells G4 and H4 我还希望与最大和值关联的城市和建筑物名称在其他2个单元格G4和H4中输出

For the max sum 对于最大和

=MAX(SUMIFS(Table1[Value],Table1[Location],Table1[Location],Table1[Building],Table1[Building]))

For the City 为城市

=INDEX(Table1[Location],MATCH(F4,SUMIFS(Table1[Value],Table1[Location],Table1[Location],Table1[Building],Table1[Building]),0))

and for the Building 对于建筑

=INDEX(Table1[Building],MATCH(F4,SUMIFS(Table1[Value],Table1[Location],Table1[Location],Table1[Building],Table1[Building]),0))

all entered as array formulas using Ctrl Shift Enter 全部使用Ctrl Shift 输入为数组公式

在此处输入图片说明

You can also do this using Power Query . 您也可以使用Power Query进行此操作。

This would be particularly useful in the event two buildings have the same value , as I have adjusted the data below to show. 如果两座建筑物的值相同 ,这将特别有用,因为我已经调整了以下数据以显示。

  • Group the rows by Location and Building 按位置和建筑物将行分组
    • Aggregating by Sum Sum汇总
  • Filter the rows by the Maximum of the Values column 按“最大值”列过滤行
  • Do some rearranging and renaming 做一些重新排列和重命名

Except for the Filter operation, which needs to be entered manually in the Advanced Editor , all of the operations can be done using the Power Query GUI. 除了需要在“ Advanced Editor手动输入的“ Filter操作外,所有操作都可以使用Power Query GUI进行。

M-Code M代码

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Location", type text}, {"Building", type text}, {"Item", type text}, {"Value", Currency.Type}}),
    #"Grouped Rows" = Table.Group(#"Changed Type", {"Location", "Building"}, {{"Total Values", each List.Sum([Value]), type number}}),
    #"Filtered Rows" = Table.SelectRows(#"Grouped Rows", each ([Total Values] = List.Max(#"Grouped Rows"[Total Values]))),
    #"Reordered Columns" = Table.ReorderColumns(#"Filtered Rows",{"Total Values", "Location", "Building"}),
    #"Renamed Columns" = Table.RenameColumns(#"Reordered Columns",{{"Total Values", "Value"}})
in
    #"Renamed Columns"

Source Data 源数据

在此处输入图片说明

Max Values 最大值

在此处输入图片说明

There's an easy workaround to that. 有一个简单的解决方法。 If it's not a problem, you can add a column to your table with the sum of that building/location 如果没问题,您可以在表格中添加一列,其中包含该建筑物/位置的总和

=SUMIFS(Table1[Value],Table1[Location],[@[Location]],Table1[Building],[@[Building]]) 

(Something like this in your table) (表格中的内容)

例

And then do the MAX of that new column 然后执行该新列的MAX

Edit: 编辑:

By the way, you can also do that with a pivot table, and filter the top 1 value of the sum 顺便说一句,您也可以使用数据透视表进行此操作,并过滤总和的前1个

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Excel - UDF Function 根据条件从多个工作表中获取 SUM 值 - Excel - UDF Function to get the SUM value from multiple sheets based on criteria 从所有子数组中找到最大值的总和 - Find the sum of the max values from all subarrays 基于两个条件的javascript对象数组总和值 - javascript object array sum values based on two criteria Python:如何根据某些条件合并和求和列表值 - Python: How to merge AND sum list values based on some criteria 在没有宏的Excel中查找MAX和SUM可变数量的列 - Find a MAX and SUM a variable number of columns in Excel without Macros 更快地找到数组中的最小值和最大值 - Faster way to find min and max values in array Excel:根据多个条件减去2个值以获得消息响应率 - Excel: Subtract 2 values based on multiple criteria to get message response rate 具有多个数组条件的Excel SUM IF - Excel SUM IF with multiple array criteria 基于多维数组制作关联值数组的最佳方法 - Best way to make an array of associated values, based on multidimensional array 查找数组列的最大值并在数据框中查找另一个数组中的关联值 - Find the Max value of an Array column and find associated value in another Array with in the dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM