Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
2 years ago

Filtering with dates

Hi,

 

I'm new to DAX queries and trying to use the below code although I keep getting an error.

 

I want to filter based on the last 2 years of data based on todays date.

My Date_Rd column is in a string format.  I keep getting an error for some reason.

 

Any help is greatly appreciated.

 

Thanks

 

EVALUATE
FILTER(
    'values',
    DATEVALUE('values'[Date_Rd]) >= TODAY() - 730
)

 

3 Replies

  • rubayatyasmin's avatar
    rubayatyasmin
    Community Champion

    hi, Anonymous 

     

    you can also use Now() instead of Today(). But ofcourse what ChielFaber suggested you can also use DATEDIFF to determine date differences. Here is the example code for you,

     

    EVALUATE
    FILTER(
    'values',
    DATEVALUE('values'[Date_Rd]) >= DATE(YEAR(NOW()) - 2, MONTH(NOW()), DAY(NOW()))
    )

     

    it should solve your issues for now. Also, please add the error message of screen shot here as well. 

     

     

     

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Anonymous ,

    What is the format of your [Date_Rd] column?

    Try to convert it first. Here’s an example of how you can do this if your dates are in the format DD/MM/YYYY:

    EVALUATE
    FILTER(
        'values',
        DATEVALUE(
            RIGHT('values'[Date_Rd], 4) & "-" &
            MID('values'[Date_Rd], 4, 2) & "-" &
            LEFT('values'[Date_Rd], 2)
        ) >= TODAY() - 730
    )

    Best Regards,
    Gao

    Community Support Team

     

    If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
    If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

    How to get your questions answered quickly --  How to provide sample data in the Power BI Forum