Count Distinct Values in Filtered Table across Multiple Columns

zero269

Board Regular
Joined
Jan 16, 2023
Messages
219
Office Version
  1. 365
Platform
  1. Windows
Hello,

I have a formula that counts distinct values within a range of columns.

Excel Formula:
=LET(
ThemeRng,t_MUs[[Theme A]:[Theme C]],
SUM(IF(ThemeRng <> "", 1 / COUNTIF(ThemeRng, ThemeRng), 0)))

However, I'm trying to figure out how to do the same thing when a Table column is filtered. I found a few examples online that work with a single column, just not a range of columns.

Unfiltered table shows a correct value for my distinct themes:

VBA Testing.xlsm
ABCDEF
1Distinct Themes:11
2
3IDMUDescriptive CodeTheme ATheme BTheme C
4P133Random DC for MU 3301 - Theme09 - Theme10 - Theme
5P1157Random DC for MU 15701 - Theme03 - Theme11 - Theme
6P1176Random DC for MU 17606 - Theme08 - Theme
7P1191Random DC for MU 19106 - Theme07 - Theme10 - Theme
8P289Random DC for MU 8902 - Theme05 - Theme
9P2148Random DC for MU 14803 - Theme04 - Theme10 - Theme
10P2181Random DC for MU 18106 - Theme09 - Theme
11P2297Random DC for MU 29701 - Theme09 - Theme
Ct. Distinct
Cell Formulas
RangeFormula
D1D1=LET( ThemeRng,t_MUs[[Theme A]:[Theme C]], SUM(IF(ThemeRng <> "", 1 / COUNTIF(ThemeRng, ThemeRng), 0)))

This filtered table still shows 11. If I can count only the visible cells in the filtered table, it would return 8.

VBA Testing.xlsm
ABCDEF
1Distinct Themes:11
2
3IDMUDescriptive CodeTheme ATheme BTheme C
4P133Random DC for MU 3301 - Theme09 - Theme10 - Theme
5P1157Random DC for MU 15701 - Theme03 - Theme11 - Theme
6P1176Random DC for MU 17606 - Theme08 - Theme
7P1191Random DC for MU 19106 - Theme07 - Theme10 - Theme
Ct. Distinct
Cell Formulas
RangeFormula
D1D1=LET( ThemeRng,t_MUs[[Theme A]:[Theme C]], SUM(IF(ThemeRng <> "", 1 / COUNTIF(ThemeRng, ThemeRng), 0)))

Any help would be greatly appreciated…
 

Excel Facts

What is the last column in Excel?
Excel columns run from A to Z, AA to AZ, AAA to XFD. The last column is XFD.
How about
Excel Formula:
=LET(a,REDUCE("",t_MUs[[Theme A]:[Theme C]],LAMBDA(x,y,VSTACK(x,IF(SUBTOTAL(103,y),y,"")))),ROWS(UNIQUE(FILTER(a,a<>""))))
 
Upvote 1
Solution
Or (somewhat shorter)
Excel Formula:
=ROWS(UNIQUE(TOCOL(MAP(t_MUs[[Theme A]:[Theme C]],LAMBDA(a,IF(SUBTOTAL(103,a),a,1/0))),2)))
 
Upvote 1
How about
Excel Formula:
=LET(a,REDUCE("",t_MUs[[Theme A]:[Theme C]],LAMBDA(x,y,VSTACK(x,IF(SUBTOTAL(103,y),y,"")))),ROWS(UNIQUE(FILTER(a,a<>""))))
Hello Fluff,

I went with your first one. It returned the distinct values for the filtered range of columns.

I noticed the 2nd one doesn't appear to be excluding the empty cells, and returns 1 value higher.

Results:

VBA Testing.xlsm
ABCDEF
1Distinct Themes:11
2Filtered results:8
3Counts EMPTY cells >>>9
4IDMUDescriptive CodeTheme ATheme BTheme C
9P289Random DC for MU 8902 - Theme05 - Theme
10P2148Random DC for MU 14803 - Theme04 - Theme10 - Theme
11P2181Random DC for MU 18106 - Theme09 - Theme
12P2297Random DC for MU 29701 - Theme09 - Theme
Ct. Distinct
Cell Formulas
RangeFormula
D1D1=LET( ThemeRng,t_MUs[[Theme A]:[Theme C]], SUM(IF(ThemeRng <> "", 1 / COUNTIF(ThemeRng, ThemeRng), 0)))
D2D2=LET(a,REDUCE("",t_MUs[[Theme A]:[Theme C]],LAMBDA(x,y,VSTACK(x,IF(SUBTOTAL(103,y),y,"")))),ROWS(UNIQUE(FILTER(a,a<>""))))
D3D3=ROWS(UNIQUE(TOCOL(MAP(t_MUs[[Theme A]:[Theme C]],LAMBDA(a,IF(SUBTOTAL(103,a),a,1/0))),2)))

Thanks again Fluff, this works perfectly…
 
Upvote 0
Are the blank cells actually empty, or do they contain a formula that returns ""
 
Upvote 0
Are the blank cells actually empty, or do they contain a formula that returns ""
Hi Fluff,

In my sample workbook, the cells are empty. The workbook where the formula will be used has data validation using drop-down lists in those three Theme columns.
...
I just tested your 2nd formula in my workbook and it works just like the first one.
Strange to say the least...

Disertation Research Tracker - Ashley.xlsx
A
68
78
DRT
Cell Formulas
RangeFormula
A6A6=LET( a,REDUCE("",t_pAll[[Theme A]:[Theme C]],LAMBDA(x,y,VSTACK(x,IF(SUBTOTAL(103,y),y,"")))), ROWS(UNIQUE(FILTER(a,a<>""))))
A7A7=ROWS(UNIQUE(TOCOL(MAP(t_pAll[[Theme A]:[Theme C]],LAMBDA(a,IF(SUBTOTAL(103,a),a,1/0))),2)))
 
Upvote 0
Glad it's working & thanks for the feedback
 
Upvote 0

Forum statistics

Threads
1,215,068
Messages
6,122,950
Members
449,095
Latest member
nmaske

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