Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
sarahyyg2
Frequent Visitor

Cannot compare date to datetime, and cannot convert date/datetime in DAX

Hi,

I have created a date table with DAX, and all the date values are of date format. I also created columns to store the start of month and end of month dates.

 

In my fact table 'Tickets' there is a column called [dateReported], also in date format. I want to filter the [dateReported] column with a slicer which selects dates in "MMMM YY" formats. 

Screenshot 2024-11-20 212051.png

I wrote a measure to find the first day of the selected value of the slicer as follows:

First Day of Selected Month =
VAR selectedMonth = SELECTEDVALUE('Date'[Year Month Full])
RETURN CALCULATE(MAX('Date'[Month Start]), FILTER('Date', 'Date'[Year Month Full] = selectedMonth))

This does not work quite as expected as it returns in datetime format when shown in a card visual, which I cannot figure out why:
Screenshot 2024-11-20 212018.png

 

And I wrote another measure to compare [dateReported] to the [First Day of Selected Month] measure:

Items before Selected Month =
CALCULATE(
    COUNTROWS('Tickets'),
    FILTER(ALL('Tickets'), 'Tickets'[dateReported] < 'Tickets'[First Day of Selected Month])
)


However this gives me BLANK.

 

I have tried to compare [dateReported] to a date literal and it works as expected:

Items before Selected Month =
CALCULATE(
    COUNTROWS('Tickets'),
    FILTER(ALL('Tickets'), 'Tickets'[dateReported] < dt"2024-10-1")
)
Is there anyway to convert the date/datetime variables so that they can compare to each other?
1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi All,
Firslty  PhilipTreacy  thank you for your solution!

And @sarahyyg2 ,It's great to see your solution to your problem and we try to optimize your code:

 

Overdue before Selected Month = 
VAR selectedYear = SELECTEDVALUE('Date'[Year])
VAR selectedMonth = SELECTEDVALUE('Date'[Month])
VAR selectedDate = DATE(selectedYear, selectedMonth, 1) -- First day of the selected month
RETURN
    CALCULATE(
        COUNTROWS('Tickets'),
        FILTER(
            ALL('Tickets'),
            'Tickets'[dateReported] < selectedDate &&
            'Tickets'[overdue] = "Overdue" &&
            'Tickets'[status] IN {"New", "Open", "Pending"}
        )
    )

 

vxingshenmsft_0-1732170294007.png

If you have further questions, check out the pbix file I uploaded, I hope it helps and I'd be honored if I could solve your problem!

Hope it helps!

Best regards,
Community Support Team_ Tom Shen

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

View solution in original post

5 REPLIES 5
sarahyyg2
Frequent Visitor

@PhilipTreacy 

Thank you for the reply. I worked around it by using hierachy and now it's able to pass the year and month as integers separtely so I can compare. I wrote a DAX measure, and wondering if there is a better way of rewriting this?

 

Overdue before Selected Month = 
VAR selectedYear = SELECTEDVALUE('Date'[Year])
VAR selectedMonth = SELECTEDVALUE('Date'[Month])
VAR monthNumber = CALCULATE(MAX('Date'[Month Number]), FILTER('Date', 'Date'[Month] = selectedMonth))
RETURN CALCULATE(
    COUNTROWS('Tickets'),
    FILTER(ALL('Tickets'), CONVERT(YEAR('Tickets'[dateReported]), INTEGER) = CONVERT(selectedYear, INTEGER)),
    FILTER(ALL('Tickets'), CONVERT(MONTH('Tickets'[dateReported]),INTEGER) < CONVERT(monthNumber, INTEGER)),
    FILTER(ALL('Tickets'),'Tickets'[overdue] = "Overdue"),
    FILTER(ALL('Tickets'),'Tickets'[status] IN {"New", "Open", "Pending"})
)
+
CALCULATE(
    COUNTROWS('Tickets'),
    FILTER(ALL('Tickets'), CONVERT(YEAR('Tickets'[dateReported]), INTEGER) < CONVERT(selectedYear, INTEGER)),
    FILTER(ALL('Tickets'),'Tickets'[overdue] = "Overdue"),
    FILTER(ALL('Tickets'),'Tickets'[status] IN {"New", "Open", "Pending"})
)
+
0
 
 
Anonymous
Not applicable

Hi All,
Firslty  PhilipTreacy  thank you for your solution!

And @sarahyyg2 ,It's great to see your solution to your problem and we try to optimize your code:

 

Overdue before Selected Month = 
VAR selectedYear = SELECTEDVALUE('Date'[Year])
VAR selectedMonth = SELECTEDVALUE('Date'[Month])
VAR selectedDate = DATE(selectedYear, selectedMonth, 1) -- First day of the selected month
RETURN
    CALCULATE(
        COUNTROWS('Tickets'),
        FILTER(
            ALL('Tickets'),
            'Tickets'[dateReported] < selectedDate &&
            'Tickets'[overdue] = "Overdue" &&
            'Tickets'[status] IN {"New", "Open", "Pending"}
        )
    )

 

vxingshenmsft_0-1732170294007.png

If you have further questions, check out the pbix file I uploaded, I hope it helps and I'd be honored if I could solve your problem!

Hope it helps!

Best regards,
Community Support Team_ Tom Shen

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

PhilipTreacy
Super User
Super User

@sarahyyg2 

 

Can you provide your file?  It'll be much easier to debug with the data and see what it is you are working with.

 

Regards

 

Phil

 



Did I answer your question? Then please mark my post as the solution.
If I helped you, click on the Thumbs Up to give Kudos.


Blog :: YouTube Channel :: Connect on Linkedin


Proud to be a Super User!


Thank you for the reply. I have worked around it by using hierachy to pass the number of year and month as integers to compare. The DAX I wrote for the measure is as below, I wonder if there is a better way to rewrite it?

Overdue before Selected Month = 
VAR selectedYear = SELECTEDVALUE('Date'[Year])
VAR selectedMonth = SELECTEDVALUE('Date'[Month])
VAR monthNumber = CALCULATE(MAX('Date'[Month Number]), FILTER('Date', 'Date'[Month] = selectedMonth))
RETURN CALCULATE(
    COUNTROWS('Tickets'),
    FILTER(ALL('Tickets'), CONVERT(YEAR('Tickets'[dateReported]), INTEGER) = CONVERT(selectedYear, INTEGER)),
    FILTER(ALL('Tickets'), CONVERT(MONTH('Tickets'[dateReported]),INTEGER) < CONVERT(monthNumber, INTEGER)),
    FILTER(ALL('Tickets'),'Tickets'[overdue] = "Overdue"),
    FILTER(ALL('Tickets'),'Tickets'[status] IN {"New", "Open", "Pending"})
)
+ CALCULATE(
    COUNTROWS('Tickets'),
    FILTER(ALL('Tickets'), CONVERT(YEAR('Tickets'[dateReported]), INTEGER) < CONVERT(selectedYear, INTEGER)),
    FILTER(ALL('Tickets'),'Tickets'[overdue] = "Overdue"),
    FILTER(ALL('Tickets'),'Tickets'[status] IN {"New", "Open", "Pending"})
)
+
0

 

Thanks for the reply.

 

I have figured it out by using hierachy and converting the year and month to integers separately. The DAX I wrote is as follows, but it has to evaluate the same filters twice I suppose. Is there a better way to rewrite this?

 

Overdue over One Month =
VAR selectedYear = SELECTEDVALUE('Date'[Year])
VAR selectedMonth = SELECTEDVALUE('Date'[Month])
VAR monthNumber = CALCULATE(MAX('Date'[Month Number]), FILTER('Date', 'Date'[Month] = selectedMonth))
RETURN CALCULATE(
    COUNTROWS('Tickets'),
    FILTER(ALL('Tickets'), CONVERT(YEAR('Tickets'[dateReported]), INTEGER) = CONVERT(selectedYear, INTEGER)),
    FILTER(ALL('Tickets'), CONVERT(MONTH('Tickets'[dateReported]),INTEGER) < CONVERT(monthNumber, INTEGER)),
    FILTER(ALL('Tickets'),'Tickets'[overdue] = "Overdue"),
    FILTER(ALL('Tickets'),'Tickets'[status] IN {"New", "Open", "Pending"})
) +
CALCULATE(
    COUNTROWS('Tickets'),
    FILTER(ALL('Tickets'), CONVERT(YEAR('Tickets'[dateReported]), INTEGER) < CONVERT(selectedYear, INTEGER)),
    FILTER(ALL('Tickets'),'Tickets'[overdue] = "Overdue"),
    FILTER(ALL('Tickets'),'Tickets'[status] IN {"New", "Open", "Pending"})
)

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.