Forum Discussion

Jkrupski's avatar
Jkrupski
Regular Visitor
1 year ago
Solved

Check if a date column is between 2 hard coded dates

Hello,

 

I am very new to power bi and dax, learning as I go.

 

I am trying to use an if statement that checks a date added column and compares it to 2 hard coded dates, if it is between the dates I want to display "P3" and if not then display "NA" in a new column. I have this code:

columnTest =

VAR dateAdded = FORMAT(table[column], "m/d/yyyy")

VAR P3Start = FORMAT(DATE(2025,3,29), "m/d/yyyy")
VAR P3End = FORMAT(DATE(2025,3,29), "m/d/yyyy")


RETURN


If(dateAdded <= P3End && dateAdded >= P3Start, "P3",
 "NA")
 
One date that is checking is 3/4/25, but it is constantly giving me the NA result where I want it to give the P3 result.
 
Any help would be greatly appreciated!
 
Thank you!
  • Jkrupski Try:

    columnTest =
    VAR dateAdded = [column]
    VAR P3Start = DATE(2025,3,29)
    VAR P3End = DATE(2025,3,29)
    RETURN
    If(dateAdded <= P3End && dateAdded >= P3Start, "P3",
     "NA")

    FORMAT turns dates into text and that is not a good way to compare dates.

2 Replies

  • Greg_Deckler's avatar
    Greg_Deckler
    Community Champion

    Jkrupski Try:

    columnTest =
    VAR dateAdded = [column]
    VAR P3Start = DATE(2025,3,29)
    VAR P3End = DATE(2025,3,29)
    RETURN
    If(dateAdded <= P3End && dateAdded >= P3Start, "P3",
     "NA")

    FORMAT turns dates into text and that is not a good way to compare dates.

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Jkrupski ,

     

    Thanks for the reply from Greg_Deckler .

     

    There is something wrong with your start date and end date, they are both March 29, 2025, and P3 is returned only when the date is equal to March 29, 2025, and NA is returned for other dates.

     

    Modify your syntax, something like the code below:

    columnTest = 
    VAR dateAdd = 'Table'[column]
    VAR P3Start = DATE(2025, 3, 4)
    VAR P3End   = DATE(2025, 3, 29)
    RETURN
        IF(
             dateAdd>= P3Start && dateAdd <= P3End,
            "P3",
            "NA"
        )

     

    The final visual effect is shown below:

     

    The pbix file is attached.

     

    If you have any other questions please feel free to contact me.

     

    Best Regards,
    Yang
    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!