SUMPRODUCT to find nth value

Pineapple_Crazy

Board Regular
Joined
May 2, 2017
Messages
51
Hello,

I'm trying to create a formula using SUMPRODUCT that takes into account multiple criteria to return a sum. A portion of the formula is found below for a MTD calculation. However, I can have multiple occurrences of text that are the exact same. I want to be able to return the nth value found at "B7" in the formula below. Like 1st occurrence, 2nd occurrence, 3rd occurrence, etc.

Anyone have any suggestions here?

Thank you!

=IF($B$3="MTD",SUMPRODUCT((TMH!$A$3:$A$100=B7)*(MONTH(TMH!$B$1:$M$1)=MONTH($B$2))*(YEAR($B$2)=YEAR($B$2))*(TMH!$B$3:$M$100)))
 

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
Hello,

In order to have the flexibility of the nth instance, you will probably need an Array Formula with Large() or Small() function ... :wink:
 
Upvote 0
Maybe like this:

=INDEX(TMH!$B$3:$M$100,SMALL(IF(TMH!$A$3:$A$100=B7,ROW(TMH!$A$3:$A$100)-ROW(TMH!$A$3)+1),B8),MATCH(B2,TMH!$B$1:$M$1))
confirmed with Control+Shift+Enter.

where n is in B8. This also assumes that the dates in B1:M1 are the first of the month. If not, this will work:

=INDEX(TMH!$B$3:$M$100,SMALL(IF(TMH!$A$3:$A$100=B7,ROW(TMH!$A$3:$A$100)-ROW(TMH!$A$3)+1),B8),SMALL(IF(MONTH(TMH!$B$1:$M$1)=MONTH(B2),COLUMN(TMH!$B$1:$M$1)-COLUMN(TMH!$B$1)+1),1))
with CSE.

You may have some issues with the way the dates are set up, since this condition:

(YEAR($B$2)=YEAR($B$2))

from your original formula does nothing. How are the dates set up in B1:M1, and what is the date in B2?
 
Upvote 0
Hey Guys,

Thanks! Eric, you are correct about the year portion it should be like this (YEAR(TMH!$B$1:$M$1)=YEAR($B$2)). The problem is I have two drop down boxes. One to select MTD, QTD, and YTD and another to select a date like JAN-2018 (1/1/2018). The formula must feed off of these values selected to sum things up appropriately. The full formula is found below. It's just the value at B7 can be found multiple times in the list and want to be able to just get the first occurrence or fourth occurrence, at my choosing. I guess I'm just not sure how to incorporate the formulas you provided here to make that work. I wish I could upload the workbook, so that you could see what I mean.


=IF($B$3="MTD",SUMPRODUCT((TMH!$A$3:$A$100=B7)*(MONTH(TMH!$B$1:$M$1)=MONTH($B$2))*(YEAR(TMH!$B$1:$M$1)=YEAR($B$2))*(TMH!$B$3:$M$100))/$C$5,IF($B$3="YTD",SUMPRODUCT((TMH!$A$3:$A$100=B7)*(MONTH(TMH!$B$1:$M$1)<=MONTH($B$2))*(YEAR(TMH!$B$1:$M$1))*(TMH!$B$3:$M$100))/$C$5,IF($B$3="QTD",SUMPRODUCT((TMH!$A$3:$A$100=B7)*((ROUNDUP(MONTH(TMH!$B$1:$M$1)/3,0)=ROUNDUP(MONTH($B$2)/3,0))*(YEAR(TMH!$B$1:$M$1)=YEAR($B$2))*(TMH!$B$3:$M$100))/$C$5))))
 
Upvote 0
You can show a sample of your workbook by using one of the tools at the HTML Maker link in my signature.

In the meantime, you can try to replace your formula with:

Code:
=IF($B$3="MTD",SUMPRODUCT((TMH!$A$3:$A$100=B7)*(MONTH(TMH!$B$1:$M$1)=MONTH($B$2))*(YEAR(TMH!$B$1:$M$1)=YEAR($B$2))*(TMH!$B$3:$M$100))/$C$5,IF($B$3="YTD",SUMPRODUCT((TMH!$A$3:$A$100=B7)*(MONTH(TMH!$B$1:$M$1)<=MONTH($B$2))*(YEAR(TMH!$B$1:$M$1)<=YEAR(B2))*(TMH!$B$3:$M$100))/$C$5,IF($B$3="QTD",SUMPRODUCT((TMH!$A$3:$A$100=B7)*((ROUNDUP(MONTH(TMH!$B$1:$M$1)/3,0)=ROUNDUP(MONTH($B$2)/3,0))*(YEAR(TMH!$B$1:$M$1)=YEAR($B$2))*(TMH!$B$3:$M$100))/$C$5),INDEX(TMH!$B$3:$M$100,SMALL(IF(TMH!$A$3:$A$100=B7,ROW(TMH!$A$3:$A$100)-ROW(TMH!$A$3)+1),B3),MATCH(B2,TMH!$B$1:$M$1)))))

confirmed with Control+Shift+Enter. Then change your drop-down list in B3 to include numbers, like {"MTD","QTD","YTD",1,2,3,4,5}.

I'm not sure if that's exactly what you want, and I had some issues testing it, especially the QTD section. But it might be worth looking at.
 
Upvote 0
Hey Eric,

Thanks again! I think this is getting closer, but it seems to sum everything in the column that has a value associated with B7. Did it do this to you when you tried it?

Thanks!
 
Upvote 0
No, it returned a single value. I'm not sure how it can return a sum, since it's an INDEX formula, not SUMPRODUCT. I'm not sure where to go from here without seeing a sample of your data showing the incorrect values.
 
Upvote 0
Here's an example of some data below. I'm taking into account "MTD" and the date of Feb-18 (bold below) to sum values based on a text value ("Available Rooms" in this case). This would sum only for the month of February. Using the formula below I was hoping just to return 4,789 which is the second value of "Available Rooms" found for February. However, this adds up both values for February returning 8,037.

=IF($A$3="MTD",SUMPRODUCT(($A$6:$A$9=A13)*(MONTH($B$4:$M$4)=MONTH($A$2))*(YEAR($B$4:$M$4)=YEAR($A$2))*($B$6:$M$9)),INDEX($B$6:$M$9,SMALL(IF($A$6:$A$9=B13,ROW($A$6:$A$9)-ROW($A$6)+1),2),MATCH(A2,$B$4:$M$4)))

[TABLE="width: 913"]
<tbody>[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Feb-18[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]MTD[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD]JAN
2018[/TD]
[TD]FEB
2018[/TD]
[TD] MAR 2018[/TD]
[TD]APR
2018[/TD]
[TD]MAY 2018[/TD]
[TD]JUN
2018[/TD]
[TD]JUL
2018[/TD]
[TD]AUG 2018[/TD]
[TD]SEP
2018[/TD]
[TD]OCT 2018[/TD]
[TD]NOV 2018[/TD]
[TD]DEC 2018[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Available Rooms[/TD]
[TD]3,596[/TD]
[TD]3,248[/TD]
[TD]3,596[/TD]
[TD]3,480[/TD]
[TD]3,596[/TD]
[TD]3,480[/TD]
[TD]3,596[/TD]
[TD]3,596[/TD]
[TD]3,480[/TD]
[TD]3,596[/TD]
[TD]3,480[/TD]
[TD]3,596[/TD]
[/TR]
[TR]
[TD]Occupied Rooms[/TD]
[TD]2,460[/TD]
[TD]2,344[/TD]
[TD]3,262[/TD]
[TD]2,982[/TD]
[TD]3,288[/TD]
[TD]3,205[/TD]
[TD]3,363[/TD]
[TD]3,295[/TD]
[TD]3,076[/TD]
[TD]3,126[/TD]
[TD]2,524[/TD]
[TD]2,502[/TD]
[/TR]
[TR]
[TD]Occupancy[/TD]
[TD="align: right"]68.4%[/TD]
[TD="align: right"]70.2%[/TD]
[TD="align: right"]90.2%[/TD]
[TD="align: right"]89.2%[/TD]
[TD="align: right"]89.6%[/TD]
[TD="align: right"]93.1%[/TD]
[TD="align: right"]96.0%[/TD]
[TD="align: right"]94.8%[/TD]
[TD="align: right"]89.8%[/TD]
[TD="align: right"]87.8%[/TD]
[TD="align: right"]0.0%[/TD]
[TD="align: right"]0.0%[/TD]
[/TR]
[TR]
[TD]Available Rooms[/TD]
[TD]2,555[/TD]
[TD]4,789[/TD]
[TD]2,222[/TD]
[TD]1,111[/TD]
[TD]3,125[/TD]
[TD]3,480[/TD]
[TD]3,596[/TD]
[TD]3,596[/TD]
[TD]3,480[/TD]
[TD]3,596[/TD]
[TD]3,480[/TD]
[TD]3,596[/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
[TR]
[TD]Available Rooms[/TD]
[TD="align: right"]8,037[/TD]
[TD="align: right"]8,037[/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[TD][/TD]
[/TR]
</tbody>[/TABLE]
^---Formulas found here---^
 
Last edited:
Upvote 0
I'll be away for a few days. I'll check when I can to see if this has been resolved.
 
Upvote 0
I'll be away for a few days. I'll check when I can to see if this has been resolved.


I did find another solution for this by adding another column to get it to sum on two values. Would be curious to know if there was a way to make that formula work though without having to add in an additional column. Thanks bud!
 
Upvote 0

Forum statistics

Threads
1,223,903
Messages
6,175,289
Members
452,631
Latest member
a_potato

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