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

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