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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

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
Sept PBI Carousel

Power BI Monthly Update - September 2024

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

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

Sept NL Carousel

Fabric Community Update - September 2024

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