Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends September 15. Request your voucher.

Reply
Anonymous
Not applicable

Help wanted: aggregate distinct count based on 4 groupby criteria.

Hi Everyone,

 

I am trying to get a distinct count of patient visits grouped by 4 criteria (grouper, month, financial class, financial ID) so that I can calculate revenue projection. However, my formula to get that distinct count does not work as GROUPBY METHOD cannot be used together with DISTINTCTCOUNT method. Please see my formula below. Is there a way to get around? Or Do I need to build a custom function? I thought PowerBI has that build-in function but it does not appear so? Any suggestions are appreciated. 

 

Formula:

 

Distinct Count Grouped By 4 Criteria = GROUPBY('TABLE1','TABLE1'[FISCAL_MONTH],'TABLE1'[Grouper],'TABLE1'[FINANCIAL_CLASS_ID],'TABLE1'[FINANCIAL_CLASS],"Grouped Unfiltered Count",DISTINCTCOUNT(CURRENTGROUP(),COUNTROWS(SELECTCOLUMNS('TABLE1', [PATIENT_VISIT ]))))

 

There are other formulas needed to get to the final expected output, but I am not including them to confuse readers.

Dummy Data Source is on the Data tab.

PATIENT_VISITMonth_NameCal_YrFISCAL_YEARGrouperCATEGORYTOT_CHGSTOT_PMTSTOT_INS_PMTSTOT_PAT_PMTSTOT_ADJTOT_INS_ADJTOT_PAT_ADJTOT_ACCT_BALFINANCIAL CLASSFINANCIAL ID
1July202120223M1452000-452-452002A
2April202220223M1452000-452-452004B
3July202120223M2452000-452-452005C
4July202120223M2452000-452-45200102D
5July202120223M2452000-452-45200105E
6July202120223M2452000-452-45200106F
1July202120223M2452000-452-45200107G
1July202120223D3452000-452-452002H
1December202120223M3452000-452-452004I
2July202120223M3452000-452-4520012J
22October202120223M2452000-452-45200102J
22September202120223M4452000-452-45200105K

Expected Output is on the Expected Output tab. 

CATEGORYFINANCE IDFINANCE CLASSJulAugSepOctNovDecJanFebMarAprMayTOTAL 
12A-219386-219386-232926-223778-219386-217008-217282-204108-236311-206761-189104 
14B-260.125-227.61-265.545-265.545-373.93-319.737-314.318-276.383-373.93-281.802-325.157 
15C000-392.860-196.4300000 
2106M-2390.8-2091.95-2988.5-2988.5-3586.19-2988.5-1793.1-2390.8-1494.25-2689.65-2988.5 
2107N-39454.1-45372.2-50194.4-46249-50852-48440.9-48440.9-37481.4-52824.7-49536.8-54578.2 
32A-322826-324751-328216-329756-306271-280091-305886-293951-338996-299341-257568 
3102J00000000000 
42C-817166-890173-886227-832106-825623-784469-773757-738804-888482-782495-697086 
44C-27386.5-27190.2-28368.1-23263.8-22674.9-19828.3-22380.4-19141.1-22085.9-19730.1-17766.9 
45N-2601.8-3469.06-2698.16-2312.71-2794.52-1830.9-1445.44-2409.07-2698.16-2505.44-1252.72 
               

 

1 ACCEPTED SOLUTION
Ashish_Mathur
Super User
Super User

Hi,

I do not understand how distinctcount of Patient ID's can be negative number (see your innitial post).  You may download my PBI file from here.

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

6 REPLIES 6
Ashish_Mathur
Super User
Super User

Hi,

I do not understand how distinctcount of Patient ID's can be negative number (see your innitial post).  You may download my PBI file from here.

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Anonymous
Not applicable

@Ashish_Mathur thank you! This does calculate the distinct count of visitor by multiple groupby criteria. Curious how did you come up with this solution? The calendar table is created as a dimension for filtering purpose? 

You are welcome.  Just learnt it myself.  If my previous reply helped, please mark that reply as Answer.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Anonymous
Not applicable

I would probably just use REMOVEFILTERS + VALUES (a very common pattern in DAX).
For instance within the Contoso model let's suppose we want to count distinct products by Category + Country pair. By using the basic measure:

 

DC_Basic = DISTINCTCOUNT ( 'Sales'[ProductKey] )

 

You will get correct results only if the Category and Country are the only columns in your visual. One additional issue might be that your total will show +1 as compared to the expected number. If I remember correclty this is happening because of the blank row feature in DAX. There are values in the dimension that do not exist in the Fact table (Sales in Contoso), and DAX adds one blank row to account for these. Some functions take into account this hidden blank row and some don't.

To avoid this issue you can filter by the Sales table:

 

DC_Basic_Calculate = 
CALCULATE (
    DISTINCTCOUNT ( 'Product'[ProductKey] ),
    'Sales'
)

 

However, in order to obtain a correct count regardless of the columns used in the visual you need to alter the filter context that the visual is creating (for instance in the table on the right where I added the SubCategory column).

You can use REMOVEFILTERS or ALL (the former is a new function used as an alias for the latter, but it is only used as a CALCULATE modifier and does not return a table as ALL does).

As you can see, the third measure returns the correct counts even for arbitrary visuals.

 

DC_REMOVEFILTERS = 
VAR Categories = VALUES ( ProductCategory[ProductCategoryKey] )
VAR Countries = VALUES ( Geography[RegionCountryName] )

RETURN
CALCULATE (
    DISTINCTCOUNT( 'Sales'[ProductKey] ),
    REMOVEFILTERS ( 'Product' ),
    REMOVEFILTERS ( Geography[RegionCountryName] ),
    Categories,
    Countries
)

 

You may notice that in the second measure I count over the Product table (best practice) while in the 1st and 3rd - over the Fact. In this case I'm doing that because the two columns I have to count by are from two different dimensions and the filters do not propagate from one another, but they do "meet" at Sales level. Having bi-directional relationships that would progagate the filters across dims would allow to count again over the Product, but introducing bi-directional relationships would be even a worse alternative than counting over the Fact.

One more point. in the last measure I am removing filters from the whole Product table but only from 

Geography[RegionCountryName] column. That is because I'm dragging into the table on the right the Subcategory and have to remove the filter either from both Category/Subcategory tables used in the visual or the Product table for which the extended table contains the Category and Subcategory, or shorter (but not necessarily better) just from the Fact table (where the extended table contains the whole model).

F_Bastiat_1-1658543810060.png

 

 

Anonymous
Not applicable

If your visual is at the granularity of the four columns that you want the DISTINCTCOUNT by, then you don't need to write any complex dax, just write a basic DISTINCTCOUNT ( Col ) and drag it in the table/matrix next to the four columns. The result should be as if you computed the values in a SQL statement with GROUP BY the 4 fields. This is because Power BI will automatically create for you a filter context based on the fields you used in the visual (+ slicers and the filter pane).

If you need the result to be computed independently of the visuals granularity it is used in, then you have to use functions like SUMMARIZE or SUMMARIZECOLUMNS (or maybe even GROUPBY, but it is used very rarely when authoring measures) and compute the DISTINCTCOUNT() at that custom granularity irrespective of the visuals.

Anonymous
Not applicable

Hi, @Anonymous , thank you so much for getting back to me!  

I did initially use the suggested method (calculate(distinctcount()) to get a distinct count of patient visit. I need to use that value to multiply other 2 aggregate values to calculate revenue project, which does not work in PowerBI. That's why I am trying the groupby method. If you are using summarize()/groupby(), how would you go about calculating that distinct value that can be filtered by named 4 criteria? Thanks again.  

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.