DISTINCTCOUNTCOLUMNVISIBLECELLS

=DISTINCTCOUNTCOLUMNVISIBLECELLS(ColumnVector)

ColumnVector
Required.

Count the visible unique elements in a ColumnVector, regardless of their frequencies. Works only on a ColumnVector.

GeertD

Board Regular
Joined
Dec 22, 2020
Messages
60
Office Version
  1. 365
Platform
  1. Windows
DISTINCTCOUNTCOLUMNVISIBLECELLS: Count the visible unique elements in a ColumnVector, regardless of their frequencies. Works only on a ColumnVector.
This one is not so trivial. IMO "DistinctCount" should be just another function in the toolbox of the SUBTOTAL function.
Excel Formula:
=LAMBDA(Source,LET(
   IndexFromZero,SEQUENCE(ROWS(Source),,0),
   IsVisible,SUBTOTAL(103,OFFSET(Source,IndexFromZero,0,1)),
   VisibleSourceData,IF(IsVisible,Source,""),
   COUNTA(UNIQUE(VisibleSourceData))-(1-PRODUCT(IsVisible))
))
DXLR's LAMBDA.LET Library_v00.07.xlsb
ABCDEFGHIJ
1Filtered Table:
2First NameLast NameScoreCountryCountRows:20
3AliceLivingstone6GermanyDistinctCount:14(First Name)
4MikaylaLindberg14GermanyDistinctCountColumnVisibleCells:4(First Name)
7MylaStukeby5Germany
9MikaylaSlater14Mexico
11AliceJohansen34MexicoDistinctCount:16(Score)
12KarlTilton11MexicoDistinctCountColumnVisibleCells:6(Score)
21KarlIngoldsby13Germany
23
24Unfiltered Table:
25First NameLast NameScoreCountry
26AliceLivingstone6Germany
27MikaylaLindberg14Germany
28CharlesHurst3Poland
29LondonEdmund1Argentina
30MylaStukeby5Germany
31XzavierAngus4Romania
32MikaylaSlater14Mexico
33AlicePoole4Finland
34AliceJohansen34Mexico
35KarlTilton11Mexico
36MikaylaBruno18Finland
37PaxtonChadsey3USA
38LamontLacy17Slovakia
39JaylanForbes9Egypt
40PaxtonBeckley15Brazil
41AlejandraDonoso22Spain
42DeonteAstley13Thailand
43AlfredSarisbury27Argentina
44KarlIngoldsby13Germany
45RylanHasbrouck33Indonesia
46
Sandbox_Mr.Excel
Cell Formulas
RangeFormula
H2H2=COUNTA(tblTop[First Name])
H3H3=DistinctCount(tblTop[First Name])
H4H4=DistinctCountColumnVisibleCells(tblTop[First Name])
H11H11=DistinctCount(tblTop[Score])
H12H12=DistinctCountColumnVisibleCells(tblTop[Score])
 
Upvote 0
Here's a better formula (note to Old School thinking me: if you have the filter mask, just use it in FILTER):
Excel Formula:
DistinctCountVisibleCells(ColumnVector)
=LAMBDA(Source,LET(
   RowdexFromZero,SEQUENCE(ROWS(Source),,0),
   IsVisible,SUBTOTAL(103,OFFSET(Source,RowdexFromZero,0,1)),
   COUNTA(UNIQUE(FILTER(Source,IsVisible)))
))
(ColumnVector)
(I dropped the "COLUMN" from the name)

Note: all formulas can be mashed together since there is no argument repeat, but I left it like this for clarity and educational puposes.
 
Totally rad, Geert!!!!!Here is non-lambda:
=ROWS(UNIQUE(FILTER(fSales[SalesRep],SUBTOTAL(3,OFFSET(fSales[[#Headers],[SalesRep]],SEQUENCE(ROWS(fSales[SalesRep])),)))))
Same things but less elegant ; )P.S. It is so funny, Mr Excel posted video yesterday and I could not figure out a single cell formula. But this morning during breakfast, it came to me: Boom! So funny how the creative process works.
 
Totally rad, Geert!!!!!Here is non-lambda:
=ROWS(UNIQUE(FILTER(fSales[SalesRep],SUBTOTAL(3,OFFSET(fSales[[#Headers],[SalesRep]],SEQUENCE(ROWS(fSales[SalesRep])),)))))
Same things but less elegant ; )P.S. It is so funny, Mr Excel posted video yesterday and I could not figure out a single cell formula. But this morning during breakfast, it came to me: Boom! So funny how the creative process works.
Well, Mike, how about that?!
You used the ROWS function in the calculation. That is even beter!
Here's why I find that:
  1. The ROWS function is the equivalent to the COUNTROWS function in DAX (and it also counts blank rows!).
    And whenever the similarities between the various environments can be emphasized, I will choose to do so. Helps with the transition between them.
  2. And it also counts blank rows! Making your solution more versatile and more robust than mine.
Thanks, I will adapt, or rather: adopt, in this case. :)

But wait, there's more: there is one thing I don't understand about your solution:
  • You replaced the oh so important arguments "0,1" by "," (just a comma):
    • in the Excel grid that works –I verified myself, too–,
    • but inside of the LET function it doesn't – the "0,1" is crucial (as per my earlier experience and as I explained in my video).
  • any idea as to why this behavior is different?
 
I have no idea why that does not work in LET!?!?!
The "just a comma" in OFFSET columns will default to zero if left empty. The last two arguments in OFFSET assume the dimensions of the first argument if left out.
But I have no idea why it does not work in LET. Anytime the behavior of a function, especially an old standby like OFFSET does not work the same in a new function, that is worrisome... : (
 
I have no idea why that does not work in LET!?!?!
The "just a comma" in OFFSET columns will default to zero if left empty. The last two arguments in OFFSET assume the dimensions of the first argument if left out.
But I have no idea why it does not work in LET. Anytime the behavior of a function, especially an old standby like OFFSET does not work the same in a new function, that is worrisome... : (
Hold on, the starting position of your ColumnVector is different from mine: You start from the header, and therefore need a RowdexFromOne, whereas I start from the upper-left position in the ColumnVector itself (that's what it takes if you put in an entire array instead of a single cell), and therefore I need a RowdexFromZero.
So, in case of my formula the other arguments cannot default to equal zero or (the first element in the vector of) the first argument.
I could differentiate the starting position from the vector itself, but that would introduce an extra argument to input to the function. I see you did exactly that:
  • Your starting position: fSales[[#Headers],[SalesRep]]
  • Your Vector: fSales[SalesRep]
I tested this and indeed that way it works in the LAMBDA.LET-environment, as well, with just the comma – so there is nothing to worry about. :-)

But I'm not going to follow that line of thought, because I feel that adding the need for an additional argument (or conversely: adding more computational complexity [another offset?]) just to be able to use that "just a comma"-trick is a step in the wrong direction, in terms of enduser-friendliness / intuitive use of the function. Please don't take offence.
The way I made the function right now, you use it just the same way as you would any other aggregator function on a column (like SUM, etc.).

Conclusion: everything is predictable and works the same way both in the Excel Grid and in a LAMBDA.LET-environment. :)
 

Forum statistics

Threads
1,223,604
Messages
6,173,312
Members
452,510
Latest member
RCan29

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