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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Super User
Super User

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


@ 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!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors