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

The Power BI Data Visualization World Championships is back! It's time to submit your entry. Live 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
December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.