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
beepboop
Frequent Visitor

Using PREVIOUSMONTH function to calculate previous month sales, able to filter the dates column

So looking through the syntax of PREVIOUSMONTH(dates) function, you would need to specify a dates column and it would return the previous month based on the first value of the dates column. Problem is, I have a "Year" slicer which selects current year, 2023, 2022, etc... and if I filter any of the years, the previousmonth function will return the number of sales in December of the prior year.

 

That is because the dates column is returning values from Jan 1-December 31 of the selected year, and the function will always base it on January 1 as that is the first value of the column and thus calculate December numbers.

 

Is there a way I can apply a filter on the previousmonth function, or somewhere in my measure?

 

I am using the USERELATIONSHIP function that links the sale date column with my date calendar.

 

We are in February right now, and the measure is still returning December 2023 numbers. If only I can apply a filter on the column that is being used in the previousmonth function, such as only start the first value from today's date? Is there a way to do this as I've used datesbetween and filter functions, and it does not return the correct values

1 ACCEPTED SOLUTION
v-zhengdxu-msft
Community Support
Community Support

Hi @beepboop 

 

Please try this:

First of all, I create a set of sample data:

vzhengdxumsft_0-1707200576232.png

Then create a measure:

Measure = 
	IF(
		MONTH(TODAY()) - 1 = 0,
		CALCULATE(
			SUM('Table'[value]),
			FILTER(
				ALLSELECTED('Table'),
				MONTH('Table'[date]) = 12 && YEAR('Table'[date]) = YEAR(TODAY() - 1)
			)
		),
		CALCULATE(
			SUM('Table'[value]),
			FILTER(
				ALLSELECTED('Table'),
				MONTH('Table'[date]) = MONTH(TODAY()) - 1 && YEAR('Table'[date]) = YEAR(TODAY())
			)
		)
	)

The result is as follow:

vzhengdxumsft_1-1707200629011.png

Best Regards,

Zhengdong Xu

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

View solution in original post

4 REPLIES 4
v-zhengdxu-msft
Community Support
Community Support

Hi @beepboop 

 

Please try this:

First of all, I create a set of sample data:

vzhengdxumsft_0-1707200576232.png

Then create a measure:

Measure = 
	IF(
		MONTH(TODAY()) - 1 = 0,
		CALCULATE(
			SUM('Table'[value]),
			FILTER(
				ALLSELECTED('Table'),
				MONTH('Table'[date]) = 12 && YEAR('Table'[date]) = YEAR(TODAY() - 1)
			)
		),
		CALCULATE(
			SUM('Table'[value]),
			FILTER(
				ALLSELECTED('Table'),
				MONTH('Table'[date]) = MONTH(TODAY()) - 1 && YEAR('Table'[date]) = YEAR(TODAY())
			)
		)
	)

The result is as follow:

vzhengdxumsft_1-1707200629011.png

Best Regards,

Zhengdong Xu

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

Is there a way to return only one calculate expression and do an IF expression within the dates itself? Such as CALCULATE(SUM(TABLE)), IF(MONTH()=....,FILTER(DATE)..,FILTER))

Daniel29195
Super User
Super User

@beepboop 

Sorry, i didnt quite understand what is the requirement,

if you are in feb 2023, what do you want your measure to return ? 

 

 

danextian
Super User
Super User

Hi @beepboop ,

 

As always, please share a usable sample data that fully encompasses your use case (not an image) and  your expected result from that data. You may also share a pbix with confidential info removed. You can post a link to Google Drive, OneDrive or DropBox.










Did I answer your question? Mark my post as a solution!


Proud to be a Super User!









"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

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.