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

I am new to DAX and looking for some help. 

I do not want to create a new measure but want to create new calculated columns. The final requirements to create measures will depend on these Calculated Columns.

 

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 , 10 Days Average etc.

 

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
Stachu
Community Champion
Community Champion

I assume the averages are for last x trading days, not calendar days, correct?
try this code

PreviousClosePrice = 
VAR __TickerID = 'Table'[TickerID]
VAR __DateCounterID = 'Table'[DateCounterID]
VAR __PreviousDateCounterID = CALCULATE(MAX('Table'[DateCounterID]), FILTER(ALL('Table'), 'Table'[TickerID]=__TickerID && 'Table'[DateCounterID]<__DateCounterID))
VAR __PreviousClosePrice = CALCULATE(MAX('Table'[ClosePrice]), FILTER(ALL('Table'), 'Table'[TickerID]=__TickerID && 'Table'[DateCounterID]=__PreviousDateCounterID))
RETURN
__PreviousClosePrice

this average includes the current row trading day close price in the average, if you want to exclude it replace blue code with __PreviousDateCounterID. To change number of days change the value in __NrOfDaysToAverage

3DayAverageClosePrice = 
VAR __TickerID = 'Table'[TickerID]
VAR __DateCounterID = 'Table'[DateCounterID]
VAR __PreviousDateCounterID = CALCULATE(MAX('Table'[DateCounterID]), FILTER(ALL('Table'), 'Table'[TickerID]=__TickerID && 'Table'[DateCounterID]<__DateCounterID))
VAR __NrOfDaysToAverage = 3
VAR __AverageClosePrice = 
CALCULATE(
AVERAGE('Table'[ClosePrice]),
FILTER(ALL('Table'),
'Table'[TickerID]=__TickerID &&
'Table'[DateCounterID] <= __DateCounterID &&
'Table'[DateCounterID] > __DateCounterID - __NrOfDaysToAverage
)
) RETURN __AverageClosePrice

 

Did I answer your question? Mark my post as a solution!

Proud to be a Datanaut!



Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

Proud to be a Super User!

Anonymous
Not applicable

Hi there.

 

Mate, this transaction table has the potential to be really big. If you use CALCULATE in a calculated column, be prepared that such code could return after a very, very, very long time or even not at all. This is due to CONTEXT TRANSITION which is an expensive operation and I've seen models where executing it in in rows of a table brought the whole model down to the point where it was unusable. CALCULATE should not be used in this case. One can rephrase this calculation using FILTER only and *X functions.

 

Best

Darek

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!