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
sestes
Regular Visitor

Yes/No Calculated Column for Last Month

I have the following code to create a calculated column returning "Yes" if the selected date was last month and "No" for everything else.  This was working fine until this month due to the year change.  I know what the problem is, but I'm not sure how to correct it.

 

Here is the custom column code;

 

if (Date.Year([RequestReceived]) = Date.Year(DateTime.LocalNow()) and Date.Month([RequestReceived]) = Date.Month(DateTime.LocalNow())-1) then "Yes" else "No"

 

It's clear that the problem is that the code is looking to see if the year matches then looking to see if the month is the current month minus one.  This was needed so the formula was taking into account both year and month.  Of course, last month's year will not match, so I'm not getting the correct result from the formula.  I expect that in February this code will work fine again, but every January it will not.  How can I revise this, or better calculate if the date in the selected field was last month?

 

Thanks,

Shawn

1 ACCEPTED SOLUTION
Vvelarde
Community Champion
Community Champion

@sestes

 

Hi, In Pquery yo can create a Custom Column with this code.

 

=if Date.IsInPreviousNMonths([Request_Recieved], 1)=true then "Yes" else "No"

 

Regards

 




Lima - Peru

View solution in original post

3 REPLIES 3
sestes
Regular Visitor

Thank you both for the quick reply.  @Vvelarde This was exactly what I needed and works like a charm.

Vvelarde
Community Champion
Community Champion

@sestes

 

Hi, In Pquery yo can create a Custom Column with this code.

 

=if Date.IsInPreviousNMonths([Request_Recieved], 1)=true then "Yes" else "No"

 

Regards

 




Lima - Peru
Greg_Deckler
Super User
Super User

Seems like you could nest your if statements such that you first check if the current month is 1 and if so then see if the month for the date is 12 and if so Yes, otherwise, your current calculation.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

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.