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

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

1 ACCEPTED SOLUTION

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

 

FreemanZ_0-1671715185245.png

View solution in original post

7 REPLIES 7
FreemanZ
Super User
Super User

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:
FreemanZ_0-1671627843969.png

 

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?

filipebodas4_0-1671628590705.png

 

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

Anonymous
Not applicable

@FreemanZ I had to rephrase the problem. 

The solution you present is fine, but it only takes in account that the values from the Value column are always growing. So, I changed the table and what I am looking for is the oldest value for a given Stock and not the minimum. 

Cheers!

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

 

FreemanZ_0-1671715185245.png

Anonymous
Not applicable

Since the value of a stock can go to zero, I would just change _price <>0 to NOT(ISBLANK(_price))

this is with less codes and shall also work:

VAR%4 = 
VAR _price = MAX(TableName[Value])
VAR _earliestprice =
CALCULATE(
    MIN( TableName[Value]),
    TOPN(
        1,
        CALCULATETABLE(TableName, ALLEXCEPT( TableName, TableName[Stock])),
        TableName[MaxDate],
        ASC
    )
)
RETURN
IF(
    _price<>0,
    DIVIDE(_price- _earliestprice, _earliestprice)
)

 

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