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

Need help in DAX, Distinct Count

Hi, I am trying to calculate distinct count for Supplier ID based on Member ID, but it's not working the I want, please find DAX below

DistinctSupplierID =
var __member = T[MemberID]
var __endDate = T[ReportReceived].[Date]
var __startDate = EDATE( __endDate, -6)
var __result =
CALCULATE(
    DISTINCTCOUNT(T[SupplierID]),
        SELECTCOLUMNS(
            FILTER(
                T,
                T[MemberID] = __member
// __startDate <= T[ReportReceived].[Date]
            ),
"SuppID", T[SupplierID]
    )
)
return
    __result
 
Data:
SupplierIDMemberIDReportReceivedDistinctSupplierIDedate
319131-03-2005 00:00230-09-2004 00:00
278131-03-2005 00:00230-09-2004 00:00
214109-08-2005 00:00109-02-2005 00:00
99127-01-2006 00:00127-07-2005 00:00
446121-02-2006 00:00121-08-2005 00:00
446121-02-2006 00:00121-08-2005 00:00
446113-09-2005 00:00113-03-2005 00:00
147130-04-2005 00:00130-10-2004 00:00
112216-12-2005 00:00216-06-2005 00:00
111216-12-2005 00:00216-06-2005 00:00
111216-01-2006 00:00116-07-2005 00:00
111217-02-2006 00:00117-08-2005 00:00
111219-10-2005 00:00119-04-2005 00:00
111224-08-2005 00:00124-02-2005 00:00
111206-07-2005 00:00106-01-2005 00:00
178230-07-2005 00:00130-01-2005 00:00
187207-01-2005 00:00107-07-2004 00:00
109207-11-2005 00:00107-05-2005 00:00
187226-07-2005 00:00126-01-2005 00:00
 
 

 

1 ACCEPTED SOLUTION

@Anonymous , OK, you want this:

 

Column = 
var __endDate = T[ReportReceived].[Date]
var __startDate = EDATE( __endDate, -6)
RETURN
    COUNTROWS(
        DISTINCT(
            SELECTCOLUMNS(
                FILTER(
                    'T',
                    [MemberID] = EARLIER([MemberID]) &&
                        T[ReportReceived].[Date] <= __endDate &&
                            T[ReportReceived].[Date] >= __startDate
                ),
                "SupplierID",[SupplierID]
            )
        )
    )

@ 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...

View solution in original post

4 REPLIES 4
Greg_Deckler
Super User
Super User

Perhaps:

 

Column = COUNTROWS(DISTINCT(SELECTCOLUMNS(FILTER('T',[MemberID] = EARLIER([MemberID])),"SupplierID",[SupplierID])))

@ 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...
Anonymous
Not applicable

The result is 6. I am not expecting this. I want from ant date to last 6 months how many unique Supplier Id. 

For above data the distinct supplier Id column should be 

2,2,4,3,2,2,5,3,5,5,5,3,3,3,2,3,1,4,2

@Greg_Deckler 

@Anonymous , OK, you want this:

 

Column = 
var __endDate = T[ReportReceived].[Date]
var __startDate = EDATE( __endDate, -6)
RETURN
    COUNTROWS(
        DISTINCT(
            SELECTCOLUMNS(
                FILTER(
                    'T',
                    [MemberID] = EARLIER([MemberID]) &&
                        T[ReportReceived].[Date] <= __endDate &&
                            T[ReportReceived].[Date] >= __startDate
                ),
                "SupplierID",[SupplierID]
            )
        )
    )

@ 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...

Oh, you commented out that line in your code so I thought you didn't want it. It is hard for me to test exactly because I'm in the US so your dates don't convert so well, let me see what I can do about that.


@ 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