Forum Discussion

ctone00's avatar
ctone00
Copper Contributor
Feb 20, 2024

sumif horiztonally and vertically

how can i update my formula in the summary tab such that when i drag my formula down it finds the month and pulls in the correct column from the raw data tab.

7 Replies

  • djclements's avatar
    djclements
    Bronze Contributor

    ctone00 One option with SUMIF is to use XLOOKUP (or INDEX/MATCH) in the [sum_range] argument:

     

    =SUMIF(Raw!$A:$A,B$1,XLOOKUP($A2,Raw!$B$1:$M$1,Raw!$B:$M))

     

    Then drag down and across as needed. Personally, I would not use entire column references here, but such is your example...

  • Patrick2788's avatar
    Patrick2788
    Silver Contributor

    ctone00 

    A 365 solution for the future with an insider function (hopefully, widely available very soon!):

    =LET(
        aggregated, GROUPBY(Division, Data, SUM, , 0),
        TRANSPOSE(aggregated)
    )

     

    • PeterBartholomew1's avatar
      PeterBartholomew1
      Silver Contributor

      Patrick2788 

      That takes the prize for elegance!

      I had wondered about normalising the data and then using PIVOTBY but this is far better.

  • ctone00 

    I have added a couple more solutions to Maciej_Kopczynski 's workbook.  I program solely for 365 dynamic arrays so they look somewhat different.  The main challenge is that your required layout is an array of arrays (month x division).  That is a characteristic of most spreadsheet solutions but, regrettably, Microsoft decided that nested arrays should be treated as an error rather than the desired result.

     

    My first solution uses array reshaping to lay the raw data out in a form that the basic summation by columns is a straightforward SUM rather than SUMIFS.

    = LET(
        reshaped,     WRAPROWS(TOCOL(data), 24),
        monthlyTotal, BYCOL(reshaped, LAMBDA(x, SUM(x))),
        WRAPCOLS(monthlyTotal, 12)
      )

    The second approach uses SUMIFS, as suggested by the OP,  but treats the divisions as two separate solutions which are then stacked to give the result.  This would be very tedious if there were 100s of divisions or if the count of divisions needed to be dynamic.

    = LET(
        div1Totals, TOCOL(BYCOL(data, LAMBDA(d, SUMIFS(d, Division, "Division1")))),
        div2Totals, TOCOL(BYCOL(data, LAMBDA(d, SUMIFS(d, Division, "Division2")))),
        HSTACK(div1Totals, div2Totals)
      )

    Something that does work well is that the BYCOL helper function returns each column as a range reference rather than an array, so permitting the use of SUMIFS etc.

     

    p.s. I appreciate that the OP ctone00  wanted to get results by 'dragging a formula down' but, since I haven't used that as a technique since 2015, I am definitely not the best person to address that requirement.

  • Patrick2788's avatar
    Patrick2788
    Silver Contributor

    ctone00 

    I offer a 365 solution with a dynamic array:

    =LET(
        GetTotals, LAMBDA(r, c,
            LET(
                month, INDEX(totals, , r),
                div, RIGHT(division) * 1,
                filtered, FILTER(month, div = c, 0),
                SUM(filtered)
            )
        ),
        MAKEARRAY(12, 2, GetTotals)
    )

Resources