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
gunicotra
Helper II
Helper II

Moving average for Bollinger bands

Hi to all,

I have a financial data on several Stocks I wish I can anilyze them with a Bollinger bands approach. Thus I started from a 20 days moving average with this formula:


MovingAverage20d =
Var PeriodEnd = LASTDATE('History-TopStocks'[Date])
Var PeriodStart= FIRSTDATE( DATESINPERIOD('History-TopStocks'[Date], PeriodEnd, -20, DAY))
Return
CALCULATE(AVERAGE('History-TopStocks'[Adj Close]),
DATESBETWEEN('History-TopStocks'[Date], PeriodStart, PeriodEnd))

 

gunicotra_0-1631703962033.png


However it does not sum at all (i.e.: each MovingAverage20d item is the same as AdjClose), neither makes the average on them! I attach a screeenshot for further details.

Any ideas?

Thanks

1 ACCEPTED SOLUTION

by trying and trying, I got a solution that wonder whether it appears good also for you.


With this code, I may have found the Simple Moving Average of 20 days also taking into consideration the fact that I have several stocks in my table. 


SMA =
CALCULATE(sum('History-TopStocks'[Adj Close]),
     DATESINPERIOD('History-TopStocks'[Date],
                LASTDATE('History-TopStocks'[Date]),-20, DAY),
ALLEXCEPT('History-TopStocks','History-TopStocks'[Symbol]))
/
CALCULATE(DISTINCTCOUNT('History-TopStocks'[Date]),
       DATESINPERIOD('History-TopStocks'[Date],
              LASTDATE('History-TopStocks'[Date]),-20, DAY),
ALLEXCEPT('History-TopStocks','History-TopStocks'[Symbol]))

What do you think?

View solution in original post

4 REPLIES 4
amitchandak
Super User
Super User

@gunicotra , Seem like you need a column, try like

 

New column =

var _dt = 'History-TopStocks'[Date]

CALCULATE(AVERAGEX(filter( 'History-TopStocks', [Date] >= _dt-20 && [Date] <= _dt) 'History-TopStocks'[Adj Close]))

 

if you need a measure, with help from date table

 

Rolling 20 = CALCULATE(sum(Sales[Sales Amount]),DATESINPERIOD('Date'[Date ],MAX('Date'[Date ]),-20,DAY))

hi amitchandak!
I tried either with your:

 
CALCULATE(AVERAGEX(filter( 'History-TopStocks', [Date] >= _dt-20 && [Date] <= _dt), 'History-TopStocks'[Adj Close]))
 
or with:

CALCULATE(AVERAGEX(filter( 'History-TopStocks', [Date] >= _dt-20 && [Date] <= _dt), sum('History-TopStocks'[Adj Close])))
 
however in both methods I had the same result: AdjClose = MovingAverage20d (see img below).


gunicotra_0-1631708175533.png

 

 

Moreover, my table has got several Stocks in it, therefore the moving average calculations should start from the beginning as the stock changes.... difficulty inside the difficulty (at least for me!!)  

by trying and trying, I got a solution that wonder whether it appears good also for you.


With this code, I may have found the Simple Moving Average of 20 days also taking into consideration the fact that I have several stocks in my table. 


SMA =
CALCULATE(sum('History-TopStocks'[Adj Close]),
     DATESINPERIOD('History-TopStocks'[Date],
                LASTDATE('History-TopStocks'[Date]),-20, DAY),
ALLEXCEPT('History-TopStocks','History-TopStocks'[Symbol]))
/
CALCULATE(DISTINCTCOUNT('History-TopStocks'[Date]),
       DATESINPERIOD('History-TopStocks'[Date],
              LASTDATE('History-TopStocks'[Date]),-20, DAY),
ALLEXCEPT('History-TopStocks','History-TopStocks'[Symbol]))

What do you think?

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.