cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
ansa_naz
Continued Contributor
Continued Contributor

Keep getting "Expression.Error: We cannot apply operator < to types Date and DateTime" - please help

I have the following M code in Query Editor from power BI:

 

let
    Source = Sql.Database("DB", "DB", [CommandTimeout=#duration(0, 1, 0, 0)]),
    dbo_Calls = Source{[Schema="dbo",Item="Calls"]}[Data],
    #"Changed Type" = Table.TransformColumnTypes(dbo_Calls,{{"Completion Date", type date}}),
    EndDate = Date.From(DateTime.FixedLocalNow()),
    StartDate = Date.AddDays(Date.AddMonths(EndDate,-6),1),
    #"Filtered Rows" = Table.SelectRows(dbo_Calls,each [Completion Date]>=StartDate 
    and [Completion Date]<=EndDate)

in
    #"Filtered Rows"

However this results in the below error:

 

Expression.Error: We cannot apply operator < to types Date and DateTime.
Details:
    Operator=<
    Left=26/12/2018
    Right=16/05/2017 00:00:00

Any ideas how I can get around this?

 

Many thanks for all help

3 ACCEPTED SOLUTIONS
Gordonlilj
Solution Sage
Solution Sage

Hi,

 

You could either change the datetime column to date or the other way around.

 

Or you could try adding DateTime.From() to the date columns.

I don't know which one is the datetime columns so i added the function it to all of them.

#"Filtered Rows" = Table.SelectRows(dbo_ANSAPBICalls,each DateTime.From([Completion Date])>=DateTime.From(StartDate) 
    and DateTime.From([Completion Date])<=DateTime.From(EndDate))

View solution in original post

tex628
Super User
Super User

You are comparing Date format with Datetime format. Change the format from datetime to date on your start and enddate before the filterstatement and it should work fine!

/J


Connect on LinkedIn

View solution in original post

ansa_naz
Continued Contributor
Continued Contributor

Its fixed now. I changed all Date functions to DateTime. I also amended the Filtered Rows step so it was using SelectRows from #ChangedType step, not from the original data source:

 

let
    Source = Sql.Database("DB", "DB", [CommandTimeout=#duration(0, 1, 0, 0)]),
    dbo_Calls = Source{[Schema="dbo",Item="Calls"]}[Data],
    #"Changed Type" = Table.TransformColumnTypes(dbo_Calls,{{"Completion Date", type date}}),
    EndDate = Date.From(DateTime.FixedLocalNow()),
    StartDate = Date.AddDays(Date.AddMonths(EndDate,-6),1),
    #"Filtered Rows" = Table.SelectRows(#"Changed Type",each DateTime.From([Completion Date])>=DateTime.From(StartDate) 
    and DateTime.From([Completion Date])<=DateTime.From(EndDate))
in
    #"Filtered Rows"

Thats done the trick, cheers for all the help!

 

View solution in original post

6 REPLIES 6
tex628
Super User
Super User

You are comparing Date format with Datetime format. Change the format from datetime to date on your start and enddate before the filterstatement and it should work fine!

/J


Connect on LinkedIn
ansa_naz
Continued Contributor
Continued Contributor

Thank you both

I have amended my M code to below:

 

EndDate = Date.From(Date.FixedLocalNow()),
StartDate = Date.AddDays(Date.AddMonths(EndDate,-6),1),
#"Filtered Rows" = Table.SelectRows(dbo_Calls,each Date.From([Completion Date])>=Date.From(StartDate) 
and Date.From([Completion Date])<=Date.From(EndDate))

However I now get below error:

 

Expression.Error: The name 'Date.FixedLocalNow' wasn't recognized.  Make sure it's spelled correctly.

Am I doing something daft here??

 

 

There is no function called Date.FixedLocalNow() which is why you get an error. DateTime.FixedLocalNow() is the name of that function.

I cant really tell whats wrong, can you post the complete query code?


Connect on LinkedIn
ansa_naz
Continued Contributor
Continued Contributor

Its fixed now. I changed all Date functions to DateTime. I also amended the Filtered Rows step so it was using SelectRows from #ChangedType step, not from the original data source:

 

let
    Source = Sql.Database("DB", "DB", [CommandTimeout=#duration(0, 1, 0, 0)]),
    dbo_Calls = Source{[Schema="dbo",Item="Calls"]}[Data],
    #"Changed Type" = Table.TransformColumnTypes(dbo_Calls,{{"Completion Date", type date}}),
    EndDate = Date.From(DateTime.FixedLocalNow()),
    StartDate = Date.AddDays(Date.AddMonths(EndDate,-6),1),
    #"Filtered Rows" = Table.SelectRows(#"Changed Type",each DateTime.From([Completion Date])>=DateTime.From(StartDate) 
    and DateTime.From([Completion Date])<=DateTime.From(EndDate))
in
    #"Filtered Rows"

Thats done the trick, cheers for all the help!

 

Gordonlilj
Solution Sage
Solution Sage

Hi,

 

You could either change the datetime column to date or the other way around.

 

Or you could try adding DateTime.From() to the date columns.

I don't know which one is the datetime columns so i added the function it to all of them.

#"Filtered Rows" = Table.SelectRows(dbo_ANSAPBICalls,each DateTime.From([Completion Date])>=DateTime.From(StartDate) 
    and DateTime.From([Completion Date])<=DateTime.From(EndDate))

Helpful resources

Announcements
Power Platform Conf 2022 768x460.jpg

Join us for Microsoft Power Platform Conference

The first Microsoft-sponsored Power Platform Conference is coming in September. 100+ speakers, 150+ sessions, and what's new and next for Power Platform.

Power BI Dev Camp Session 23 768x460.jpg

Check it Out!

Mark your calendars and join us on Thursday, June 30 at 11a PDT for a great session with Ted Pattison!

June 20 episode 7 with aka link 768x460.jpg

The Power BI Community Show

Join us on June 20 at 11 am PDT when Kim Manis shares the latest on Azure Synapse analytics, the Microsoft Intelligent Data Platform, and notable Power BI Updates from Microsoft Build 2022.

Top Solution Authors
Top Kudoed Authors