Forum Discussion

LilyB's avatar
LilyB
Tin Contributor
Sep 24, 2026
Solved

Return the Cell(s) with the Highest SUM() between groups of Cells

So, long story short, I've been banging my head against a wall with this one. I'm teaching myself Excel and I'm struggling to wrap my mind around this one. After trying combo, after combo, after combo, for two days now and every forum failing me. I would like to ask for help from the kind and intelligent internet! I feel like I must be missing something basic...

Goals: 

  • Return the name/cell of the "Branch"/"Region" with the Highest SUM() between groups of Cells
  • Return the SUM() of the "Branch"/"Region" with the Highest SUM() between groups of Cells
  • Expected Output of Example: 

    • Top Region: "Name 1" 
    • Top Region Total: 15
    • Top Branch: "City 4"
    • Top Branch Total: 9

 

Current Formulas: 

  • =IFERROR(INDEX(Table25[Branch], MODE(MATCH(Table25[Branch], Table25[Branch], 0))), "N/A")
    • This successfully grabs the branch with the highest number of occurrences, but I need total points related to that branch, not just the most occurring one. My mind says I need to essentially have a running list of each Branches points, SUM() the points, and return the highest SUM() for Total and the Cell linked to the highest total. I'm assuming I'll have to use MAKEARRAY() and BYCOL(), but I'm not sure how to incorporate them.
  • =SUMIF(Table25[Branch], "Name 1", Table25[Points])
    • This can manually grab the total of a specific name... But, I need something that can discover the highest name on its own and adapt if names change instead of static specifications.  

 

  • I think you're on the right track with SUMIF and MAX. You just need to incorporate UNIQUE and FILTER to return the item(s) with the maximum total. In the event of ties, you may also want to consider using SORT and ARRAYTOTEXT to consolidate the list of items.

    The following is just one example of a possible solution using a custom LAMBDA function:

    = TopInλ(Table25[[#All],[Region]], Table25[[#All],[Points]])

    Where TopInλ is defined in Name Manager as:

    = LAMBDA(field,values,[_0123],
        LET(
            k, UNIQUE(SORT(DROP(field,1))),
            i, SUMIF(field,k,values),
            j, MAX(i),
            x, CHOOSE(1+_0123,{1;3;4},{1,3,4},{1,3;2,4},{1,2;3,4}),
            CHOOSE(x,"Top "&TAKE(field,1),"Total",ARRAYTOTEXT(FILTER(k,i=j)),j)
        )
    )

    The optional [_0123] argument can be set to 1, 2 or 3 (default is 0 if omitted) to change the orientation of the final output. For example:

    = TopInλ(Table25[[#All],[Region]], Table25[[#All],[Points]], 1)

    The same formula can also be applied to the Branch field (or any other applicable field):

    = TopInλ(Table25[[#All],[Branch]], Table25[[#All],[Points]], 1)

    Please note, the function has been written to read the column header/label from the field reference, so you must include the header row when selecting the table column.

    If you don't like any of the output options demonstrated here, or would prefer not to use a function defined in Name Manager, you could also just write the entire thing with your desired output using a single LET statement. For example:

    = LET(
        TopIn, LAMBDA(field, LET(
            k, UNIQUE(SORT(DROP(field,1))),
            i, SUMIF(field,k,Table25[[#All],[Points]]),
            j, MAX(i),
            HSTACK("Top "&TAKE(field,1)&{":";" Total:"}, VSTACK(ARRAYTOTEXT(FILTER(k,i=j)),j))
        )),
        VSTACK(TopIn(Table25[[#All],[Region]]), TopIn(Table25[[#All],[Branch]]))
    )

    The same idea could also be achieved using GROUPBY instead of SUMIF:

    = LET(
        TopIn, LAMBDA(field, LET(
            k, GROUPBY(field,Table25[[#All],[Points]],SUM,1,0),
            i, DROP(k,,1),
            j, MAX(i),
            HSTACK("Top "&TAKE(field,1)&{":";" Total:"}, VSTACK(ARRAYTOTEXT(FILTER(TAKE(k,,1),i=j)),j))
        )),
        VSTACK(TopIn(Table25[[#All],[Region]]), TopIn(Table25[[#All],[Branch]]))
    )

    Please follow-up with additionl questions, if needed. Cheers!

13 Replies

  • LilyB's avatar
    LilyB
    Tin Contributor

    I finally was able to take a look at all these! Thank you all again!! I've got a lot of functions to learn and understand now 😆Plus, the variety of answers gives me ideas how to make functions in the future.

  • An alternative could be Power Query. In the attached file you can add data to the blue dynamic table. Then you can click in any cell of the green table and right-click with the mouse and select refresh to update the green result table.

    Below is the Power Query M code that produces the green result table.

    let
    Source = Excel.CurrentWorkbook(){[Name="Tabelle1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Branch", type text}, {"Region", type text}, {"Points", Int64.Type}}),
    
    // determine top-branch
    BranchTotals = Table.Group(#"Changed Type", {"Branch"}, {{"TotalPoints", each List.Sum([Points]), type number}}),
    BranchSorted = Table.Sort(BranchTotals, {{"TotalPoints", Order.Descending}}),
    BranchTopName = BranchSorted{0}[Branch],
    BranchTopPoints = BranchSorted{0}[TotalPoints],
    
    // determine top-region 
    RegionTotals = Table.Group(#"Changed Type", {"Region"}, {{"TotalPoints", each List.Sum([Points]), type number}}),
    RegionSorted = Table.Sort(RegionTotals, {{"TotalPoints", Order.Descending}}),
    RegionTopName = RegionSorted{0}[Region],
    RegionTopPoints = RegionSorted{0}[TotalPoints],
    
    // combine result
    Result = Table.FromRecords({
        [Top Branch=BranchTopName, Top Branch Total=BranchTopPoints, Top Region=RegionTopName, Top Region Total=RegionTopPoints]
    }),
        #"Demoted Headers" = Table.DemoteHeaders(Result),
        #"Changed Type1" = Table.TransformColumnTypes(#"Demoted Headers",{{"Column1", type text}, {"Column2", type any}, {"Column3", type text}, {"Column4", type any}}),
        #"Transposed Table" = Table.Transpose(#"Changed Type1")
    in
        #"Transposed Table"

     

  • LilyB's avatar
    LilyB
    Tin Contributor

    These are all beautiful!! Thank you all so much! Especially for explanations on why they work and suggestions. I haven't had the chance to go through all yet, but I'm hoping to tomorrow or Wednesday. I don't have access to my doc right now.

  • My preference would be to aggregate by branch and region as two distinct calculation (unless you require a top branch per region).

    = TAKE(GROUPBY(Table1[branch],Table1[points],SUM,,0,-2),1)
    
    = TAKE(GROUPBY(Table1[region],Table1[points],SUM,,0,-2),1)

    It is possible to obtain both sets of values as  total columns in a PIVOTBY but that requires effort sorting out the top row sum and column sum whilst avoiding the grand total.

    • LilyB's avatar
      LilyB
      Tin Contributor

      This is my favorite way too! It's actually what I intended and didn't even think of having a spillover table when I created the example output. I wish I could mark two solutions. I love how simple your answer is compared to the others 😆 But, even seeing your answer I feel better about being so stumped on it.

      How does the ",,0,-2" work? I can see in the description it says field_header, total_depth, and sort_order. But, leaves off filter_array and field_relationship...

      • PeterBartholomew1's avatar
        PeterBartholomew1
        Silver Contributor

        The key is the sort_order which I set to column 2, largest to smallest (the minus).  Had I left the sort ascending, TAKE would need to be set to pick up the final row rather than the first.

  • Terio's avatar
    Terio
    Brass Contributor

    Other way:

    =LET(g,LAMBDA(x,GROUPBY(x,Table25[Points],SUM,0,0)),city,g(Table25[Branch]),region,g(Table25[Region]),c,CHOOSECOLS,VSTACK(XLOOKUP(MAX(c(city,2)),c(city,2),c(city,{1;2})),XLOOKUP(MAX(c(region,2)),c(region,2),c(region,{1;2}))))

    return:


    Bye

  • IlirU's avatar
    IlirU
    Iron Contributor

    Hi LilyB​,

    Try this formula in cell F5:

    =LET(
         data, Table25[#All],
               TAKE(PIVOTBY(CHOOSECOLS(data, 2), TAKE(data,, 1), TAKE(data,, -1), SUM,,, -2,, -2), 2)
    )

    and this formula in cell F9:

    =LET(
          data, Table25[#All],
         p_tbl, PIVOTBY(CHOOSECOLS(data, 2), TAKE(data,, 1), TAKE(data,, -1), SUM,,, -2,, -2),
          drop, DROP(DROP(p_tbl, 1, 1), -1, -1),
         m_row, MAX(BYROW(drop, SUM)),
         m_col, MAX(BYCOL(drop, SUM)),
                HSTACK({"Top Region:";"Top Region Total:";"Top Branch:";"Top Branch Total:"},
                       VSTACK(XLOOKUP(m_row, BYROW(drop, SUM), TAKE(DROP(DROP(p_tbl, 1), -1),, 1)), m_row,
                              XLOOKUP(m_col, BYCOL(drop, SUM), TAKE(DROP(DROP(p_tbl,, 1),, -1), 1)), m_col))
    )

    HTH

    IlirU

  • djclements's avatar
    djclements
    Silver Contributor

    I think you're on the right track with SUMIF and MAX. You just need to incorporate UNIQUE and FILTER to return the item(s) with the maximum total. In the event of ties, you may also want to consider using SORT and ARRAYTOTEXT to consolidate the list of items.

    The following is just one example of a possible solution using a custom LAMBDA function:

    = TopInλ(Table25[[#All],[Region]], Table25[[#All],[Points]])

    Where TopInλ is defined in Name Manager as:

    = LAMBDA(field,values,[_0123],
        LET(
            k, UNIQUE(SORT(DROP(field,1))),
            i, SUMIF(field,k,values),
            j, MAX(i),
            x, CHOOSE(1+_0123,{1;3;4},{1,3,4},{1,3;2,4},{1,2;3,4}),
            CHOOSE(x,"Top "&TAKE(field,1),"Total",ARRAYTOTEXT(FILTER(k,i=j)),j)
        )
    )

    The optional [_0123] argument can be set to 1, 2 or 3 (default is 0 if omitted) to change the orientation of the final output. For example:

    = TopInλ(Table25[[#All],[Region]], Table25[[#All],[Points]], 1)

    The same formula can also be applied to the Branch field (or any other applicable field):

    = TopInλ(Table25[[#All],[Branch]], Table25[[#All],[Points]], 1)

    Please note, the function has been written to read the column header/label from the field reference, so you must include the header row when selecting the table column.

    If you don't like any of the output options demonstrated here, or would prefer not to use a function defined in Name Manager, you could also just write the entire thing with your desired output using a single LET statement. For example:

    = LET(
        TopIn, LAMBDA(field, LET(
            k, UNIQUE(SORT(DROP(field,1))),
            i, SUMIF(field,k,Table25[[#All],[Points]]),
            j, MAX(i),
            HSTACK("Top "&TAKE(field,1)&{":";" Total:"}, VSTACK(ARRAYTOTEXT(FILTER(k,i=j)),j))
        )),
        VSTACK(TopIn(Table25[[#All],[Region]]), TopIn(Table25[[#All],[Branch]]))
    )

    The same idea could also be achieved using GROUPBY instead of SUMIF:

    = LET(
        TopIn, LAMBDA(field, LET(
            k, GROUPBY(field,Table25[[#All],[Points]],SUM,1,0),
            i, DROP(k,,1),
            j, MAX(i),
            HSTACK("Top "&TAKE(field,1)&{":";" Total:"}, VSTACK(ARRAYTOTEXT(FILTER(TAKE(k,,1),i=j)),j))
        )),
        VSTACK(TopIn(Table25[[#All],[Region]]), TopIn(Table25[[#All],[Branch]]))
    )

    Please follow-up with additionl questions, if needed. Cheers!

    • LilyB's avatar
      LilyB
      Tin Contributor

      I finally got back to my work computer and tested all the solutions. I liked yours the best! Thank you so much for both the solution itself and the explanations. I'm trying to fully wrap my head around it and understand each part of the function. I'll probably follow-up and ask for clarification as I play with it since almost all its parts are new to me.❤️

  • m_tarler's avatar
    m_tarler
    Silver Contributor

    There are lots of answers but here is one:

    you can use PIVOTBY to do the grouping and totalling and then pull off those top values.  In the below I just show the top result from the PIVOTBY which is the Top Region but it does show all the Branches (I could easily trim off the middle non-top branches but the resulting output may be confusing.  But using that ouput I added some simple INDEX to pull of the individual values to output the table you requested:

    here is the first formula (cell F2):

    =TAKE(PIVOTBY(Table1[region],Table1[branch],Table1[points],SUM,,,-2,,-2),2)

    here is the final formula (cel F6) which is also shown in the formula bar in the image:

    =LET(a,TAKE(PIVOTBY(Table1[region],Table1[branch],Table1[points],SUM,,,-2,,-2),2),
             HSTACK({"Top Region:";"Top Region Total:";"Top Branch:";"Top Branch Total"},
                           VSTACK(INDEX(a,2,1),TAKE(a,-1,-1),INDEX(a,1,2),INDEX(a,2,2))))

     

    • LilyB's avatar
      LilyB
      Tin Contributor

      Oh wow! That solution looks much different than expected. I've never used PIVOTBY. I'll have to study it. 👀

      After messing with your solution in my document, I noticed that it doesn't always add the top branch total properly, it'll just leave the cell blank. It can solve it sometimes, but not always, and I haven't fully studied why yet... After messing around with a few more things I noticed I was on the right track with one of my formula's. So, I'm able to get the max sum for Branches or Regions. Is it possible to adapt a similar formula to below for finding the Region name linked to the max sum? Instead of making a table?: 

      • =MAX(BYROW(Table25[Region], LAMBDA(c, SUMIF(Table25[Region], c, Table25[Points]))))

      (Added colored boarders for visibility) 

       

      • m_tarler's avatar
        m_tarler
        Silver Contributor

        LilyB​ , my bad.  The sort of the columns is actually based on the Total but that line is only showing the total for that branch.  Here is the updated formula where I removed the TAKE on the first line and then added a TAKE(a,-1) on the part to get that total.

        Notice the PIVOTBY alone now shows the full pivot table (just for reference) and the update formula in F15 now shows the total for the top branch.

        As for the MAX BYROW LAMBDA ... yes you can do that but I just preferred to use the functionality provided by excel to do all that grouping and summing and sorting.