cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
DaxNewbie2019
Regular Visitor

Calculated Column for previous trading day close price and previous 3 trading days avg close price

 am new to DAX and looking for some help. 

 

I have below sample dataset to work with in Tabular Model (DateCounterID is brought from Date table using related function). Date table keeps track of Trading Days and adds a counter to DateCounterID. in Date table, the dates trading didn't happen DateCounterID is populated as 0. My Goal is to have few more calculated columns created to store "Previous Trading Day's Close Price", 3 Days Average of Closing Price ( 3 day avg is average of 3 previous day's closing price), and then 5 Days Average closing price, 10 Days Average closing price etc.

 

I think we need to create calculated column to save previous trading day's close price. This can not be done in a measure as there is no aggregation. Also, the final requirements to create measures will depend on these Calculated Columns when users will be selecting Date range from PowerBI. Say a measure would be % increase in Stock Price for the selected range where we need to use closing price of previous day on start day and closing price on end day.

 

TickerIDDateClosePriceDateKeyDateCounterID
11/3/2017382017010310750
21/3/201750.872017010310750
11/4/201738.292017010410751
21/4/201751.122017010410751
11/5/201738.572017010510752
21/5/201751.752017010510752
11/6/201737.912017010610753
21/6/201751.152017010610753
11/9/201737.312017010910754
21/9/201750.612017010910754
11/10/201737.112017011010755
21/10/201750.622017011010755
11/11/201737.552017011110756
21/11/201751.212017011110756
11/12/201737.762017011210757
21/12/201751.812017011210757
11/13/201737.662017011310758
21/13/201751.592017011310758
11/17/201737.442017011710760
21/17/201751.512017011710760
11/18/201737.12017011810761
21/18/201751.192017011810761
11/19/201736.92017011910762
21/19/201750.662017011910762
11/20/201736.842017012010763
21/20/201751.162017012010763
11/23/201736.612017012310764
21/23/201750.912017012310764
11/24/201736.912017012410765
21/24/201751.232017012410765
11/25/201737.032017012510766
21/25/201751.482017012510766
11/26/201736.742017012610767
21/26/201750.792017012610767
11/27/201736.582017012710768
21/27/201750.42017012710768
11/30/201735.962017013010769
21/30/201749.822017013010769
11/31/201735.982017013110770
21/31/201750.562017013110770
2 REPLIES 2
Anonymous
Not applicable

You could do these as measures, all depends on what your final goal is and all of that. But going the calculated column route:

Prev Close = 
var __CurrentDate= 'Table'[DateCounterID]
var __CurrentTicker = 'Table'[TickerID]
return

CALCULATE(
    sum( 'Table'[ClosePrice] ),
    filter(
        'Table',
        'Table'[TickerID] = __CurrentTicker
        && 
        'Table'[DateCounterID] = __CurrentDate - 1 
    )
)
3 Day Average = 
var __CurrentDate= 'Table'[DateCounterID]
var __CurrentTicker = 'Table'[TickerID]
vAR __MALength= 3
return

Var __MovingAverage=
CALCULATE(
    AVERAGEX(
    filter(
        ALL('Table'),
        'Table'[TickerID] = __CurrentTicker
        && 
        'Table'[DateCounterID] = __CurrentDate 
        &&
        'Table'[DateCounterID] >= __CurrentDate - (__MALength-1)
    ),
    sum('Table'[ClosePrice])
))

Var __RowCount=
COUNTROWS(
       filter(
        ALL('Table'),
        'Table'[TickerID] = __CurrentTicker
        && 
        'Table'[DateCounterID] <= __CurrentDate 
        
    )
)
RETURN
if( __RowCount >= __MALength,__MovingAverage)

Moving average one looks much worse than it is. Just basically feeding the averagex function a list of dates ( using your DateCounter ID, which for ticker1 skips from 10758 to 10760...) then does a check to see if there is enough dates for the moving average. Probably didnt want to see a 3 day MA figure on day two...  You can just copy that code, change the __MALength variable to whatever other length. 

Moving Average and Prve Close calc columns.png

Anonymous
Not applicable

Here's a post I did about moving averages in measure form. 

https://community.powerbi.com/t5/Desktop/Moving-Average-Calculation/m-p/659153

Helpful resources

Announcements
Microsoft Build 768x460.png

Microsoft Build is May 24-26. Have you registered yet?

Come together to explore latest innovations in code and application development—and gain insights from experts from around the world.

May 23 2022 epsiode 5 without aka link.jpg

The Power BI Community Show

Welcome to the Power BI Community Show! Jeroen ter Heerdt talks about the importance of Data Modeling.

Power BI Dev Camp Session 22 with aka link 768x460.jpg

Check it out!

Mark your calendars and join us on Thursday, May 26 at 11a PDT for a great session with Ted Pattison!

charticulator_carousel_with_text (1).png

Charticulator Design Challenge

Put your data visualization and design skills to the test! This exciting challenge is happening now through May 31st!