Join us for an expert-led overview of the tools and concepts you'll need to pass exam PL-300. The first session starts on June 11th. See you there!
Get registeredPower BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.
I am looking to calculate the month-to-date (MTD) and year-to-date (YTD) returns of some stocks.
I have the historical stock price and the corresponding dates, and here are my DAX code for computing the Price of last year end.
Price Last Year =
var
dt = CALCULATE(
MAXX(Price, ENDOFMONTH(DATEADD('Price'[Date], -MONTH('Price'[Date]), MONTH))),
allexcept('Price', 'Price'[Ticker])
)
RETURN
CALCULATE(
MAX('Price'[Price]),
'Price'[Date] = dt
)
It does not work for every stock. For stock like SWX:ROG (Roche), who does not have a price on the 12/31/2024 (In fact, its last trade day was 12/30/2024), my DAX code will fail. I will like my date calculation is wrong and it is not specific to a particular stock. Can you shed me some light how can I correct this please?
Separately, I did the calculation using three measure [Date of Last Year] --> [End Price of Last Year] --> [YTD Return] and they iterate over each stock so the process is slow. Is there more efficient way of doing so? Thanks in advance.
My Power BI File:
https://limewire.com/d/VTO8d#A0AMZXrGgC
Solved! Go to Solution.
How about this where you take the last date that does have a price?
Price Last Year =
VAR _CurrentDate = MAX ( 'Price'[Date] )
VAR _PrevYearEnd = EOMONTH ( _CurrentDate, -MONTH ( _CurrentDate ) )
VAR _LastDate =
CALCULATE(
MAX ( 'Price'[Date] ),
ALL ( 'Price'[Date] ),
'Price'[Date] <= _PrevYearEnd,
KEEPFILTERS ( NOT ISBLANK ( 'Price'[Price] ) )
)
RETURN
CALCULATE(
SELECTEDVALUE ( 'Price'[Price] ),
'Price'[Date] = _LastDate
)
Hi @kc_1116 ,
@AlexisOlson ’s solution is solid and handles the main issue around missing price values on year-end dates. If you’re looking for even more robustness or performance (especially with larger datasets), here are a couple of additional suggestions you could try:
If you want an example of building such a summary table or a reusable measure, let me know—I’d be happy to share a step-by-step!
Hope this helps you get even more out of your model!
Hi @kc_1116 ,
@AlexisOlson ’s solution is solid and handles the main issue around missing price values on year-end dates. If you’re looking for even more robustness or performance (especially with larger datasets), here are a couple of additional suggestions you could try:
If you want an example of building such a summary table or a reusable measure, let me know—I’d be happy to share a step-by-step!
Hope this helps you get even more out of your model!
How about this where you take the last date that does have a price?
Price Last Year =
VAR _CurrentDate = MAX ( 'Price'[Date] )
VAR _PrevYearEnd = EOMONTH ( _CurrentDate, -MONTH ( _CurrentDate ) )
VAR _LastDate =
CALCULATE(
MAX ( 'Price'[Date] ),
ALL ( 'Price'[Date] ),
'Price'[Date] <= _PrevYearEnd,
KEEPFILTERS ( NOT ISBLANK ( 'Price'[Price] ) )
)
RETURN
CALCULATE(
SELECTEDVALUE ( 'Price'[Price] ),
'Price'[Date] = _LastDate
)
User | Count |
---|---|
16 | |
15 | |
14 | |
12 | |
11 |
User | Count |
---|---|
19 | |
15 | |
14 | |
11 | |
9 |