简体   繁体   中英

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. 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.

在此处输入图片说明

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.

=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

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

在此处输入图片说明

You can also do this using 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
  • 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.

M-Code

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

Edit:

By the way, you can also do that with a pivot table, and filter the top 1 value of the sum

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM