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
Anonymous
Not applicable

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 🙂

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 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.

Top Solution Authors