Forum Discussion

davib99's avatar
davib99
Frequent Visitor
1 year ago
Solved

What is the equivalent of this Python script to DAX?

Hello!   Suppose I have a table with one date column and 300 other columns named by stocks with values being the daily return of these stocks on each date. My goal is to have a slicer of  the colum...
  • Anonymous's avatar
    Anonymous
    1 year ago

    Thank you for your prompt reply! Your answer provided me with a great idea!

    Hi davib99 

    Based on your needs, I have created the following table.

     

    In the Power Query Editor, select the table that contains the dates and daily returns of the stocks. Select all the stock columns (e.g., StockA, StockB, StockC). You can select multiple columns by holding down the Ctrl key. After selecting all the stock columns, right-click any of the selected columns. Choose the 'Unpivot Columns' option.



     

    Then create the following measure:

     

     

     

     

    Cumulative Return = 
    VAR CurrentDate = MAX('StockReturns'[Date])
    VAR CurrentStock = SELECTEDVALUE('StockReturns'[Stock])
    RETURN
    CALCULATE(
    PRODUCTX(
    FILTER(
    ALL('StockReturns'),
    'StockReturns'[Date] <= CurrentDate &&
    'StockReturns'[Stock] = CurrentStock
    ),
    1 + 'StockReturns'[Value]
    ) - 1
    )

     

     

     

    Result:

     

     

     

     

     

    Best Regards,

    Jayleny

     

    If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.