Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
manglegose
Frequent Visitor

Create Measure with multiple and variable Conditions

Hi there,

 

I want to do some backtesting with stock market data via Power BI. This includes calculating the return of a certain strategy based on several filters.

In order to do that, I also need to remove duplicate data. Duplicate data means for me, that a ticker that is in my backtesting (or in the list below as an entry) already in my portfolio can't be bought more than once. In the example below the holding period of a position are 10 trading days. Trading days are translated via Date ID. In other words, Date ID consists only of trading days.

 

Here is some data that is extracted from my database and I added the column "How it should be" manually. I need a measure for that column. I also thought of a counter that adds up all the distances between the previous entries of a certain ticker. If they are >= 10 (or variable Holding time) it resets to 0 and counts on from there for the following entries.

 

[Data follows with the next post]

 

Since I started intensively with Power BI a few weeks ago column 3 to 5 are not calculated with measures, but within the data table.

 

I am trying to solve this for a few days, now, without any progress. So, I need your help for this one.

Any help is highly appreciated. Thank you in advance.

4 REPLIES 4
v-stephen-msft
Community Support
Community Support

Hi @manglegose ,

 

Sorry, I didn't find the [Holding Time in Trading Days] in sample data provided. How was the [Holding Time in Trading Days] created?

vstephenmsft_0-1696817488217.png

 

Best Regards,

Stephen Tao

 

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

Hi @v-stephen-msft ,

 

Thank you for your message.

 

[Holding Time in Trading Days] is just a variable (or measure) that equals 10 in my example. I use it as a variable in several measures.

 

Measure: 

Holding Time in Trading Days = 10
 
Thanks in advance.
 
Regards,
manglegose
manglegose
Frequent Visitor

Here are the formulas for the columns 3 to 5:

Duplicates = 
VAR CurrentDateID = 'Backtesting'[Date ID]
VAR CurrentTicker = 'Backtesting'[Ticker]

RETURN
IF(
    COUNTROWS(
        FILTER(
            'Backtesting',
            'Backtesting'[Ticker] = CurrentTicker &&
            'Backtesting'[Date ID] >= CurrentDateID - [Holding Time in Trading Days] &&
            'Backtesting'[Date ID] < CurrentDateID
        )
    ) >=1,
    "Yes",
    "No"
)

 

Holding Time Check = 
VAR CurrentDateID = 'Backtesting'[Date ID]
VAR CurrentTicker = 'Backtesting'[Ticker]
VAR CurrentDuplicates = 'Backtesting'[Duplicates]
VAR LastNoDateID =
    CALCULATE(
        MAX('Backtesting'[Date ID]),
        FILTER(
            'Backtesting',
            'Backtesting'[Ticker] = CurrentTicker &&
            'Backtesting'[Date ID] < CurrentDateID &&
            'Backtesting'[Duplicates] = "No"
        )
    )
VAR MinDateIDWithNo = 
    CALCULATE(
        MIN('Backtesting'[Date ID]),
        FILTER(
            'Backtesting',
            'Backtesting'[Ticker] = CurrentTicker &&
            'Backtesting'[Date ID] >= CurrentDateID - [Holding Time in Trading Days] &&
            'Backtesting'[Date ID] < CurrentDateID &&
            'Backtesting'[Duplicates] = "No"
        )
    )

VAR DuplicationCheck =
IF(
    (CurrentDateID - LastNoDateID >= [Holding Time in Trading Days] &&
    CurrentDateID - MinDateIDWithNo >= [Holding Time in Trading Days]),
    "No",
    "Yes"
)

VAR HoldingTimeCheck =
IF(
    COUNTROWS(
        FILTER(
            'Backtesting',
            'Backtesting'[Ticker] = CurrentTicker &&
            'Backtesting'[Date ID] >= CurrentDateID - [Holding Time in Trading Days] &&
            'Backtesting'[Date ID] < CurrentDateID &&
            'Backtesting'[Duplicates] = "No"
        )
    ) >= 1,
    "Yes",
    "No"
)

RETURN
IF(
    DuplicationCheck = "Yes" && HoldingTimeCheck = "Yes",
    "Yes",
    "No"
)

 

Distance Last No Duplicates = 
VAR CurrentDateID = 'Backtesting'[Date ID]
VAR CurrentTicker = 'Backtesting'[Ticker]
VAR LastNoDateID = 
    CALCULATE(
        Max('Backtesting'[Date ID]),
        FILTER(
            'Backtesting',
            'Backtesting'[Ticker] = CurrentTicker &&
            'Backtesting'[Date ID] < CurrentDateID &&
            'Backtesting'[Duplicates] = "No"
        )
    )
VAR PreviousDateID = 
    CALCULATE(
        Max('Backtesting'[Date ID]),
        FILTER(
            'Backtesting',
            'Backtesting'[Ticker] = CurrentTicker &&
            'Backtesting'[Date ID] < CurrentDateID
        )
    )

Return
IF(
    CurrentDateID - PreviousDateID < [Holding Time in Trading Days],
    CurrentDateID - PreviousDateID,
    CurrentDateID - LastNoDateID
)
manglegose
Frequent Visitor

Here is the sample data:

TickerDate IDDuplicatesHolding Time CheckDistance Last No DuplicatesHow it should be
AAPL12NoNo12No
AAPL443NoNo431No
AAPL445YesYes2Yes
AAPL446YesYes1Yes
AAPL447YesYes1Yes
AAPL448YesYes1Yes
AAPL449YesYes1Yes
AAPL470NoNo27No
AAPL471YesYes1Yes
AAPL472YesYes1Yes
AAPL474YesYes2Yes
AAPL475YesYes1Yes
AAPL476YesYes1Yes
AAPL477YesYes1Yes
AAPL478YesYes1Yes
AAPL479YesYes1Yes
AAPL481YesNo2No
AAPL483YesNo2Yes
AAPL488YesNo5Yes
AAPL490YesNo2Yes
AAPL495YesNo5No
AAPL496YesNo1Yes
AAPL542NoNo72No
AAPL543YesYes1Yes
GOOGL13NoNo13No
GOOGL15YesYes2Yes
GOOGL16YesYes1Yes
GOOGL17YesYes1Yes
GOOGL31NoNo18No
GOOGL34YesYes3Yes
GOOGL35YesYes1Yes
GOOGL36YesYes1Yes
GOOGL37YesYes1Yes
GOOGL38YesYes1Yes
GOOGL39YesYes1Yes
GOOGL40YesYes1Yes
GOOGL41YesNo1No
GOOGL42YesNo1Yes
GOOGL43YesNo1Yes
GOOGL44YesNo1Yes
GOOGL45YesNo1Yes
GOOGL46YesNo1Yes
GOOGL47YesNo1Yes
GOOGL48YesNo1Yes
GOOGL49YesNo1Yes
GOOGL50YesNo1Yes
GOOGL52YesNo2No
GOOGL53YesNo1Yes
GOOGL54YesNo1Yes

Helpful resources

Announcements
Sept PBI Carousel

Power BI Monthly Update - September 2024

Check out the September 2024 Power BI update to learn about new features.

September Hackathon Carousel

Microsoft Fabric & AI Learning Hackathon

Learn from experts, get hands-on experience, and win awesome prizes.

Sept NL Carousel

Fabric Community Update - September 2024

Find out what's new and trending in the Fabric Community.