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

why can't I use firstdate() in a filter expression

I received an error message with the DAX message below:

 

New Hires2 =
CALCULATE (
DISTINCTCOUNT ( UserDirectory[USERID] ),
UserDirectory[Hire Date]
>= FIRSTDATE('Calendar'[Date] )
&& UserDirectory[Hire Date]
<= LASTDATE('Calendar'[Date] ))

 

Error message: A function 'FIRSTDATE' has been used in a True/False expression that is used as a table filter expression. This is not allowed.

 

I have replaced firstdate() with min() which doesn't work either. Why can't i use functions in a filter condition?

1 ACCEPTED SOLUTION
v-xiaotang
Community Support
Community Support

Hi @Anonymous 

 

CALCULATE(<Expression>[,<Filter>[,<Filter>[,...]]])
Filter:A boolean (True/False) expression or a table expression that defines a filter.

FYI: https://dax.guide/calculate/ 

 

So, you can use a boolean (True/False) expression like this:

 

New Hires2 = 
VAR _firstdate =
    FIRSTDATE ( 'Calendar'[Date] )
VAR _enddate =
    LASTDATE ( 'Calendar'[Date] )
RETURN
    CALCULATE (
        DISTINCTCOUNT ( UserDirectory[USERID] ),
        UserDirectory[Hire Date] >= _firstdate
            && UserDirectory[Hire Date] <= _enddate
    )

 

OR

use a table expression like this:

 

New Hires2 =
CALCULATE (
    DISTINCTCOUNT ( UserDirectory[USERID] ),
    FILTER (
        ALL ( UserDirectory ),
        UserDirectory[Hire Date] >= FIRSTDATE ( 'Calendar'[Date] )
            && UserDirectory[Hire Date] <= LASTDATE ( 'Calendar'[Date] )
    )
)

 

 

Best Regards,

Community Support Team _ Tang

If this post helps, please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

5 REPLIES 5
v-xiaotang
Community Support
Community Support

Hi @Anonymous 

 

CALCULATE(<Expression>[,<Filter>[,<Filter>[,...]]])
Filter:A boolean (True/False) expression or a table expression that defines a filter.

FYI: https://dax.guide/calculate/ 

 

So, you can use a boolean (True/False) expression like this:

 

New Hires2 = 
VAR _firstdate =
    FIRSTDATE ( 'Calendar'[Date] )
VAR _enddate =
    LASTDATE ( 'Calendar'[Date] )
RETURN
    CALCULATE (
        DISTINCTCOUNT ( UserDirectory[USERID] ),
        UserDirectory[Hire Date] >= _firstdate
            && UserDirectory[Hire Date] <= _enddate
    )

 

OR

use a table expression like this:

 

New Hires2 =
CALCULATE (
    DISTINCTCOUNT ( UserDirectory[USERID] ),
    FILTER (
        ALL ( UserDirectory ),
        UserDirectory[Hire Date] >= FIRSTDATE ( 'Calendar'[Date] )
            && UserDirectory[Hire Date] <= LASTDATE ( 'Calendar'[Date] )
    )
)

 

 

Best Regards,

Community Support Team _ Tang

If this post helps, please consider Accept it as the solution to help the other members find it more quickly.

Fowmy
Super User
Super User

@Anonymous 

CALCULATE accepts boolean expressions or table expressions that define filters, or filter modifier functions.
You can modify your measure as follows using variables:

New Hires2 =
var __mindate = min('Calendar'[Date] )
var __maxdate = max('Calendar'[Date] )
return
    CALCULATE (
        DISTINCTCOUNT ( UserDirectory[USERID] ),
        UserDirectory[Hire Date] >= __mindate && 
        UserDirectory[Hire Date] <= __maxdate
    )

 



Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

Anonymous
Not applicable

Thanks! I enjoy watching the tutorial. It's very helpful.

Anonymous
Not applicable

Sorry, I'm proably a pain in the neck. What do you mean that CALCULATE accepts boolean expressions? Does that mean I can not wrap a function (e.g., min()) in a CALCULATE expression?

@Anonymous 

I would like to share some links for you to understand the concepts better:

https://dax.guide/calculate/
https://www.youtube.com/watch?v=Tk-7gBt9CDE&list=PLU6II7MW-aiIees6mrPfdjt9c8noi7P66



Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

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.