Forum Discussion
Help with a count function in DAX
It sounds like all you need is a plain DISTINCTCOUNT.
Project Count = DISTINCTCOUNT('Table Name'[Project])
Seanit looks like DISTINCTCOUNT already ignores blanks, so that CALCULATE is probably redundant.
Edit: nope. I was wrong. Posted too fast. I did a quick test before posting this but I need to work on my reading comprehension. I forgot how many unique values were actually present in my test data, and I used a slightly different formula than Sean. So I ended up using a bad formula to test a bad formula, and concluded that because they agreed they were both right. Turns out they were both wrong. *facepalm*
I made a test set with five rows, similar to the OP example. Three unique project names, one of them repeated, and one blank row. So the correct answer to the count is 3.
Measure A = DISTINCTCOUNT(TableName[Project])
yields a count of 4. 3 distinct values + 1 distinct blank, I guess.
Measure B = CALCULATE(DISTINCTCOUNT(TableName[Project], NOT(ISBLANK(TableName[Project])))
yields a count of 4. I thought ISBLANK([whatever]) was interchangeable with [whatever] = "" but it seems not. I have no idea why. Does a blank in a text column not evaluate true for ISBLANK? Maybe ISBLANK only works on numerical values? I don't see anything in he function documentation to suggest this.
Measure C = CALCULATE(DISTINCTCOUNT(TableName[Project]), TableName[Project] <> "")
(Sean's suggestion) yields 3, the correct answer.
I've tested a little further and found that if the column in question is numerical (integer, decimal, date...) you cannot use the TableName[Column] <> "" construction. The formula simply fails. For those you have to use NOT(ISBLANK(TableName[Column])). I kind of get why <> "" wouldn't make sense for non-text columns, but I still don't get why NOT(ISBLANK()) fails on text.
/end edit.
Does that not give you the results you are looking for litifeta? If not you're going to need to give us more information about any other requirements you have.
Anonymous I wrote this last night without testing it first...
But just tested and it seems it doesn't ignore blanks
EDIT: Wow! Anonymous How much coffee did you drink? I had not seen the Edit...
- Sean10 years agoCommunity Champion
Anonymous With a slight adjustment my formula works on both numbers and text columns - and so does NOT (ISBLANK ...
Projects TEXT & NUMBERS = CALCULATE ( DISTINCTCOUNT ( 'Table'[Project] ), FILTER ( 'Table', 'Table'[Project] <> BLANK() ) ) Projects ALT TEXT & NUMBERs = CALCULATE ( DISTINCTCOUNT ( 'Table'[Project] ), NOT ( ISBLANK('Table'[Project]) ) ) - Anonymous10 years agoNot applicable
Sean my company database has crapped out this morning so I have nothing to do. So it's either this or play games on my phone. And now we all know a little more about how DAX handles blanks.
Edit: also, so much coffee. I'm basically on an IV drip at this point.
- Sean10 years agoCommunity Champion
Anonymous :smileylol: :smileylol: :smileylol: