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

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

Reply
Anonymous
Not applicable

MCode Date Syntax - Error Message

I'm getting an error, but I dont know how to fix it. The error is coming from the "IfStatement" row.

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

 

I'm trying to say, If Month >= 8 then use 8/1/ current year, else use 8/1/ year - 1

 

 

let
    Source = Sql.Database(".net", "database", [Query="select *  from [dbo].[TableImUsing]  where substring([TableColumnName], 1, 3) = 'XS ' and [Status] = 'Active' and [OtherStatus] in ('Pun')"]),
    #"Added Prefix" = Table.TransformColumns(Source, {{"Column2", each "XS" & _, type text}}),
    #"Changed Type" = Table.TransformColumnTypes(#"Added Prefix",{{"DAte2", type date}, {"Date1", type date}}),
    #"IfStatement"  = Table.SelectRows(#"Changed Type", if DateTime.Month(DateTime.LocalNow()) >= 8 then [Date1] > DateTime.Date(Date.Year(DateTime.LocalNow()), 8, 1) else [Date1] > DateTime.Date(DateTime.Year(Date.LocalNow())-1, 8, 1))
in
    #"IfStatement"

 

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

I ended up breaking this out into several lines in order to simplify what I'm trying to get to.

 

This is what worked:

 

    #"Added Custom" = Table.AddColumn(#"Changed Type", "Current Month Number", each Date.Month(DateTime.LocalNow())),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Current Year", each #date(Date.Year(DateTime.LocalNow()), 8, 1)),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Prior Year", each #date(Date.Year(DateTime.LocalNow())-1, 8, 1)),
    #"Added Conditional Column" = Table.AddColumn(#"Added Custom2", "FilterDate", each if [Current Month Number] >= 8 then [Current Year] else [Prior Year]),
    #"Filtered Rows" = Table.SelectRows(#"Added Conditional Column", each [Date1] >= [FilterDate])
in
    #"Filtered Rows"

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

I ended up breaking this out into several lines in order to simplify what I'm trying to get to.

 

This is what worked:

 

    #"Added Custom" = Table.AddColumn(#"Changed Type", "Current Month Number", each Date.Month(DateTime.LocalNow())),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Current Year", each #date(Date.Year(DateTime.LocalNow()), 8, 1)),
    #"Added Custom2" = Table.AddColumn(#"Added Custom1", "Prior Year", each #date(Date.Year(DateTime.LocalNow())-1, 8, 1)),
    #"Added Conditional Column" = Table.AddColumn(#"Added Custom2", "FilterDate", each if [Current Month Number] >= 8 then [Current Year] else [Prior Year]),
    #"Filtered Rows" = Table.SelectRows(#"Added Conditional Column", each [Date1] >= [FilterDate])
in
    #"Filtered Rows"
Anonymous
Not applicable

@amitchandak 

 

I'm still getting an error, although now its different:

 

Expression.Error: There is an unknown identifier. Did you use the [field] shorthand for a _[field] outside of an 'each' expression?

 

 

let
    Source = Sql.Database(".net", "database", [Query="select *  from [dbo].[myTable]  where substring([Campaign Name], 1, 3) = 'XS ' and [Status] = 'Active' and [OtherStatus] in ('Pun')"]),
    #"Added Prefix" = Table.TransformColumns(Source, {{"ColumnA", each "XS" & _, type text}}),
    #"Changed Type" = Table.TransformColumnTypes(#"Added Prefix",{{"Date2", type date}, {"Date1", type date}}),
    #"IfStatement"  = Table.SelectRows(#"Changed Type", if Date.Month( DateTime.Date( DateTime.LocalNow() )) >= 8 then [Date1] > DateTime.Date( Date.Year( DateTime.Date( DateTime.LocalNow())), 8, 1) else [Date1] > DateTime.Date(                                                                             Date.Year(DateTime.Date(DateTime.LocalNow()))-1, 8, 1))
in
    #"IfStatement"

 

 

amitchandak
Super User
Super User

@Anonymous , Actually Date has Month and year, Not datetime.

example

Date.Month(DateTime.Date(DateTime.LocalNow()))

 

correct others

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.