2 criteria Filtered subtotal where the source data is on a different sheet than the criteria

PrettyGood_Not Great

Board Regular
Joined
Nov 24, 2023
Messages
95
Office Version
  1. 365
Platform
  1. Windows
Hi, I have a high octane challenge. I want to have a dynamic subtotal at the top of this range, the caveat however is that the data comprising the subtotal is from another data sheet. The only connection is that both sheets use the same two criteria.

In the subtotal row I am returning the SUM of a given type (A, B, C, D, E) from the source sheet using SUMIF. The source sheet also lists the charge codes for each type. The challenge is to dynamically subtotal the SUMIF result for the types, but only for the results of the filtered charge codes.

Any 365 users out there solved this one?

Note for clarity - The data for the desired subtotal is not what would appear in the array shown below. It is connected to this array only by type and charge code and will only be displayed above the array as a single result above the header cell.

1703780230435.png
 
I don't see any problems. There should be no issues with the subtotal formula...at least down to the last component in the LET formula (you could eliminate anything earlier involving the Primary Criteria---and probably should---but that's not critical). The only critical thing that needs to be revised is to remove the *(pc=M$10) part, as that determines which array positions in the "pc" array correspond to the specified Primary Criteria shown in M10 and to the right (those values being either A, B, D, or G). But the "pc" array does contain values other than A, B, D, or G. Some array elements have values of E or F, and those will be included (if the Primary Criteria really doesn't matter). By inclusion, I mean those rows will be considered, and the hours summed on that row if the Charge Code satisfies the matching condition.

I double checked the part of the formula that determines which array elements in the "cc" array (the Charge Code array formed from column B on the 'Ref Data' worksheet) match any of the Charge Codes shown in the filtered list shown in C12 and down:
Excel Formula:
ISNUMBER(MATCH(cc,FILTER($C$11#,MAP($C$11#,LAMBDA(r,--(SUBTOTAL(103,r)=1)))),0))
I don't see any issues with it...nothing here is affected by the Primary Criteria. I'll refer to the array generated by this formula as the Primary Criteria Row Array, or "pcra" for short.
So the last formula is simply SUM( h * pcra), where "h" is the hours array from the 'Ref Data' worksheet and "pcra" is an array of TRUE/FALSE indicating whether the value shown in the Charge Code column of the 'Ref Data' worksheet matches any of those shown in the filtered list shown in C12 and down.

If you do trim down the formula to eliminate the pc components, remember to redefine the h array, as it is formed from the data array, and "data" has only two columns, not three as it originally did...so the "h" array will be created from the 2nd column of "data":
MrExcel_20240104.xlsx
CLMNOP
8Subtotal160
9
10Charge Code
12X-0110133144155166
13X-0120233244255266
Test2
Cell Formulas
RangeFormula
M8M8=LET(src,'Ref Data'!$A:$N,bcol,INDEX(src,,2),lrow,MAX(IF(ISBLANK(bcol),0,ROW(bcol))),data,DROP(TAKE(CHOOSECOLS(src,2,14),lrow),1), cc,INDEX(data,,1),h,INDEX(data,,2), SUM(h*ISNUMBER(MATCH(cc,FILTER($C$11#,MAP($C$11#,LAMBDA(r,--(SUBTOTAL(103,r)=1)))),0))))
M12:P13M12=IFERROR(INDIRECT("'"&$C12&"'!"&CELL("address",H$13)),"")
Hi KRice, I don't see the what the issue here is either. You are correct that there appears to be nothing wrong with the subtotal, that part of the equation works perfectly. It is the unfiltered result that is truly bizarre. Since the Src has a lot of noise in the sheet where some of our target reference columns contain data other than what we want, I tried using pc on other columns that represent the results wanted, no change. I then cleaned up the src data so that only what we want is available to begin with, no change. The SUM it pulls when unfiltered is approx. only half of the actual total. But again, as soon as I filter for a charge code I get accurate results. strange.
 
Upvote 0

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney
I then cleaned up the src data so that only what we want is available to begin with, no change. The SUM it pulls when unfiltered is approx. only half of the actual total. But again, as soon as I filter for a charge code I get accurate results.
Do you have an example of this? I can't duplicate it. When I use the "pc"-free version of the formula, everything in src data is summed, provided the Charge Code appears in the filtered list...and the result is correct. I'm not sure what you mean by cleaning up src data and Src having a lot of noise. Are there rows in src where the Charge Codes match those in the filtered list, but you do not want to sum them?
 
Upvote 0
Do you have an example of this? I can't duplicate it. When I use the "pc"-free version of the formula, everything in src data is summed, provided the Charge Code appears in the filtered list...and the result is correct. I'm not sure what you mean by cleaning up src data and Src having a lot of noise. Are there rows in src where the Charge Codes match those in the filtered list, but you do not want to sum them?
ok so I think I found the problem. I created a test workbook with two sheets to show you, how to I take the whole workbook as a XLBB? not having luck with this.

In short, what is happening is this: the charge code column (C11#) has multiple instances of any given cc. When I take the ref data sheet to a new workbook and create a list of cc on another sheet that has only one occurance of each cc I get the correct result, both filtered and unfiltered. Then using the same example, if I reference another list of cc that has multiple occurances of each cc then I get my error condition. It in fact has nothing to do with the src data.
 
Upvote 0
Interesting...you can't really upload the entire workbook via XL2BB...it it designed for taking only relatively small samples from a worksheet. If sharing an entire workbook is necessary, then the user can upload it to a reputable file-sharing site (often Dropbox or Google Drive are used) and then share the link to the file...but be sure to not include any sensitive information in the file. If you don't mind sharing the example you've described (where another list of cc with multiple occurrences of each cc is referenced), I'd like to look at it.
 
Upvote 0
Interesting...you can't really upload the entire workbook via XL2BB...it it designed for taking only relatively small samples from a worksheet. If sharing an entire workbook is necessary, then the user can upload it to a reputable file-sharing site (often Dropbox or Google Drive are used) and then share the link to the file...but be sure to not include any sensitive information in the file. If you don't mind sharing the example you've described (where another list of cc with multiple occurrences of each cc is referenced), I'd like to look at it.
ok so I jammed it into one sheet and hacked down the data to get under 3000 cells. This should illustrate well though. p.s. this issue explains why we didn't see this in the original problem solution as that sheet had only on occurrence of each charge code.

dynamic filter debug.xlsx
ABCDEFGHI
1correct resultincorrect resultccpchProblem listworking list
249344A-24AB16A-50A-10
3A-24AB4A-50A-21
4A-24AB1A-50A-22
5A-24AB5A-50A-23
6A-24AB2.5A-50A-24
7A-24AB2A-21A-25
8A-24AB1A-30A-30
9working listProblem listA-24AB1.5A-32A-32
10Charge CodeCharge CodeA-24AB1A-70A-34
11A-10A-50A-24AB0.5A-24A-50
12A-21A-50A-24AB1A-30A-60
13A-22A-50A-24AB0.5A-34A-65
14A-23A-50A-24AB0.5A-32A-70
15A-24A-50A-24AB0.5A-60
16A-25A-21A-24AB1A-21
17A-30A-30A-24AB0.5A-70
18A-32A-32A-24AB1A-70
19A-34A-70A-10AG1.5A-65
20A-50A-24A-23AG2.5A-24
21A-60A-30A-23AG1.5A-30
22A-65A-34A-10AG3.5A-34
23A-70A-32A-23AG36.75A-30
24A-60A-23AG77.25A-21
25A-21A-23AG1.75A-21
26A-70A-23AG8A-30
27A-70A-23AG1A-30
28A-65A-23AG19A-30
29A-24A-23AG0.25A-30
30A-30A-23AG1A-30
31A-34A-23AG0.5A-30
32A-30A-23AG7.5A-30
33A-21A-23AG1A-30
34A-21A-23AG8.75A-30
35A-30A-23AG0.5A-32
36A-30A-23AG1A-32
37A-30A-23AG2.5A-32
38A-30A-23AG0.5A-32
39A-30A-23AG7.5A-32
40A-30A-23AG1.5A-32
41A-30A-23AG3A-32
42A-30A-23AG1.5A-32
43A-30A-23AG5A-32
44A-32A-23AG4A-32
45A-32A-23AG6.5A-32
46A-32A-23AG2A-32
47A-32A-23AG3A-32
48A-32A-23AG5.5A-32
49A-32A-23AG1A-32
50A-32A-23AG0.5A-34
51A-32A-10AG24A-34
52A-32A-10AG2.5A-34
53A-32A-23AG23.75A-34
54A-32A-23AG8A-34
55A-32A-23AG7A-34
56A-32A-10AK1A-34
57A-32A-10AK3.5A-34
58A-32A-10AK4A-34
59A-34A-10AK0.5A-34
60A-34A-10AK1A-34
61A-34A-10AK1A-34
62A-34A-10AK1.5A-34
63A-34A-10AK0.5A-34
64A-34A-10AK0.5A-60
65A-34A-50AK1A-60
66A-34A-50AK1A-60
67A-34A-50AK0.75A-60
68A-34A-10AK1A-60
69A-34A-10AK1.25A-60
70A-34A-10AK1.5A-60
71A-34A-10AK0.25A-60
72A-34A-10AK1A-60
73A-60A-10AK7.5A-60
74A-60A-10AK4.25A-60
75A-60A-10AK13A-60
76A-60A-10AK4.5A-60
77A-60A-10AK3A-60
78A-60A-10AK10A-60
79A-60A-10AK14.5A-70
80A-60A-10AK3.25
81A-60A-10AK13.5
82A-60A-10AK3.25
83A-60A-10AK8.5
84A-60A-10AK5.75
85A-60A-10AK12.5
86A-60A-10AK0.25
87A-60A-10AK0.75
88A-70A-10AK5.5
89A-10AK10
90A-10AK0.5
91A-10AK11.5
92A-10AK8
93A-10AK0.75
94A-10AK7
95A-10AK0.25
96A-50AK0.5
97A-50AK0.5
98A-50AK0.25
99A-50AK0.5
Sheet2
Cell Formulas
RangeFormula
A2A2=LET(src,$D:$F,bcol,INDEX(src,,2),lrow,MAX(IF(ISBLANK(bcol),0,ROW(bcol))),data,DROP(TAKE(CHOOSECOLS(src,1,2,3),lrow),1),cc,INDEX(data,,1),pc,INDEX(data,,2),h,INDEX(data,,3),SUM(h*ISNUMBER(MATCH(cc,FILTER($A11#,MAP($A11#,LAMBDA(r,--(SUBTOTAL(103,r)=1)))),0))))
B2B2=LET(src,$D:$F,bcol,INDEX(src,,2),lrow,MAX(IF(ISBLANK(bcol),0,ROW(bcol))),data,DROP(TAKE(CHOOSECOLS(src,1,2,3),lrow),1),cc,INDEX(data,,1),pc,INDEX(data,,2),h,INDEX(data,,3),SUM(h*ISNUMBER(MATCH(cc,FILTER($B11#,MAP($B11#,LAMBDA(r,--(SUBTOTAL(103,r)=1)))),0))))
A11:A23A11=wl
B11:B88B11=pl
Dynamic array formulas.
Named Ranges
NameRefers ToCells
pl=Sheet2!$H$2:$H$79B11
wl=Sheet2!$I$2:$I$14A11
 
Upvote 0
Thanks for the example. I see the issue. I hadn't anticipated that the filtered list of Charge Codes might contain duplicates. For that scenario, the problem is caused by the MATCH(cc,FILTER($C$11#... construction, as MATCH finds only the first match. So the list of Charge Codes in the data table can only ever match to the first of any duplicates in the filtered list, and the duplicates are never considered. Here is an idea to address that: We use the same approach as before to determine the list of filtered charge codes, but rather than operating on that list right away, we establish it as a variable called "fcc" (for filtered charge codes). Then we use the BYROW LAMBDA function to process fcc one row at a time...each charge code in the "fcc" array is passed as an argument to the same SUM function we had before. This generates a subtotal for each element in the "fcc" array, which is found in the "res" array. Then all of the subtotals in "res" are summed for the final result.

Here is the original version (sans the pc component) and the revised formula using the approach just described:
MrExcel_20240104.xlsx
CLMNOP
8Subtotal65130
9
10Charge Code
11XY-110033445566
35XY-1100
Test2
Cell Formulas
RangeFormula
M8M8=LET(src,'Ref Data'!$A:$N,bcol,INDEX(src,,2),lrow,MAX(IF(ISBLANK(bcol),0,ROW(bcol))),data,DROP(TAKE(CHOOSECOLS(src,2,14),lrow),1), cc,INDEX(data,,1),h,INDEX(data,,2), SUM(h*ISNUMBER(MATCH(cc,FILTER($C$11#,MAP($C$11#,LAMBDA(r,--(SUBTOTAL(103,r)=1)))),0))))
N8N8=LET(src,'Ref Data'!$A:$N,CCcol,INDEX(src,,2),lrow,MAX(IF(ISBLANK(CCcol),0,ROW(CCcol))),data,DROP(TAKE(CHOOSECOLS(src,2,14),lrow),1), cc,INDEX(data,,1),h,INDEX(data,,2), fcc, FILTER($C$11#,MAP($C$11#,LAMBDA(r,--(SUBTOTAL(103,r)=1)))), res, BYROW(fcc,LAMBDA(r,SUM(h*ISNUMBER(MATCH(cc,r,0))))), SUM(res) )
C11:C35C11=CCN
M11:P11M11=IFERROR(INDIRECT("'"&$C11&"'!"&CELL("address",H$13)),"")
Dynamic array formulas.
Named Ranges
NameRefers ToCells
CCN=Lists!$A$3:$A$27C11

And when adapted to your last example, see the yellow-shaded cells:
MrExcel_20240104.xlsx
AB
1correct resultincorrect result
249344
3493101.5
Test3
Cell Formulas
RangeFormula
A2A2=LET(src,$D:$F,CCcol,INDEX(src,,1),lrow,MAX(IF(ISBLANK(CCcol),0,ROW(CCcol))),data,DROP(TAKE(CHOOSECOLS(src,1,2,3),lrow),1),cc,INDEX(data,,1),pc,INDEX(data,,2),h,INDEX(data,,3),SUM(h*ISNUMBER(MATCH(cc,FILTER($A11#,MAP($A11#,LAMBDA(r,--(SUBTOTAL(103,r)=1)))),0))))
B2B2=LET(src,$D:$F,CCcol,INDEX(src,,1),lrow,MAX(IF(ISBLANK(CCcol),0,ROW(CCcol))),data,DROP(TAKE(CHOOSECOLS(src,1,2,3),lrow),1),cc,INDEX(data,,1),pc,INDEX(data,,2),h,INDEX(data,,3),SUM(h*ISNUMBER(MATCH(cc,FILTER($B11#,MAP($B11#,LAMBDA(r,--(SUBTOTAL(103,r)=1)))),0))))
A3A3=LET(src,$D:$F,CCcol,INDEX(src,,1),lrow,MAX(IF(ISBLANK(CCcol),0,ROW(CCcol))),data,DROP(TAKE(CHOOSECOLS(src,1,2,3),lrow),1), cc,INDEX(data,,1),pc,INDEX(data,,2),h,INDEX(data,,3), fcc, FILTER($A$11#,MAP($A$11#,LAMBDA(r,--(SUBTOTAL(103,r)=1)))), res, BYROW(fcc,LAMBDA(r,SUM(h*ISNUMBER(MATCH(cc,r,0))))), SUM(res) )
B3B3=LET(src,$D:$F,CCcol,INDEX(src,,1),lrow,MAX(IF(ISBLANK(CCcol),0,ROW(CCcol))),data,DROP(TAKE(CHOOSECOLS(src,1,2,3),lrow),1), cc,INDEX(data,,1),pc,INDEX(data,,2),h,INDEX(data,,3), fcc, FILTER($B$11#,MAP($B$11#,LAMBDA(r,--(SUBTOTAL(103,r)=1)))), res, BYROW(fcc,LAMBDA(r,SUM(h*ISNUMBER(MATCH(cc,r,0))))), SUM(res) )
 
Upvote 0
=LET(src,'Ref Data'!$A:$N,CCcol,INDEX(src,,2),lrow,MAX(IF(ISBLANK(CCcol),0,ROW(CCcol))),data,DROP(TAKE(CHOOSECOLS(src,2,14),lrow),1), cc,INDEX(data,,1),h,INDEX(data,,2), fcc, FILTER($C$11#,MAP($C$11#,LAMBDA(r,--(SUBTOTAL(103,r)=1)))), res, BYROW(fcc,LAMBDA(r,SUM(h*ISNUMBER(MATCH(cc,r,0))))), SUM(res) )
When I apply this equation I get an unfiltered result many time higher than the sum of the src data.
 
Upvote 0
=LET(src,$D:$F,CCcol,INDEX(src,,1),lrow,MAX(IF(ISBLANK(CCcol),0,ROW(CCcol))),data,DROP(TAKE(CHOOSECOLS(src,1,2,3),lrow),1), cc,INDEX(data,,1),pc,INDEX(data,,2),h,INDEX(data,,3), fcc, FILTER($B$11#,MAP($B$11#,LAMBDA(r,--(SUBTOTAL(103,r)=1)))), res, BYROW(fcc,LAMBDA(r,SUM(h*ISNUMBER(MATCH(cc,r,0))))), SUM(res) )
When I apply this equation I get a #VALUE. scr range, choosecols(), filter(), and map() updated to fit my working sheet.
 
Upvote 0
Thanks for the example. I see the issue. I hadn't anticipated that the filtered list of Charge Codes might contain duplicates. For that scenario, the problem is caused by the MATCH(cc,FILTER($C$11#... construction, as MATCH finds only the first match. So the list of Charge Codes in the data table can only ever match to the first of any duplicates in the filtered list, and the duplicates are never considered. Here is an idea to address that: We use the same approach as before to determine the list of filtered charge codes, but rather than operating on that list right away, we establish it as a variable called "fcc" (for filtered charge codes). Then we use the BYROW LAMBDA function to process fcc one row at a time...each charge code in the "fcc" array is passed as an argument to the same SUM function we had before. This generates a subtotal for each element in the "fcc" array, which is found in the "res" array. Then all of the subtotals in "res" are summed for the final result.

Here is the original version (sans the pc component) and the revised formula using the approach just described:
MrExcel_20240104.xlsx
CLMNOP
8Subtotal65130
9
10Charge Code
11XY-110033445566
35XY-1100
Test2
Cell Formulas
RangeFormula
M8M8=LET(src,'Ref Data'!$A:$N,bcol,INDEX(src,,2),lrow,MAX(IF(ISBLANK(bcol),0,ROW(bcol))),data,DROP(TAKE(CHOOSECOLS(src,2,14),lrow),1), cc,INDEX(data,,1),h,INDEX(data,,2), SUM(h*ISNUMBER(MATCH(cc,FILTER($C$11#,MAP($C$11#,LAMBDA(r,--(SUBTOTAL(103,r)=1)))),0))))
N8N8=LET(src,'Ref Data'!$A:$N,CCcol,INDEX(src,,2),lrow,MAX(IF(ISBLANK(CCcol),0,ROW(CCcol))),data,DROP(TAKE(CHOOSECOLS(src,2,14),lrow),1), cc,INDEX(data,,1),h,INDEX(data,,2), fcc, FILTER($C$11#,MAP($C$11#,LAMBDA(r,--(SUBTOTAL(103,r)=1)))), res, BYROW(fcc,LAMBDA(r,SUM(h*ISNUMBER(MATCH(cc,r,0))))), SUM(res) )
C11:C35C11=CCN
M11:P11M11=IFERROR(INDIRECT("'"&$C11&"'!"&CELL("address",H$13)),"")
Dynamic array formulas.
Named Ranges
NameRefers ToCells
CCN=Lists!$A$3:$A$27C11

And when adapted to your last example, see the yellow-shaded cells:
MrExcel_20240104.xlsx
AB
1correct resultincorrect result
249344
3493101.5
Test3
Cell Formulas
RangeFormula
A2A2=LET(src,$D:$F,CCcol,INDEX(src,,1),lrow,MAX(IF(ISBLANK(CCcol),0,ROW(CCcol))),data,DROP(TAKE(CHOOSECOLS(src,1,2,3),lrow),1),cc,INDEX(data,,1),pc,INDEX(data,,2),h,INDEX(data,,3),SUM(h*ISNUMBER(MATCH(cc,FILTER($A11#,MAP($A11#,LAMBDA(r,--(SUBTOTAL(103,r)=1)))),0))))
B2B2=LET(src,$D:$F,CCcol,INDEX(src,,1),lrow,MAX(IF(ISBLANK(CCcol),0,ROW(CCcol))),data,DROP(TAKE(CHOOSECOLS(src,1,2,3),lrow),1),cc,INDEX(data,,1),pc,INDEX(data,,2),h,INDEX(data,,3),SUM(h*ISNUMBER(MATCH(cc,FILTER($B11#,MAP($B11#,LAMBDA(r,--(SUBTOTAL(103,r)=1)))),0))))
A3A3=LET(src,$D:$F,CCcol,INDEX(src,,1),lrow,MAX(IF(ISBLANK(CCcol),0,ROW(CCcol))),data,DROP(TAKE(CHOOSECOLS(src,1,2,3),lrow),1), cc,INDEX(data,,1),pc,INDEX(data,,2),h,INDEX(data,,3), fcc, FILTER($A$11#,MAP($A$11#,LAMBDA(r,--(SUBTOTAL(103,r)=1)))), res, BYROW(fcc,LAMBDA(r,SUM(h*ISNUMBER(MATCH(cc,r,0))))), SUM(res) )
B3B3=LET(src,$D:$F,CCcol,INDEX(src,,1),lrow,MAX(IF(ISBLANK(CCcol),0,ROW(CCcol))),data,DROP(TAKE(CHOOSECOLS(src,1,2,3),lrow),1), cc,INDEX(data,,1),pc,INDEX(data,,2),h,INDEX(data,,3), fcc, FILTER($B$11#,MAP($B$11#,LAMBDA(r,--(SUBTOTAL(103,r)=1)))), res, BYROW(fcc,LAMBDA(r,SUM(h*ISNUMBER(MATCH(cc,r,0))))), SUM(res) )
Hi! nice attempt but something is off. the result you have in the YELLOW B3 cell needs to be the same as A2 & A3.
 
Upvote 0
the result you have in the YELLOW B3 cell needs to be the same as A2 & A3
Why is that? Every charge code appearing in B11 and down contributes a sum. So if we look at just cc A-50, each of those contributes 4.5, and if we sum all of the contributions for all of the charge codes, the sum is 101.5. The earlier formula did not account for the possibility that there would be more than one of the same Charge Code in the B11 (and down) range...so it only computes a subtotal for the 1st cc A-50 (4.5) and the 1st cc A-24 (39.5)...and the sum of those is 44, which you said was incorrect...right? The revision I provided contributes a subtotal for each instance of a charge code in the B11 (and down) range. Am I missing something?

I'm not sure what to say about posts 17 and 18, as I cannot see your actual worksheet. I would recommend using just one of those formulas. If you don't want the pc criteria, then avoid that version. But you will need to update the indexes and column choices to reflect your actual data. If you can, post the 1st row or first two rows of actual data to show the column locations...sanitize data as necessary.
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,633
Messages
6,125,928
Members
449,274
Latest member
mrcsbenson

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top