+ Reply to Thread
Results 1 to 14 of 14

SUMPRODUCT / SUMIF based on both row and column criteria

  1. #1
    Registered User
    Join Date
    03-08-2019
    Location
    Denmark
    MS-Off Ver
    Office 365
    Posts
    57

    Exclamation SUMPRODUCT / SUMIF based on both row and column criteria

    Hi there,

    I have been searching for an answer to this problem for a few hours now, however without success.

    Basically, I am looking for a formula that can sum a range based on two criteria;
    • (1) Columns that contain one of multiple headers (looked up from a separate list)
    • (2) Rows that contain a specific company name

    I have enclosed a detailed description and example of the problem, which should clarify any uncertainties from the description above. The core of the problem is that I would like to avoid summing multiple SUMPRODUCT / SUMIF formulas (since the real case can include a fairly large amount of columns).

    Thank you very much in advance. Any help is appreciated.

    Best,
    Phil
    Attached Files Attached Files

  2. #2
    Forum Expert XLent's Avatar
    Join Date
    10-13-2010
    Location
    Northumberland, UK
    MS-Off Ver
    various
    Posts
    2,704

    Re: SUMPRODUCT / SUMIF based on both row and column criteria

    To keep your life simple, remove your merged cells in range K5:K11 such that you have Infrastructure/Utility etc repeated on each Project line... merged cells are generally best avoided.

    Assuming you've done the above then, based on my interpretation of requirements (i.e. field positions can fluctuate within Cols A & H), I believe below should work:

    Formula: copy to clipboard
    Please Login or Register  to view this content.

    note: I've assumed ; delimiter - modify to , if required.

    the above, applied to C21:C26, would generate below values for Company 1 to 6, respectively

    Please Login or Register  to view this content.
    edit: I also assumed per your narrative, albeit not reflected in sample, that you could have other non-Infrastructure projects {i.e. numeric columns} in the same dataset
    Last edited by XLent; 03-21-2019 at 05:16 AM.

  3. #3
    Registered User
    Join Date
    03-08-2019
    Location
    Denmark
    MS-Off Ver
    Office 365
    Posts
    57

    Re: SUMPRODUCT / SUMIF based on both row and column criteria

    Thanks, Xlent, worked perfectly in the sample.

    However, once copied to my full data set, I get the error that "Excel ran out of resources while attempting to calculate one or more formulas. As a result, these formulas cannot be evaluated".

    Are you familiar with a workaround this issue? The real data set should be able to handle approximately range A1:DA20000. Further, is there any way to make the formula lighter, since it really slows down the workbook (and the formula will be needed about 10 different places in the workbook).

  4. #4
    Forum Expert XLent's Avatar
    Join Date
    10-13-2010
    Location
    Northumberland, UK
    MS-Off Ver
    various
    Posts
    2,704

    Re: SUMPRODUCT / SUMIF based on both row and column criteria

    that's a pretty big question to try and answer on the fly, without seeing how the file works etc but...

    yes, a SUMPROUCT / Array approach on that size of data will be slow -- the matrix referenced is over 2m cells, and SUMPRODUCTs/Arrays are iterative by nature
    (unlike most native functions they don't limit themselves to the used range intersections)

    based on your request you might find it much quicker to add more calcs

    whilst this may seem counter-intuitive less <> more in XL...
    i.e. thousands of lightweight calcs can perform much, much, quicker than a handful of "elegant" ones (like SUMPRODUCT etc)

    so, using your sample file for ex...

    you could insert a new Row1 & place a simple INDEX/MATCH for each column to pull the "class" for the respective header value
    at this point you would have the Class in Row 1 - e.g. Infrastruture, Utility etc...

    then, in the cols immediate to the right of your data...list each unique Class as a header value

    with above in place add a basic row SUMIF for each row in the dataset - generating a row total per Class.

    then, finally, your "summary" calcs become much more efficient as they need only look at the "fixed" column and name column - ie. SUMIF rather than SUMPRODUCT

    as noted, without a real-life sample, it's hard to give definitive formula references but, hopefully, this gives you the idea?

    I've updated your summary file with illustrations of both the SUMPRODUCT & above approach so you can better visualise
    obviously with the sample dataset you won't see any sort of performance difference but with a real-life source range as large as you describe you should.

    the downside to more calcs is, of course, file size however I would always err towards a fast file than a small file (ideally both of course!)
    Attached Files Attached Files
    Last edited by XLent; 03-21-2019 at 06:26 AM. Reason: typo in narrative

  5. #5
    Registered User
    Join Date
    03-08-2019
    Location
    Denmark
    MS-Off Ver
    Office 365
    Posts
    57

    Re: SUMPRODUCT / SUMIF based on both row and column criteria

    I see your point. I found an alternative method using a dynamic range (as data will usually contain much fewer than 20000 rows). Thanks for all the help, excellent stuff.

    One final question; How do I modify the SUM PRODUCT formula to sum the characters in the data set from right until a certain character (in this case "/")? For instance, some data is given as "Category/Topic/40%", for which I am looking for a separate formula (i.e. not an extension of the previous formula that allows it to handle such cases).

    VALUE(TRIM(RIGHT(SUBSTITUTE(A2;"/";REPT(" ";100));100))) seems to do the extraction part, however, I am not sure how to incorporate it into the SUM PRODUCT formula from before without getting errors.

  6. #6
    Forum Expert XLent's Avatar
    Join Date
    10-13-2010
    Location
    Northumberland, UK
    MS-Off Ver
    various
    Posts
    2,704

    Re: SUMPRODUCT / SUMIF based on both row and column criteria

    Yes, a DNR will help but only to a point.... and ensure you're not using OFFSET to create it else you will simply compound your problems (if running on Auto Calc)

    In terms of the latest issue - well, given the nature of your data (i.e. you don't know where the numbers are, 100% of the time) you have a problem if you explicitly coerce as-is.

    The below would probably work for you -- again, references old sample range so modify to suit requirements and locale etc...

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    I've attached working example, for ease of reference / testing.
    Attached Files Attached Files

  7. #7
    Registered User
    Join Date
    03-08-2019
    Location
    Denmark
    MS-Off Ver
    Office 365
    Posts
    57

    Re: SUMPRODUCT / SUMIF based on both row and column criteria

    Excellent, thanks.

    However, please do note that this time around (unlike) my last post, I do know that the numbers will always be the last characters from right until "/", and that there will only be one % per row (however, the number may have any number of decimals). For clarity, these values are in a separate range that does not interfere with the ones referred to previously and will always be given as "XXXXXX/XXXX/XX%", for which I only need the XX%.

    Would you suggest continuing with your formula above, or modify it to reflect these facts?

    EDIT: It seems the above formula slows down the calculations significantly in the full data set, so if possible, I think something similar to VALUE(TRIM(RIGHT(SUBSTITUTE($A$2:$H$16;"/";REPT(" ";100));100))) would be a better fit, as it requires fewer searches (and as I know the relevant data will always be in the same position (from right to "/").
    Last edited by Phil123456789; 03-21-2019 at 02:19 PM. Reason: Important additional info

  8. #8
    Forum Expert XLent's Avatar
    Join Date
    10-13-2010
    Location
    Northumberland, UK
    MS-Off Ver
    various
    Posts
    2,704

    Re: SUMPRODUCT / SUMIF based on both row and column criteria

    The prior suggestion should work, and the overhead should not prove that significant in the bigger scheme of things so I'd be inclined to use it as it should prove pretty robust.

    If you're saying your summation range, in this latest example, is the only column to be aggregated and whose position is fixed then that would likely warrant an altogether different approach from that back in post 2, and so would likely warrant a new thread; i.e. if your original question has been answered, I hope it has, please mark as Solved [see FAQ if unclear on steps].

    Thanks,

  9. #9
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: SUMPRODUCT / SUMIF based on both row and column criteria

    Here's an example using sumifs instead of sumproduct, which should calculate much faster. It still uses a couple of arrays to set up the criteria, but these will be much smaller than the ones in a sumproduct formula.

    You will notice 3 blocks of coloured cells, each colour contains a different formula, which was entered into the top left cell of that block, then drag filled to the rest.

    Note that I've based this on the sample file from post #1.

    edit:-

    You should be able to use full columns with sumif as opposed to needing dynamic ranges, as long as the number of cells containing the sumif formula is not too high (ideally maximum of around 100 cells).
    Attached Files Attached Files
    Last edited by jason.b75; 03-21-2019 at 02:38 PM.

  10. #10
    Forum Expert XLent's Avatar
    Join Date
    10-13-2010
    Location
    Northumberland, UK
    MS-Off Ver
    various
    Posts
    2,704

    Re: SUMPRODUCT / SUMIF based on both row and column criteria

    Quote Originally Posted by Phil123456789 View Post
    EDIT: It seems the above formula slows down the calculations significantly in the full data set, so if possible, I think something similar to VALUE(TRIM(RIGHT(SUBSTITUTE($A$2:$H$16;"/";REPT(" ";100));100))) would be a better fit, as it requires fewer searches (and as I know the relevant data will always be in the same position (from right to "/").
    To be frank, without a sample file to reference file that accurately reflects your real requirement(s) it's hard to comment as to the feasibility of the above.

    However, as noted earlier, your initial topic/question was answered - with additional suggestions from both myself & others re: follow up question as to how you might avoid use of SUMPRODUCT altogether.

    I would suggest you now close this thread, create a new thread with your latest issue/requirement coupled with a sample file that reflects that setup.

  11. #11
    Registered User
    Join Date
    03-08-2019
    Location
    Denmark
    MS-Off Ver
    Office 365
    Posts
    57

    Re: SUMPRODUCT / SUMIF based on both row and column criteria

    Thanks jason.

    However, please note that my biggest concern is to avoid helper columns and too long formulas, which your suggestion (if converted into a single formula by summing the sumifs) unfortunately would turn out to be, although it undoubtedly would increase calculation speed.

    As I see it, the only real difference between the three formulas in D21:G26 is the project name (D20, E20, F20, and G20). Is there any way to modify your formula, so instead of having one formula for each project, they are combined into one, similarly to IF(COUNTIFS($K$5:$K$11;$B$19;$L$5:$L$11;D$20:G20);SUMIF($B$2:$B$16;$B21;INDEX($C$2:$F$16;0;MATCH(D$20;$C$1:$F$1;0)));""))?

    I am aware that the above formula does not work, but it simplifies the formula.

    As I am looking to pass the project on to somebody else once up and running, I am a little concerned that too long formulas will make it less intuitive and more difficult to understand for the next owner, considering they have not built the model from scratch.

    Thanks in advance.

  12. #12
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: SUMPRODUCT / SUMIF based on both row and column criteria

    This uses volatile functions, which XLent has already advised against using when you mentioned dynamic ranges earlier. (There is no other way to do it with a single formula).

    Formula: copy to clipboard
    Please Login or Register  to view this content.


    edit:-

    I got that wrong, the offseting is not working correctly. Going back for another go.
    Attached Files Attached Files
    Last edited by jason.b75; 03-21-2019 at 03:46 PM.

  13. #13
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: SUMPRODUCT / SUMIF based on both row and column criteria

    Here's an updated test file with a formula that looks like it works this time. Still volatile though.

    Formula: copy to clipboard
    Please Login or Register  to view this content.
    Attached Files Attached Files

  14. #14
    Registered User
    Join Date
    03-08-2019
    Location
    Denmark
    MS-Off Ver
    Office 365
    Posts
    57

    Re: SUMPRODUCT / SUMIF based on both row and column criteria

    Thanks, really appreciate the effort and time.

    It seems to be running OK with XLent's SUM PRODUCT formula and a non-volatile DNR for now, so hopefully it will continue to work once I add further elements to the model.

    Best,
    Phil

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Need to sum values based on set criteria: SUMIF, SUMPRODUCT or something else?
    By Jonathan King in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 12-21-2018, 06:44 AM
  2. Replies: 6
    Last Post: 11-03-2018, 04:50 AM
  3. Combine sumif/sumproduct or sumproduct with multiple criteria
    By sab128 in forum Excel Formulas & Functions
    Replies: 2
    Last Post: 10-16-2018, 08:25 AM
  4. [SOLVED] SUMIF/SUMPRODUCT Multiple Criteria
    By melissanelson110 in forum Excel Formulas & Functions
    Replies: 7
    Last Post: 08-01-2017, 02:20 PM
  5. [SOLVED] Sumif/sumproduct criteria based on integer portion only
    By carsto in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 04-28-2017, 05:37 PM
  6. SUMIF or SUMPRODUCT formula to SUM totals based on multiple criteria
    By relmasri in forum Excel Formulas & Functions
    Replies: 13
    Last Post: 06-01-2016, 01:08 PM
  7. [SOLVED] sumproduct 2 columns based on criteria in 3rd column
    By excel guru i''m not in forum Excel General
    Replies: 5
    Last Post: 12-31-2005, 11:50 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1