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

St. Deviation of Portfolio Returns

Hi everyone,

I have a table with some portfolios and their daily values. Negative values mean they are short some assets.

My ultimate goal is to get a standard deviation of the combined daily returns of selected portfolios. So if I select Portfolio 1 and Portfolio 2, I want a st. deviation of the combined returns of the two.

I only managed to get a st. deviation of the Value column. But it's not what I need. I'm struggling to find a solution to my problem.


The returns = (ValueToday - ValueYesterday) / ValueYesterday

 

The pbix file is saved here

Will appreciate your help.
Thanks very much.

badger007_0-1663334103894.png

 

badger007_1-1663334145285.png

 

1 ACCEPTED SOLUTION

Thank you for providing the sample data. That helps a lot with proposing a potential solution.


Small comment: your "Day over Day"  measure includes transition from negative to positive values. In that scenario it is advisable to adjust the formula to use the absolute of the prior value.

 

 

m_Daily_Returns = 
VAR thisvalue = 
    MAX ( 'Table'[value] )
VAR thisdate =
    MAX ( 'Table'[Date] )
VAR prevvalue =
    CALCULATE (
        LASTNONBLANKVALUE (
            'Table'[Date],
            MAX ( 'table'[value] )
        ),
        FILTER (
            ALL ( 'Table'[Date] ),
            'table'[Date] < thisdate
        )
    )
RETURN
    DIVIDE (
        (thisvalue - prevvalue),
        abs(prevvalue)
    ) 

 

 

Next you need to rethink your entire approach.  For example 

lbendlin_0-1664286441622.png

You have gaps in your dates (weekends, I assume) that you will need to take into account (or not - but you need to decide). You can also see that the row totals are incorrect. They need to be calculated separately for each portfolio based not on the sum, not on the maximum, but on the last value.

 

 

Pval = 
var a = values(Portfolios[Portfolio])
var b = ADDCOLUMNS(a,"md",var p=[Portfolio] return CALCULATE(max('Table'[Date]),'Table'[Portfolio]=p))
var c = ADDCOLUMNS(b,"mv",var md=[md] var p =[Portfolio] return CALCULATE(sum('Table'[Value]),'Table'[Date]=md,'Table'[Portfolio]=p))
return sumx(c,[mv])

 

 

lbendlin_1-1664287280375.png

 

 

The same is then true for the daily returns and the standard deviation. See attached.

View solution in original post

4 REPLIES 4
lbendlin
Super User
Super User

In your measure get a list of all dates in the current filter context, then compute the return for each, and finally run the standard deviation function over that table.

 

Please provide sanitized sample data that fully covers your issue.
https://community.powerbi.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-Forum/ba-...
Please show the expected outcome based on the sample data you provided.

https://community.powerbi.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447523

Hi lbendlin,

Thanks for your reply. The pbix file is saved here.

It'll be so great if you can help me with this.

Thanks!

Thank you for providing the sample data. That helps a lot with proposing a potential solution.


Small comment: your "Day over Day"  measure includes transition from negative to positive values. In that scenario it is advisable to adjust the formula to use the absolute of the prior value.

 

 

m_Daily_Returns = 
VAR thisvalue = 
    MAX ( 'Table'[value] )
VAR thisdate =
    MAX ( 'Table'[Date] )
VAR prevvalue =
    CALCULATE (
        LASTNONBLANKVALUE (
            'Table'[Date],
            MAX ( 'table'[value] )
        ),
        FILTER (
            ALL ( 'Table'[Date] ),
            'table'[Date] < thisdate
        )
    )
RETURN
    DIVIDE (
        (thisvalue - prevvalue),
        abs(prevvalue)
    ) 

 

 

Next you need to rethink your entire approach.  For example 

lbendlin_0-1664286441622.png

You have gaps in your dates (weekends, I assume) that you will need to take into account (or not - but you need to decide). You can also see that the row totals are incorrect. They need to be calculated separately for each portfolio based not on the sum, not on the maximum, but on the last value.

 

 

Pval = 
var a = values(Portfolios[Portfolio])
var b = ADDCOLUMNS(a,"md",var p=[Portfolio] return CALCULATE(max('Table'[Date]),'Table'[Portfolio]=p))
var c = ADDCOLUMNS(b,"mv",var md=[md] var p =[Portfolio] return CALCULATE(sum('Table'[Value]),'Table'[Date]=md,'Table'[Portfolio]=p))
return sumx(c,[mv])

 

 

lbendlin_1-1664287280375.png

 

 

The same is then true for the daily returns and the standard deviation. See attached.

Thanks very much for your help!

This solved my problem.

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.