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 monthly difference from the latest date with value

Hello, 

 

I'm looking to write a measure that would calculate the difference between the latest price of an item and its price in the previous month. My dataset has different latest dates for each item, so I was using LASTDATE and DATESBETWEEN to get the latest price. However, I am not sure how to get the previous month's price. 

 

 

Latest Price = 
VAR _LastDate = LASTDATE(Products[Date])
RETURN

CALCULATE(SUM(Products[Price]), DATESBETWEEN(Calendar[Date], _LastDate, _LastDate))

 

 

In the example below, the Latest Price measure returns $4 for Bananas, and $7 for Apples. 

 

The price difference I am looking for would be:

  • Bananas: 4-6 = -2 (March - Feb)
  • Apples: 7-4 = 3 (April - March). 

 

ItemDatePrice
BananasJan 20205
BananasFeb 20206
BananasMarch 20204
ApplesJan 20202
ApplesFeb 20205
ApplesMarch 20204
ApplesApril 20207

 

I also need the percentage difference but that should be straightforward once I figure this out. Thank you in advance!

 

Best, 

Alex

1 ACCEPTED SOLUTION
mahoneypat
Employee
Employee

Please try these expressions:

 

Latest Price =
VAR __latestdate =
MAX ( 'Table'[Date] )
RETURN
CALCULATE ( SUM ( 'Table'[Price] ), 'Table'[Date] = __latestdate )

 

Prev Price =
VAR __latestdate =
MAX ( 'Table'[Date] )
VAR __prevdate =
CALCULATE (
MAX ( 'Table'[Date] ),
FILTER ( ALL ( 'Table'[Date] ), 'Table'[Date] < __latestdate )
)
RETURN
CALCULATE ( SUM ( 'Table'[Price] ), 'Table'[Date] = __prevdate )

 

If this works for you, please mark it as solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

5 REPLIES 5
Anonymous
Not applicable

Thank you everyone for your help! 😊

mahoneypat
Employee
Employee

Please try these expressions:

 

Latest Price =
VAR __latestdate =
MAX ( 'Table'[Date] )
RETURN
CALCULATE ( SUM ( 'Table'[Price] ), 'Table'[Date] = __latestdate )

 

Prev Price =
VAR __latestdate =
MAX ( 'Table'[Date] )
VAR __prevdate =
CALCULATE (
MAX ( 'Table'[Date] ),
FILTER ( ALL ( 'Table'[Date] ), 'Table'[Date] < __latestdate )
)
RETURN
CALCULATE ( SUM ( 'Table'[Price] ), 'Table'[Date] = __prevdate )

 

If this works for you, please mark it as solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


@mahoneypat 

 

What if you had a row with April 2 in it! this formula VAR __latestdate doen't not show the last date of the previous month, it shows the row of April 1.

That's why i want to ask @Anonymous if in his Table there is only one row for evry 1st of the month or many dates and if he is using a Calendar date in order to use Time Intelligent DAX formulas accuratly!

 

Thanks

Regards
Amine Jerbi

If I answered your question, please mark this thread as accepted
and you can follow me on
My Website, LinkedIn and Facebook

You are correct about an April 2 values.  From the example data that seemed to not be the case, but it would be easy to adapt that approach use current and prev month if needed.

Regards,

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


TomMartens
Super User
Super User

Hey @Anonymous ,

 

I assume you have a measure that returns the last price (the price of the latest date), maybe this measure looks like this:

last Amount = 
LASTNONBLANKVALUE('Calendar'[Date] , [Amount (explicit measure)]) 

To find the latest price for the previous month you can restrict the avaiable days to the previous months like so:

last amount previous month = 
var maxDate = CALCULATE(MAX('Calendar'[Date]))
var tDatesPreviousMonth = 
	DATESBETWEEN(
		'Calendar'[Date] 
		, EOMONTH(maxDate , -2) + 1 
		, EOMONTH(maxDate , -1)
		)
return
CALCULATE(
	[last Amount]
	, tDatesPreviousMonth 
	)

Hopefully, this provides some new ideas on how to tackle your challenge.

 

Regards,

Tom



Did I answer your question? Mark my post as a solution, this will help others!

Proud to be a Super User!
I accept Kudos 😉
Hamburg, Germany

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.