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
at01
Frequent Visitor

How to use filters in the measures

Hello,

 

I am discovering DAX and I would need some help understanding what I do wrong in building my measures.

 

I have the following measure:

-------------------------------------------------------------------------------------

Contacted Clients =
var startDate=min('Date'[Date])
var endDate=max('Date'[Date])
var countClients=distinctcount(Communication[Client ID])
Return
calculate( countClients,
filter(Communication, Communication[Message Created At] >= startDate && Communication[Message Created At] <=endDate)
)

-------------------------------------------------------------------------------------

which counts the number of clients that have been contacted during the period between the dates that are choosen by the user (based on the Date[Date] column.

 

I want to also calculate the number of clients that have been contacted 30 days prior to the first date choosen by the user. SO I defined the following measure:

-------------------------------------------------------------------------------------

Contacted Clients 30 days prior=
var startDate=min('Date'[Date])-30
var endDate=min('Date'[Date])
var countClients=distinctcount(Communication[Client ID])
Return
calculate( countClients,
filter(Communication, Communication[Message Created At] >= startDate && Communication[Message Created At] <=endDate)
)

-------------------------------------------------------------------------------------

but I get the same number in Contacted Clients and Contacted Clients 30 days Prior

 

I suppose I do not understand exactly how it should work, any tips on what needs correcting? Is there an issue that the Date and Communication tables are linked using a Many to one relationship table between Communication[Message Created At] and Date[Date] column?

 

I have tried ALL(Communication) as well to make sure to ignore the filters but I suppose it is not the issue.

 

Thank you very much for any tips on how to correct this!

1 REPLY 1
AlB
Super User
Super User

Hi @at01 

VAriables in DAX are immutable. Their value won't change after declaration. So that calculate will not have any effect whatsoever. Try this: 

Contacted Clients =
VAR startDate =
    MIN ( 'Date'[Date] )
VAR endDate =
    MAX ( 'Date'[Date] )
RETURN
    CALCULATE (
        DISTINCTCOUNT ( Communication[Client ID] ),
        FILTER (
            Communication,
            Communication[Message Created At] >= startDate
                && Communication[Message Created At] <= endDate
        )
    )

 

SU18_powerbi_badge

Please accept the solution when done and consider giving a thumbs up if posts are helpful. 

Contact me privately for support with any larger-scale BI needs, tutoring, etc.

 

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