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

The Power BI Data Visualization World Championships is back! It's time to submit your entry. Live now!

Reply
Anonymous
Not applicable

Distinct Count is very slow, is there any alternative

Hi,

I would like to calculate DistinctCount(SupplierID) over the previous 6 months for the same MemberID. For the same, I wrote following DAX;

DistinctSupplierID =
CALCULATE(
DISTINCTCOUNT('Sales&Orders'[SupplierID]),
DATESINPERIOD(
'Sales&Orders'[ReportReceived].[Date],
LASTDATE('Sales&Orders'[ReportReceived].[Date]),
-6,
MONTH),
FILTER(
'Sales&Orders',
'Sales&Orders'[MemberID]= EARLIER('Sales&Orders'[MemberID])
)
)
Unfortunately its taking too much time. Is there any alternative to make it faster.
Thank you in advance.
6 REPLIES 6
Anonymous
Not applicable

Please find data;

SupplierIDMemberIDReportReceived
319131-03-2005 00:00
278131-03-2005 00:00
214108-09-2005 00:00
99127-01-2006 00:00
446121-02-2006 00:00
446121-02-2006 00:00
446113-09-2005 00:00
147130-04-2005 00:00
112216-12-2005 00:00
111216-12-2005 00:00
111216-01-2006 00:00
111217-02-2006 00:00
111219-10-2005 00:00
111224-08-2005 00:00
111207-06-2005 00:00
178230-07-2005 00:00
187201-07-2005 00:00
109211-07-2005 00:00
187226-07-2005 00:00
Anonymous
Not applicable

// T is your table's name

// calc column without the use of context transition
# DistinctSupplierID =
var __member = T[MemberID]
var __endDate = T[ReportReceived]
var __startDate = edate( __endDate, -6 )
var __result
	distinctcount(
		selectcolumns(
			filter(
				T,
				T[MemberID] = __member
				&& __startDate < T[ReportReceived]
				&& __T[ReportReceived] <= __endDate
			),
			"SuppID", T[SupplierID]
		)
	)
return
	__result
	

 

Best

D

Anonymous
Not applicable

What is the second && statement can you please explain. @Anonymous 

&& __T[ReportReceived] <= __endDate

Anonymous
Not applicable

Remove __ from __T. It's a mistake.

Best
D
Anonymous
Not applicable

It's not working, I am getting error The syntax for 'distinctcount' is incorrect. 

Greg_Deckler
Community Champion
Community Champion

Well you can use COUNTROWS coupled with DISCTINCT or VALUES. You could eschew time intelligence functions in favor of simple filtering. Hard to test anything without sample data. Please see this post regarding How to Get Your Question Answered Quickly: https://community.powerbi.com/t5/Community-Blog/How-to-Get-Your-Question-Answered-Quickly/ba-p/38490



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! It's time to submit your entry.

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 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.