DAX CountDistinct with Filter

domtrump

Board Regular
Joined
Apr 1, 2010
Messages
245
I kind of thought this would be pretty easy (and maybe it is) but every example I've come across in searching yields either an error or unexpected results. I have the following measure:

Code:
Gamer_Cnt:=calculate(countrows(HDET01),filter(HDET01,[GAMER]="GAMING"))

I need to convert this into a measure that gives me the DISTINCTCOUNT (based on a field called HDET01[FFRMNO]) for the records in a table that is filtered on [GAMER]="GAMING".

If I just create a DISTINCT COUNT calculation and then create a slicer with the GAMING column, I can get he desired results, so I'm guessing there must be a way to do this in a measure.

Help appreciated.

-Dom
 

Excel Facts

Can you sort left to right?
To sort left-to-right, use the Sort dialog box. Click Options. Choose "Sort left to right"
Great - that worked! Follow up:

I have another measure that gives me the unique count of room numbers for a given day:

Code:
=
SUMX (
    VALUES ( [COLOR=#ff0000][B]DateTable[/B][/COLOR][Date] ),
    CALCULATE ( DISTINCTCOUNT ( [COLOR=#ff0000][B]FactTable[/B][/COLOR][Rm Number] ) )
)

How can I adapt this calculation so that it also has a Criteria of
HDET01[GAMER]="GAMING"?

Thanks.
 
Upvote 0
I tried the formula below, but it gave me way more records than were even in the table! Any suggestions?

Code:
TestUnq_Rms_Game:=SUMX (
    VALUES ( DT_01[Date] ),
   calculate(DISTINCTCOUNT([FFRMNO]),filter(HDET01,[GAMER]="GAMING")))
 
Upvote 0
If you put the DT_01[Date] field on the row of the Pivot Table, theBardd formula gives you the distinct count of room #'s per day for "GAMING". What else are you looking for?
 
Upvote 0
My bad, I should have referenced the original post. I have MONTHS in the pt - not days. If I use the calc above, it just gives me the total number of rooms in the hotel. I need the unique number for each day - summed up for the month. (sorry again)
 
Upvote 0
ok first off, as a matter of best practice in your function code the naming convention is to fully qualify column names ( use HDET01[GAMER], not just [GAMER]) and just use bracket notation only for measures [TestUng_Rms_Game]. It makes the code much easier to read by yourself and others trying to help.

Second, the code you posted above should work for what you want then. Here is a slightly shorter version but functionally (almost) the same:

Code:
TestUnq_Rms_Game:=SUMX ( DT_01[Date] , CALCULATE(DISTINCTCOUNT( HDET01[FFRMNO] ), HDET01[GAMER] = "GAMING")

So when you say "but it gave me way more records than were even in the table!" not sure what you mean unless it is showing months without any data? Do you have any other fields in the rows or columns? DT_01 is related to HDET01 via the Date field right?
 
Last edited:
Upvote 0
With a minor adjustment, it worked great!
Code:
TestUnq_Rms_Game2:=SUMX ( [B]DT_01[/B] , CALCULATE(DISTINCTCOUNT( HDET01[FFRMNO] ), HDET01[GAMER] = "GAMING")[B])[/B]

I don't understand why the first version didn't work either. But this formula returns 234,055 records (without any filters) - the first one returned 1,377,723. ??

Thanks for your help (and advice)!

-Dom
 
Upvote 0

Forum statistics

Threads
1,224,166
Messages
6,176,849
Members
452,748
Latest member
harob1223001

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