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
Anonymous
Not applicable

Calculate previous month based on todays date

What is the easiest way to get previous month from today's date? I want to filter dynamic report to previous month. 

Query1= Date.Month

Query2=DateTime.LocalNow

Invoked function= (#"Query1"(Query2()))-1 gives me the value 2. But I am not able to attach it to the month.

2 ACCEPTED SOLUTIONS
Greg_Deckler
Super User
Super User

Not sure if it is the easiest way but you could do something like this:

 

=#date(Date.Year(DateTime.LocalNow()),Date.Month(DateTime.LocalNow()) - 1,Date.Day(DateTime.LocalNow()))

@ 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...

View solution in original post

v-kelly-msft
Community Support
Community Support

Hi @Anonymous ,

 

Do you just wanna get the month,if so ,you can use an expression as below:

 

Previous month=If(Month(Datetime.LocalNow())-1=0,12,Month(Datetime.LocalNow())-1)

 

If you wanna get the date,you can use following expressions:

 

Previous date=If(Month(DateTime.LocalNow())-1=0,Date(Year(DateTime.LocalNow()),12,Day(DateTime.LocalNow()),Date(Year(DateTime.LocalNow()),Month(DateTime.LocalNow()) - 1,Day(DateTime.LocalNow()))

 

Or you can create a calendar table and create a relationship between the 2 table,finally using an expression as below:

 

DATEADD ('Calendar'[Date], -1, month)

 

 

If today is for the real date,you can use today() instead of DateTime.LocalNow().

 

Best Regards,
Kelly
 
Did I answer your question? Mark my post as a solution!

View solution in original post

3 REPLIES 3
v-kelly-msft
Community Support
Community Support

Hi @Anonymous ,

 

Do you just wanna get the month,if so ,you can use an expression as below:

 

Previous month=If(Month(Datetime.LocalNow())-1=0,12,Month(Datetime.LocalNow())-1)

 

If you wanna get the date,you can use following expressions:

 

Previous date=If(Month(DateTime.LocalNow())-1=0,Date(Year(DateTime.LocalNow()),12,Day(DateTime.LocalNow()),Date(Year(DateTime.LocalNow()),Month(DateTime.LocalNow()) - 1,Day(DateTime.LocalNow()))

 

Or you can create a calendar table and create a relationship between the 2 table,finally using an expression as below:

 

DATEADD ('Calendar'[Date], -1, month)

 

 

If today is for the real date,you can use today() instead of DateTime.LocalNow().

 

Best Regards,
Kelly
 
Did I answer your question? Mark my post as a solution!
ali1234
Helper I
Helper I

DATEADD is the function to use for this. It does require a date format field for the date and is able to add or subtract day, month, quarter or year from a given value. Hope that helps

DATEADD (<dates>, <number_of_intervals>, <interval type>)

 

Greg_Deckler
Super User
Super User

Not sure if it is the easiest way but you could do something like this:

 

=#date(Date.Year(DateTime.LocalNow()),Date.Month(DateTime.LocalNow()) - 1,Date.Day(DateTime.LocalNow()))

@ 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.