Nesting INDEX function within a SUMPRODUCT to aggregate values within the correct column - excel

This is an adaption to my previous question regarding how to aggregate values based on lookup IDs and multiple criteria.
This time I would like to index for the correct year. My goal is to get to sheet 3, where a formula contained in cells C4:D6 will reference the product name in column A, the quarter in cell A1 (which the user will input) and the year in cell C3 & D3, and aggregate the relevant sales figures under each region in column B.
In my previous question, I was provided a solution that would nest a SUMIF within a SUMPRODUCT. I am trying to build on this function by adding an INDEX & MATCH formula within the formula to index for the correct year's column in sheet 1. I have tried the following in the report but am getting a #N/A error.
=SUMPRODUCT(SUMIFS(INDEX(Sheet1!$D:$F,0,MATCH(C$3,Sheet1!$D$1:$F$1,0)),Sheet1!C:C,IF(Sheet2!$B$2:$B$8=$B4,Sheet2!$A$2:$A$8),Sheet1!A:A,$A$1,Sheet1!B:B,A4))
UPDATE: It has been discovered that the above formula indeed works. This issue was that Sheet 1 was a pivot table, and as such the column headers for each year was in text format, and was different than the formatting of the look up cell in the report, thus there was no link to reference the data.
Sheet 1 (Raw data)
A
B
C
D
E
F
1
Quarter
Product
ID
2021
2020
2019
2
Q1
A
1
$12
$12
$9
3
Q1
A
3
$4
$30
$50
4
Q1
A
7
$48
$15
$39
5
Q1
A
14
$42
$7
$26
6
Q1
A
25
$36
$50
$20
7
Q1
A
27
$45
$8
$9
8
Q1
A
44
$12
$10
$2
9
Q1
B
1
$40
$32
$23
10
Q1
B
3
$15
$14
$30
11
Q1
B
7
$21
$4
$42
12
Q1
B
14
$38
$26
$13
13
Q1
B
25
$31
$45
$9
14
Q1
B
27
$32
$46
$30
15
Q1
B
44
$21
$40
$30
16
Q2
A
1
$6
$1
$43
17
Q2
A
3
$12
$16
$44
and so forth…
Sheet 2 (lookup table)
A
B
1
ID
Region
2
1
East
3
3
East
4
7
Central
5
14
Central
6
25
Central
7
27
West
8
44
West
Sheet 3 (Report)
A
B
C
D
1
Q1
2
3
Product
Region
2021
2020
4
A
East
$16
$42
5
A
Central
$126
$45
6
A
West
$57
$22

Related

Aggregate values based on lookup IDs while meeting multiple criteria

I am given sales data as per "sheet 1" below. Each row contains the quarter, the product, the region ID, and the sales figure. Each region can have multiple IDs, and I have a lookup table (sheet 2) which denote which region each ID belongs to.
My goal is to get to sheet 3. Essentially I am trying to write a formula that will reference the product name in column A, the quarter in cell A1 (which the user will input), and aggregate the relevant sales figures under each region in column B.
I have tried nesting an INDEX & MATCH function within a SUMIFS within a SUMPRODUCT as per below, but I am getting a #VALUE! error:
=SUMPRODUCT(SUMIFS(INDEX(sheet1!$D:$D$,MATCH(1,($A4=sheet1!$B:$B)*($A$1=Sheet1!$A:$A),0),0),sheet1$C:$C,sheet2!$A$2:$A$8)*(sheet2!$B$2:$B$8=$B4))
Does anyone know what is wrong with my formula, or if there is a better approach to this problem?
Sheet 1 (Raw data)
A
B
C
D
1
Quarter
Product
ID
2021 Sales
2
Q1
A
1
39
3
Q1
A
3
41
4
Q1
A
7
20
5
Q1
A
14
7
6
Q1
A
25
2
7
Q1
A
27
2
8
Q1
A
44
45
9
Q1
B
1
28
10
Q1
B
3
34
11
Q1
B
7
29
12
Q1
B
14
48
13
Q1
B
25
5
14
Q1
B
27
15
15
Q1
B
44
32
16
Q2
A
1
19
17
Q2
A
3
28
and so forth…
Sheet 2 (region ID lookup table)
A
B
1
ID
Region
2
1
East
3
3
East
4
7
Central
5
14
Central
6
25
Central
7
27
West
8
44
West
Sheet 3 (Report)
A
B
C
1
Q1
2
3
Product
Region
Sales
4
A
East
29
5
A
Central
42
6
A
West
31
A little rework:
=SUMPRODUCT(SUMIFS(Sheet1!D:D,Sheet1!C:C,IF(Sheet2!$B$2:$B$8=$B4,Sheet2!$A$2:$A$8),Sheet1!A:A,$A$1,Sheet1!B:B,A4))
Depending on version one may need to confirm with Ctrl-Shift-Enter instead of Enter when exiting edit mode.
With the dynamic array formula FILTER we can replace the IF() part with FILTER():
=SUMPRODUCT(SUMIFS(Sheet1!D:D,Sheet1!C:C,FILTER(Sheet2!$A$2:$A$8,Sheet2!$B$2:$B$8=$B4),Sheet1!A:A,$A$1,Sheet1!B:B,A4))
And it will save a few iterations.

Look up value with multiple criteria

Category QTY $ Disc /unit
A 2
B 4
C 2
A 7
A 7
A 16
B 7
C 5
A 1
C 13
A 6
C 9
B 3
A 4
C 3
Category Min QTY Max Qty $ Disc /unit
A 1 5 $1
A 6 10 $2
A 11 11 + $3
B 1 5 $2
B 6 10 $3
B 11 11 + $4
C 1 5 $3
C 6 10 $4
C 11 11 + $5
I need to get Disc/unit from the table with two criteria
Category can be A, B, or C
QTY can be between / equal to min. QTy and max qty ranges.
This ain't pretty but it works, IF and that is a big IF:
Your data is sorted ascending by column G and then column H
you don't need column I but you can keep it for decoration purposes
The formula in cell D2 and copied down is
=INDEX(INDEX($J$2:$J$10,MATCH(A2,$G$2:$G$10,0)):INDEX($J$2:$J$10,MATCH(A2,$G$2:$G$10,1)),MATCH(B2,INDEX($H$2:$H$10,MATCH(A2,$G$2:$G$10,0)):INDEX($H$2:$H$10,MATCH(A2,$G$2:$G$10,1)),1))
Do not apply that formula to whole columns unless you like to stare at the "Calculating -- 2%" message for minutes on end.

Display rows as blank if another cell is empty

I have the following 'hospitals' sheet in excel:
A B C D E
1 Regions Region 1 Region 2 Region 3 Region 4
2 Region 1 Hospital 1 Hospital 6 Hospital 11 Hospital 15
3 Region 2 Hospital 2 Hospital 7 Hospital 12 Hospital 16
4 Region 3 Hospital 3 Hospital 8 Hospital 13 Hospital 17
5 Region 4 Hospital 4 Hospital 9 Hospital 14 Hospital 18
6 Region 5 Hospital 5 Hospital 10
7 Region 6
8 Region 7
9 Region 8
On my 'report' sheet, I have the following table set up with column headers 'Region' in A6 and 'Hospital' in B6:
A B C D E
6 Region Hospital Dept Admissions Discharges
7 Region 1 Hospital 1 A&E 24 12
8
9
10
11 Hospital 2 Opth 45 76
12
13
14
15 Hospital 3
16
17
18
19 Hospital 4
20
21
22
23 Hospital 5
24
A7 in the table above is a drop-down menu with the values from A2-A9 from my 'hospitals' sheet. When this is entered, I'd like to return a list of hospitals from that particular region in cells B7, B11, B15, B19, B23 etc of my 'report' sheet.
However, when it gets to the last hospital in the respective column on the 'hospitals' sheet, I would then like the formatting of columns A:E on the report sheet to appear as blank, rather than have zeros or #N/A values in columns C-E of my report sheet. Is this something that can be done in VBA?
To summarise, basically, I need some code for the workbook that will show the following range of cells report!A27:E30 to be empty/blank of all formatting if there is no value in 'hospitals!B7'. i.e. when the formulas in column B of the 'report' sheet stop pulling values from column b of the 'hospitals' sheet, everything below this will appear empty.
I'm not sure this is possible.
VBA is not required for the task.
For Sheet 'report' the formula in cells:
B7: =IFERROR("Hospital "&(RIGHT($A$7,1)/1-1)*5+1,"")
B11: =IFERROR("Hospital "&(RIGHT($A$7,1)/1-1)*5+2,"")
B15: =IFERROR("Hospital "&(RIGHT($A$7,1)/1-1)*5+3,"")
B19: =IFERROR("Hospital "&(RIGHT($A$7,1)/1-1)*5+4,"")
B23: =IFERROR(IF(RIGHT($A$7,1)<3,("Hospital "&RIGHT($A$7,1)/1-1)*5+5,""),"")

count data using two columns as references

Is it possible to count or countif by using a column as the data, a cell for the criteria (or what to match) and range of what to count?
Here is what I am looking at:
A1 B C D E F G H I J K L M N O
2 Running Data Total Count of Tardies (by category)
3 Date Employees Leader Start of Shift Break 1 Lunch Break 2 Employees Start of Shift Break 1 Lunch Break 2 Total
4 1-Jul Abe Sue 15 Abe 0
5 3-Jul Steve Bob 20 Anna 0
6 5-Jul Eve Andy 9 20 Eve 0
7 7-Jul Anna Andy 30 Helen 0
8 15-Jul Abe Sue 15 Mark 0
9 18-Jul Anna Andy 10 Steve 0
10 20-Jul Helen Sue 9 0
11 31-Jul Mark Bob 45 0
I am trying to count the data entered on the left (running data) in each category and having it show based on the Employees on the right (in the orange cells). So Abe should show 1 for Start of Shift, Eve should show 1 for Break 1 and Break 2, and Anna should show 2 for Start of Shift.
I have tried using:
=countif(C:C,$J4,D:D) to get the data from JUST Column D for Start of shift, but it gives and error saying too many arguments for the function have been entered.
Help...
...and Thanks!
Countif will only look at 1 column to decide what to count.
Countifs will look at multiple columns. Your formula would look something like this:
=COUNTIFS($C:$C,$J4,E:E,">0")

Sum number according to date and name in excel

To sum the third column (numbers o companies) I've used this
=SUM(1/COUNTIF(Names;Names))
Names is name of array in C column and CTRL+SHIFT+ENTER and it works perfectly.
Now I'd like to sum earnings but only for each company once and with the latest data. For example, the result shoud be like this
=C4+C6+C7+C8+C9+C10
(93)
Thanks
A B C D
1 # company earnings date
2 1 ISB 12 10/11/2011
3 2 DTN 15 11/11/2011
4 3 ABC 13 12/11/2011
5 4 ISB 17 13/11/2011
6 5 RTV 18 14/11/2011
7 6 DTN 22 15/11/2011
8 7 PVS 11 16/11/2011
9 8 ISB 19 17/11/2011
10 9 ANH 10 18/11/2011
Sum 6 93
Assuming ascending dates, you could try with CTRL+SHIFT+ENTER in C11:
=SUM((MAX(A2:A10)-MATCH(B2:B10,LOOKUP(MAX(A2:A10)-A2:A10,A2:A10-1,B2:B10),0)=A2:A10-1)*C2:C10)
I'd suggest using a helper column as the easiest approach. In E2 use this formula
=IF(COUNTIF(B2:B$1000,B2)=1,C2,"")
and copy down the column. Now sum column D for the required answer.
Note that the above formula assumes 1000 rows of data maximum, increase if required.

Resources