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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
mkai
Regular Visitor

Correct Power Query Syntax to reflect month and year?

I'm new to Power BI and Power Query and having a hard time finding the correct way to write the syntax for a simple if then statement using month and year. In my if function, one of the parameters I need is today's date to equal a certain month and year. 

 

What I wrote (incorrect): 

 

if Date.From(DateTime.LocalNow()) = Date.From(2021, 02) and ..... then "1" else "0"

 

Where am I going wrong? Thanks in advance to anyone who helps!

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

You are close.  Use Date.Month and Date.Year  for the comparisons.

 

if Date.Month(a)=Date.Month(b) and Date.Year(a)=Date.Year(b) then x else y

View solution in original post

3 REPLIES 3
lbendlin
Super User
Super User

if Date.Year([Actual Finish]) <= 2021 and Date.Month([Actual Finish]) <= 1 then "1" else "0"

lbendlin
Super User
Super User

You are close.  Use Date.Month and Date.Year  for the comparisons.

 

if Date.Month(a)=Date.Month(b) and Date.Year(a)=Date.Year(b) then x else y

Thank you! I think I'm entering the dates wrong. I've looked at the syntax for M Language on Microsoft's site and tried to replicate it, along with trying different formats and it's not working correctly. I also ensured that the Actual Finish Date column is formatted to be Date.Time. 

 

if [Actual Finish] <= Date.Year(2021) and [Actual Finish] <= Date.Month(01) then "1" else "0"

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors