Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
3 years ago
Solved

% Variation

Hey all,

I want to calculate the % variation of a stock, but I am having trouble to lock it's earliest and lastest value to perform my calculation.

StockMaxDateyear-monthValue
A7/31/2022Jul-2214
A8/31/2022Aug-2215
A9/30/2022Sep-2215
A10/31/2022Oct-2212
A11/30/2022Nov-2216
B7/31/2022Jul-2274
B8/31/2022Aug-2265
B9/30/2022Sep-2264
B10/31/2022Oct-2267
B11/30/2022Nov-2280
C7/31/2022Jul-22110
C8/31/2022Aug-22105
C9/30/2022Sep-2290
C10/31/2022Oct-22110
C11/30/2022Nov-2285

 

Besides this table, there is also a Calendar table that is connect by a Date column.

 

For Stock A I always want to Initial Value to be = 14. Then, as the months go by I want to:

  • August % = (15-14)/14
  • September = (15-14)/14
  • October = (12-14)/14
  • November = (16-14)/14

 

How can I do this? Thanks!

  • FreemanZ's avatar
    FreemanZ
    3 years ago

    hi Anonymous 

    try like:

    VAR% = 
    VAR _price = MAX(TableName[Value])
    VAR _stock = MAX(TableName[Stock])
    VAR _earliestdate =     //earliest date for the current stock
    MINX(
        FILTER(
            ALL(TableName),
            TableName[Stock] = _stock
        ),
        TableName[MaxDate]
    )
    VAR _earliestprice =    //value on the earliest date for the current stock
    MINX(
        FILTER(
            ALL(TableName),
            TableName[Stock] = _stock
                &&TableName[MaxDate]=_earliestdate
        ),
        TableName[Value]
    )
    RETURN
    IF(
        _price<>0,
        DIVIDE(_price- _earliestprice, _earliestprice)
    )

     

7 Replies

  • hi Anonymous 

     

    try to plot a table with this:

    VAR% =
    VAR _minprice =
    CALCULATE(
    MIN(TableName[Value]),
    ALL(TableName),
    VALUES(TableName[Stock])
    )
    VAR _price = MIN(TableName[Value])
    RETURN
    DIVIDE(_price- _minprice , _minprice)
     
    i tried and it worked like this:

     

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hi FreemanZ !

      Thanks, that worked! Just need a small adjustment. How do I get rid of those date that don't have any values?

       

      • FreemanZ's avatar
        FreemanZ
        Super User

        Hi  try like:

        VAR% =
        VAR _minprice =
        CALCULATE(
            MIN(TableName[Value]),
            ALL(TableName),
            VALUES(TableName[Stock])
        )
        VAR _price = MIN(TableName[Value])
        RETURN
        IF(
            _price<>0,
            DIVIDE(_price- _minprice ,  _minprice)
        )

        Anonymous