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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
StephenF
Responsive Resident
Responsive Resident

Power query M - how to define "this year" and "last year"

Hi,

 

I'm making a custom column and what it populated with 2020 and another with 2019.

 

How do I define this year and last year in this language?

1 ACCEPTED SOLUTION

I'm going to go with

 

if Date.Year(DateTime.LocalNow()) = ( [InvPayment_Due_Year] ) or Date.Year(DateTime.LocalNow()) = ( [InvPayment_Due_Year]+1 ) then 1 else 0

View solution in original post

8 REPLIES 8
Rickmaurinus
Helper V
Helper V

You could go about this in several ways. Either you can retrieve the current datetime value, extract the year of it and then use it to filter your dates: 

 

if Date.Year( [DateColumn] ) = Date.Year( DateTime.LocalNow() ) then "Current Year" else if Date.Year( [DateColumn] ) = Date.Year( DateTime.LocalNow() ) - 1 then "Prior Year" else "Other periods " 

 On the other hand, if you want to make use of Power Query's build in date information functions, you can use: 

if Date.IsInCurrentYear( [DateColumn] ) then "Current Year" else if Date.IsInPreviousYear( [DateColumn] ) then "Prior Year" else "Other"

For some clear screenshots of the cutoff dates, check out: 

https://powerquery.how/date-isinpreviousyear/

https://powerquery.how/date-isincurrentyear/

 

--------------------------------------------------

@ me in replies or I'll lose your thread

 

Master Power Query M? -> https://powerquery.how

Read in-depth articles? -> BI Gorilla

Youtube Channel: BI Gorilla

 

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

bakerm00
Regular Visitor

since i've gone thru the pain of learning this today. 

determines if an end date is in this year or next (for a gantt visual filter)

= Table.AddColumn(#"convert progress to percent", "ganttyearfilter", each if([Forecast End Date] is null) then "0" else if(Date.IsInCurrentYear([Forecast End Date])) then "1" else if(Date.IsInNextYear([Forecast End Date]))then "2" else "99")

littlemojopuppy
Community Champion
Community Champion

Question: why define separate columns for this in Power Query when you can easily accomplish the same in DAX?

But if you have to, there are M functions to accomplish this...

Because I had this at the report level. I want it at the data source "edit query" level so I dont need to write a new formula for every report the data source references.

 

I've searched for half an hour on google and here and found nothing. The M documentation is not so good.

Are you trying to accomplish a year over year comparison?

No, Im creating a filter to limit data to this year and last year.

Oversimplifying, but you could simply have columns for year and amount and filter for Date.IsInPreviousYear or Date.IsInCurrentYear.  Having amounts in different fields could make measure writing later on more complicated than it needs to be

I'm going to go with

 

if Date.Year(DateTime.LocalNow()) = ( [InvPayment_Due_Year] ) or Date.Year(DateTime.LocalNow()) = ( [InvPayment_Due_Year]+1 ) then 1 else 0

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Prices go up Feb. 11th.

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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