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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Anonymous
Not applicable

Correlation of returns over time but as calculated column

Column A = Date, Column B = returns on X stock, Column C = returns on Y stock. The quick measure in PowerBI desktop gives the correlation as a single scalar value taking the correlation between all rows of B and C. 

 

Is there a way to only look back 60 days from last row date as a calculated column to give me the 60 day historic correlation between X & Y on each row. This way I can chart change in 60 day correlation over time between X&Y. 

 

Thanks in advance for your help.

2 ACCEPTED SOLUTIONS
v-lid-msft
Community Support
Community Support

Hi @Anonymous ,

 

We can create a calculated column using following formula to meet your requirement:

 

60 Days =
VAR d = [Date]
VAR Correlation_Table =
    FILTER (
        FILTER (
            ADDCOLUMNS (
                GROUPBY ( 'Table', 'Table'[Date] ),
                "Value_X",
                VAR de = [Date]
                RETURN
                    CALCULATE ( SUM ( 'Table'[Value] ), FILTER ( 'Table', [Date] = de ) ),
                "Value_Y",
                VAR de = [Date]
                RETURN
                    CALCULATE ( SUM ( 'Table'[Value2] ), FILTER ( 'Table', [Date] = de ) )
            ),
            AND ( NOT ( ISBLANK ( [Value_X] ) ), NOT ( ISBLANK ( [Value_Y] ) ) )
        ),
        [Date] > d - 60
            && [Date] <= d
    )
VAR Count_Items =
    COUNTROWS ( Correlation_Table )
VAR Sum_X =
    SUMX ( Correlation_Table, [Value_X] )
VAR Sum_X2 =
    SUMX ( Correlation_Table, [Value_X] ^ 2 )
VAR Sum_Y =
    SUMX ( Correlation_Table, [Value_Y] )
VAR Sum_Y2 =
    SUMX ( Correlation_Table, [Value_Y] ^ 2 )
VAR Sum_XY =
    SUMX ( Correlation_Table, [Value_X] * [Value_Y] )
VAR Pearson_Numerator = Count_Items * Sum_XY - Sum_X * Sum_Y
VAR Pearson_Denominator_X = Count_Items * Sum_X2 - Sum_X ^ 2
VAR Pearson_Denominator_Y = Count_Items * Sum_Y2 - Sum_Y ^ 2
VAR Pearson_Denominator =
    SQRT ( Pearson_Denominator_X * Pearson_Denominator_Y )
RETURN
    DIVIDE ( Pearson_Numerator, Pearson_Denominator )

 

4.PNG


If it doesn't meet your requirement, Please show the exact expected result based on the Tables that we have shared.


Best regards,

 

Community Support Team _ Dong Li
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

Hi @Anonymous ,

 

The measure is dynamic but the calculated columns are computed during the database processing and then stored in the model. So In your scenario, we cannot make the calculated column dynamically.

 

But we can create a measure used in visual if the x-axis is date. Firstly, We create a What-If Parameter.

 

7.jpg

 

Then we can create a measure:

 

Correlation_Measure = 
VAR d = MAX('Table'[Date])
VAR Correlation_Table =
    FILTER (
        FILTER (
            ADDCOLUMNS (
                GROUPBY ( ALLSELECTED('Table'), 'Table'[Date] ),
                "Value_X",
                VAR de = [Date]
                RETURN
                    CALCULATE ( SUM ( 'Table'[Value] ), FILTER ( ALLSELECTED('Table'), [Date] = de ) ),
                "Value_Y",
                VAR de = [Date]
                RETURN
                    CALCULATE ( SUM ( 'Table'[Value2] ), FILTER ( ALLSELECTED('Table'), [Date] = de ) )
            ),
            AND ( NOT ( ISBLANK ( [Value_X] ) ), NOT ( ISBLANK ( [Value_Y] ) ) )
        ),
        [Date] > d - [PreviousDaySlicer Value]
            && [Date] <= d
    )
VAR Count_Items =
    COUNTROWS ( Correlation_Table )
VAR Sum_X =
    SUMX ( Correlation_Table, [Value_X] )
VAR Sum_X2 =
    SUMX ( Correlation_Table, [Value_X] ^ 2 )
VAR Sum_Y =
    SUMX ( Correlation_Table, [Value_Y] )
VAR Sum_Y2 =
    SUMX ( Correlation_Table, [Value_Y] ^ 2 )
VAR Sum_XY =
    SUMX ( Correlation_Table, [Value_X] * [Value_Y] )
VAR Pearson_Numerator = Count_Items * Sum_XY - Sum_X * Sum_Y
VAR Pearson_Denominator_X = Count_Items * Sum_X2 - Sum_X ^ 2
VAR Pearson_Denominator_Y = Count_Items * Sum_Y2 - Sum_Y ^ 2
VAR Pearson_Denominator =
    SQRT ( Pearson_Denominator_X * Pearson_Denominator_Y )
RETURN
    DIVIDE ( Pearson_Numerator, Pearson_Denominator )

 

8.jpg9.jpg10.jpg

 


Best regards,

 

Community Support Team _ Dong Li
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

3 REPLIES 3
v-lid-msft
Community Support
Community Support

Hi @Anonymous ,

 

We can create a calculated column using following formula to meet your requirement:

 

60 Days =
VAR d = [Date]
VAR Correlation_Table =
    FILTER (
        FILTER (
            ADDCOLUMNS (
                GROUPBY ( 'Table', 'Table'[Date] ),
                "Value_X",
                VAR de = [Date]
                RETURN
                    CALCULATE ( SUM ( 'Table'[Value] ), FILTER ( 'Table', [Date] = de ) ),
                "Value_Y",
                VAR de = [Date]
                RETURN
                    CALCULATE ( SUM ( 'Table'[Value2] ), FILTER ( 'Table', [Date] = de ) )
            ),
            AND ( NOT ( ISBLANK ( [Value_X] ) ), NOT ( ISBLANK ( [Value_Y] ) ) )
        ),
        [Date] > d - 60
            && [Date] <= d
    )
VAR Count_Items =
    COUNTROWS ( Correlation_Table )
VAR Sum_X =
    SUMX ( Correlation_Table, [Value_X] )
VAR Sum_X2 =
    SUMX ( Correlation_Table, [Value_X] ^ 2 )
VAR Sum_Y =
    SUMX ( Correlation_Table, [Value_Y] )
VAR Sum_Y2 =
    SUMX ( Correlation_Table, [Value_Y] ^ 2 )
VAR Sum_XY =
    SUMX ( Correlation_Table, [Value_X] * [Value_Y] )
VAR Pearson_Numerator = Count_Items * Sum_XY - Sum_X * Sum_Y
VAR Pearson_Denominator_X = Count_Items * Sum_X2 - Sum_X ^ 2
VAR Pearson_Denominator_Y = Count_Items * Sum_Y2 - Sum_Y ^ 2
VAR Pearson_Denominator =
    SQRT ( Pearson_Denominator_X * Pearson_Denominator_Y )
RETURN
    DIVIDE ( Pearson_Numerator, Pearson_Denominator )

 

4.PNG


If it doesn't meet your requirement, Please show the exact expected result based on the Tables that we have shared.


Best regards,

 

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
Anonymous
Not applicable

Perfect! This works. How do you make the 60 value dynamic to take in user values from a slicer e.g 20,30,60 ?

Hi @Anonymous ,

 

The measure is dynamic but the calculated columns are computed during the database processing and then stored in the model. So In your scenario, we cannot make the calculated column dynamically.

 

But we can create a measure used in visual if the x-axis is date. Firstly, We create a What-If Parameter.

 

7.jpg

 

Then we can create a measure:

 

Correlation_Measure = 
VAR d = MAX('Table'[Date])
VAR Correlation_Table =
    FILTER (
        FILTER (
            ADDCOLUMNS (
                GROUPBY ( ALLSELECTED('Table'), 'Table'[Date] ),
                "Value_X",
                VAR de = [Date]
                RETURN
                    CALCULATE ( SUM ( 'Table'[Value] ), FILTER ( ALLSELECTED('Table'), [Date] = de ) ),
                "Value_Y",
                VAR de = [Date]
                RETURN
                    CALCULATE ( SUM ( 'Table'[Value2] ), FILTER ( ALLSELECTED('Table'), [Date] = de ) )
            ),
            AND ( NOT ( ISBLANK ( [Value_X] ) ), NOT ( ISBLANK ( [Value_Y] ) ) )
        ),
        [Date] > d - [PreviousDaySlicer Value]
            && [Date] <= d
    )
VAR Count_Items =
    COUNTROWS ( Correlation_Table )
VAR Sum_X =
    SUMX ( Correlation_Table, [Value_X] )
VAR Sum_X2 =
    SUMX ( Correlation_Table, [Value_X] ^ 2 )
VAR Sum_Y =
    SUMX ( Correlation_Table, [Value_Y] )
VAR Sum_Y2 =
    SUMX ( Correlation_Table, [Value_Y] ^ 2 )
VAR Sum_XY =
    SUMX ( Correlation_Table, [Value_X] * [Value_Y] )
VAR Pearson_Numerator = Count_Items * Sum_XY - Sum_X * Sum_Y
VAR Pearson_Denominator_X = Count_Items * Sum_X2 - Sum_X ^ 2
VAR Pearson_Denominator_Y = Count_Items * Sum_Y2 - Sum_Y ^ 2
VAR Pearson_Denominator =
    SQRT ( Pearson_Denominator_X * Pearson_Denominator_Y )
RETURN
    DIVIDE ( Pearson_Numerator, Pearson_Denominator )

 

8.jpg9.jpg10.jpg

 


Best regards,

 

Community Support Team _ Dong Li
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors