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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Sum the results of a distinct count by date (or other category)

Hello everyone. 

 

Is there a way I can take a distinctcount and sum the values that comes from it dynamically to be used in a calculation?  The results would need to change by date ranges that are being viewed.

 

My data is below but right now I'm using this measure in DAX:

Calculate(
DistinctCOUNT(Fact[VehicleID]))
When I pull this into a table and I group it week to week it is working fine.  The issue is the "total" is also doing a select distinct.  So say I pull a range of 4 weeks. 
 
Each week counts correctly where
week 1 is 30
week 2 is 28
week 3 is 33
week 4 is 24
 
But the toal is showing 36 since it is counting the distinct number of vehicles that occurred in that month.  
 
I want it to sum.  So the above example the total would be 115.  

 

Here is some example data.  I am going to do a similar calculation for Drivers as well but I am hoping I can utilize what is good for this for that.  

 

Table:      Results:     
Driver1VehicleIDBillDateLocationCode    VehicleCountBillDateLocationCode
BillA7/13/2019HERE    27/13/2019HERE 
BillA7/13/2019HERE    37/13/2019ELSE 
BillA7/13/2019HERE    17/20/2019HERE 
BillA7/13/2019HERE        
JeffB7/13/2019HERE        
JeffB7/13/2019HERE    Month   
JeffB7/13/2019HERE    VehicleCountBillDateLocationCode
JeffB7/13/2019HERE    3JulyHERE 
JeffB7/13/2019HERE    1JulyHERE 
JaneB7/13/2019HERE        
JaneA7/20/2019HERE        
JaneA7/20/2019HERE        
JaneA7/20/2019HERE        
JaneA7/20/2019HERE        
JaneA7/20/2019HERE        
JaneA7/20/2019HERE        
JaneA7/20/2019HERE        
JaneA7/20/2019HERE        
JaneA7/20/2019HERE        
JaneA7/20/2019HERE        
BillA7/13/2019ELSE        
BillA7/13/2019ELSE        
BillA7/13/2019ELSE        
BillA7/13/2019ELSE        
JimC7/13/2019ELSE        
JimC7/13/2019ELSE    `   
JimC7/13/2019ELSE        
JimC7/13/2019ELSE        
JimC7/13/2019ELSE        
JeffC7/13/2019ELSE        
JeffD7/13/2019ELSE        
JeffD7/13/2019ELSE        
JeffD7/13/2019ELSE        
JeffD7/13/2019ELSE        
JackE7/13/2019ELSE        

 

1 ACCEPTED SOLUTION
d_gosbell
Super User
Super User

So if week is the lowest date grain you could probably do something like the following looping over each BillDate and summing the distinct counts:

 

Distinct Vehicles per week = SUMX( VALUES('Table'[BillDate]), CALCULATE(DISTINCTCOUNT('Table'[VehicleID])))

I'm not sure if this meets your requirements or not as I get different values to what you show in your expected results. But you are not showing the expected grand total and I can't see how you got your results (eg. on 13 July I can see 4 different vehicle IDs for the ELSE location, not 3)

 

201909 sum distinctcount.png

View solution in original post

1 REPLY 1
d_gosbell
Super User
Super User

So if week is the lowest date grain you could probably do something like the following looping over each BillDate and summing the distinct counts:

 

Distinct Vehicles per week = SUMX( VALUES('Table'[BillDate]), CALCULATE(DISTINCTCOUNT('Table'[VehicleID])))

I'm not sure if this meets your requirements or not as I get different values to what you show in your expected results. But you are not showing the expected grand total and I can't see how you got your results (eg. on 13 July I can see 4 different vehicle IDs for the ELSE location, not 3)

 

201909 sum distinctcount.png

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.