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

Measure to Get Standard Deviation of Measure

Hello,

 

I have a measure with the following DAX:

Logs_7d moving average =
VAR period = 7
VAR the_first_date =
CALCULATE ( FIRSTDATE ( Logs[Date] ), ALLSELECTED ( Logs[Date] ) )
VAR last_date =
LASTDATE ( Logs[Date] )
VAR sum_in_period =
CALCULATE (
SUM ( Logs[Value] ),
DATESINPERIOD ( Logs[Date], last_date, - period, DAY )
)
RETURN
IF ( last_date - the_first_date >= period - 1, sum_in_period / period )
 
And I would like to now calculate 2* the standard deviation of that measure in order to create upper and lower bollinger bands. I have attempted the solutions in the following links with no success:
 
 
Edit: 
In case anyone wants to create bollinger bands in the future for a daily chart a la the below pic:
 
Blue line is the actual price line, orange is a 20 -day simple moving average, purple are the top and bottom bollinger bands.Blue line is the actual price line, orange is a 20 -day simple moving average, purple are the top and bottom bollinger bands.The following measure is for the SMA:
BB2.PNG
The standard deviations can be calculated using the same formula, but for the sum_in_period variable replace sum() with stdv.p() and remove the last division by the period like below:
bb3.PNG
Finally, you can just follow the equation for calculation the bands from here. 
Here is the upper for instance:
bb4.PNG
And the lower:
BB5.PNG
1 ACCEPTED SOLUTION
AlexisOlson
Super User
Super User

The pattern that should apply is this:

 

Logs_7d_MA_StdDev = 
STDEVX.P (
    VALUES ( DateTable[Date] ),
    [Logs_7d moving average]
)

 

However, this won't work as-is because your 7-day moving average is written using Logs[Date] instead of DateTable[Date]. If you update that measure to replace each of the former with the latter, then your Std Dev measure should be good to go.

View solution in original post

8 REPLIES 8
AlexisOlson
Super User
Super User

The pattern that should apply is this:

 

Logs_7d_MA_StdDev = 
STDEVX.P (
    VALUES ( DateTable[Date] ),
    [Logs_7d moving average]
)

 

However, this won't work as-is because your 7-day moving average is written using Logs[Date] instead of DateTable[Date]. If you update that measure to replace each of the former with the latter, then your Std Dev measure should be good to go.

Okay, I tried that but my sma and new standard deviation measure both do not chart. Maybe something wrong with my date table? Any ideas?

 

Standard Deviation MeasureStandard Deviation MeasureSMASMAChartChart

Use DateTable[Date] for the chart axis and update the 14-day measure too.

I did both of the recommendations you said. I can tell it did calculate something because when I put the measure on a tile, it gives me back the values and they change over time with a date slider. Unfortunately, I still can't get the measure to chart. pbhelp4.PNG

I can't tell what the problem is but I'm attaching the copy I modified where it does appear to be working.

I appreciate your help. I'd been able to overlay the different SMAs. I was trying to implement bollinger bands. I did end up getting it by just substituting stdv.p to my SMA measure where the average calc went and taking off the division by period at the end. I'm going to add pics of the calcs and the result to my op in case it can help others solve their problems.

AlexisOlson
Super User
Super User

The basic approach in the first link should work but might need a bit of adjustment to your particular case.

 

What granularity are you trying to compute the StdDev over? Daily?

 

I couldn't get the link to your file to work.

AlexisOlson_0-1637771419934.png

 

Yes, it's daily over 180 days.

 

I fixed the link in the post. Helps if I post a shareable link lol.

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.