Tricky counting formula

mjones

Board Regular
Joined
Oct 27, 2007
Messages
94
Office Version
  1. 365
Platform
  1. Windows
Hi All,

At > 6000 rows, it’s impossible to manually count so I hope a formula genius out there can help.

All cells are general format with text.

Count how many situations occur with rows with the same text in columns B, D & E that also have a row with x in column G and another row with y in column G.

The case below would count 1 for rows 1 and 2 matching the scenario, but rows 3 and 4 do not count because even though B, D & E match, there are not two rows where one has an x and one has a y in column G.

B D E G
1 AB CD EF x
2 AB CD EF y
3 GH KL TY z
4 GH KL TY y

The above is in the 'Songs in Playlists'! tab, but the count/answer will be in another tab.

Thank you for considering my formula,

Michele
 
Oh good!...so those numbers make sense to you? I'll give some thought to a more efficient way.
 
Upvote 0

Excel Facts

Create a Pivot Table on a Map
If your data has zip codes, postal codes, or city names, select the data and use Insert, 3D Map. (Found to right of chart icons).
To confirm, yes numbers are correct - formula works! The speed is just unworkable and most likely will worsen when the source data is doubled. I didn't know a formula could slow Excel so much but it makes sense. Crossing fingers. Good night.
 
Upvote 0
Good...about the numbers being correct. And yes, some formulas can make a worksheet sluggish. I've reviewed your earlier comment---I guess there is no need to sum the number of rows where B,D,E match with one of the G=x or G=y terms because you've said that there would ever only be one or none with an x, and one or none with a y. And that appears to be the case, so I think there are some other approaches to try out. Just to confirm then, for any chosen B,D,E entry, you're expecting the number of pairs (after considering the G=x and G=y criteria) will be either 0 or 1....and you want the sum of those 0's and 1's for the entire set of B,D,E on the 'Songs...' worksheet?
 
Upvote 0
I think this idea is much better. We start by defining the source data "src" and ensure that the row labels that consist of two Opt. Playlists categories use exact spelling for matching purposes, and the two categories use a colon space delimiter. The formula extracts the first of those (to the left of ": ") as the x match target, and the second of those (to the right of the delimiter) as the y match target. For convenience, we specify variable "g" as the data in the 6th column of src data...col G. Next we perform a simple concatenation on the B, D, E columns (cols 1, 3, 4 of src dat), separating each with a pipe character (|)...and we call this single column array "bde". Then we filter the 5000+ row bde array to obtain only those entries where g=x (the G column matches the 1st category---Easy Listening for example). This trims the 5000+ array down to several hundred entries. And then we take only unique entries in this list. Strictly speaking, this UNIQUE operation is probably not necessary since there should be no more than one of any bde entry whose g=x. This UNIQUE/FILTER bde array is called "fsx", short for filtered source data with "x" matching.
Finally, we return to the full length "g" array (5000+ entries in it) and perform a logical test...where does g=y (the 2nd category..."Oldies (Pre 1970)" for example)? We also perform a MATCH, checking to see if each item in the 5000+ bde array matches any one of the unique, filtered fsx entries (where the x match is already known to exist)...ISNUMBER converts the MATCH result to TRUE or FALSE, and both arrays are multiplied together inside SUMPRODUCT to determine which array index positions satisfy both criteria, resulting in either a 0 or 1 for each array position. SUMPRODUCT then sums the column. This should be much faster than the earlier approach.
Michele's iTunes Music_revKR.xlsx
AB
22Easy Listening300
23Easy Listening: Oldies (Pre 1970)34
24Easy Listening: 70's Music28
25Easy Listening: 80's18
26Easy Listening: 90's & Newer12
Playlist Totals
Cell Formulas
RangeFormula
B22B22=COUNTIF('Songs in Playlists'!G$2:G$5748,A22)
B23:B26B23=LET(src,'Songs in Playlists'!B$2:G$5748, x,TEXTBEFORE(A23,": "), y,TEXTAFTER(A23,": "), g,INDEX(src,,6), bde, INDEX(src,,1) & "|"& INDEX(src,,3) & "|" & INDEX(src,,4), fsx,UNIQUE(FILTER(bde,g=x,"")), SUMPRODUCT((g=y) * ISNUMBER(MATCH(bde,fsx,0))))
 
Upvote 1
Solution
Now you have it! Formula works and at a fast speed!

A thing to note is that column G has a drop down from a list and thus won't have a typo. Plus, you are correct in that any song will have only unique items in column G. If I put more than one of the same playlist in column G (e.g. easy listening or 80's), other sums will give me a warning and I'll find it that way, i.e. sums in iTunes won't match those in the spreadsheet because iTunes won't let you put a song in more than one playlist.

I cannot thank you enough. I do hope you enjoyed the tricky question. All the best!

Michele
 
Upvote 0
Michele, I noticed that about column G...a good idea to use a drop down list for consistency. And in testing out the formula--and making some targeted changes to the source table to confirm counting accuracy--I saw the TRUE/FALSE flags alert. So I think you could safely avoid performing the UNIQUE operation, as it would not actually eliminate anything because of the other checks. In that case, a minor tweak:
Excel Formula:
=LET(src,'Songs in Playlists'!B$2:G$5748, x,TEXTBEFORE(A23,": "), y,TEXTAFTER(A23,": "), g,INDEX(src,,6),
bde,INDEX(src,,1)&"|"&INDEX(src,,3)&"|"&INDEX(src,,4), fsx,FILTER(bde,g=x,""), SUMPRODUCT((g=y)*ISNUMBER(MATCH(bde,fsx,0))))
You're very welcome...I appreciate your patience working through the formula evolution.
 
Upvote 0
New formula works and is fast. I think I should be thanking you for your patience. Thank you!
 
Upvote 0

Forum statistics

Threads
1,215,069
Messages
6,122,959
Members
449,096
Latest member
Anshu121

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