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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
powerbidu
Frequent Visitor

write measure / dax to calculate number of rows for given date range

 

 

OrderIdentifierBuy/SellNo.of exceptions
1-OctABuy0
1-OctASell1
2-OctBBuy0
2-OctASell1
4-OctASell1
6-OctABuy1
8-OctABuy0
8-OctABuy0
10-OctASell2

 

I have the trade data presented above. For the same identifier, I want to identify the number of trades that have an opposite direction occurring within three days prior to each trade. 

 

For example, the first trade line will flag 0 exceptions since there are no prior trades. The second trade line (a sell trade for A) will flag one exception because there is a buy trade for A on the same date (i.e. this is within three days prior). The fourth trade line (another sell trade for A) will also flag one exception. The logic is that three days before 2nd October is 29th September, so I would like to count all opposite trades (i.e. buy trades) for A during the preceding three-day period (i.e., 29th September to 2nd October). I would like to create a measure that produces results shown under 'No. of exceptions' column. 

 

Additional question (seems tough to execute): I know I had set the date range to be three days, but could anyone also advise on how I could approach this if I want to dynamically change the number of days? I am visualising a slicer where I could select the number of days (e.g. 1 to 10 days) and the number of exceptions will change accordingly.

 

Thank you.

1 ACCEPTED SOLUTION
AmiraBedh
Super User
Super User

You need to create a calculated column if you are looking for a static solution :

No. of Exceptions = 
VAR CurrentDate = Trades[Order] 
VAR CurrentIdentifier = Trades[Identifier] 
VAR CurrentBuySell = Trades[Buy/Sell] 
VAR OppositeBuySell = IF(CurrentBuySell = "Buy", "Sell", "Buy") 
VAR StartDate = CurrentDate - 3  

VAR ExceptionCount = 

CALCULATE(
    COUNTROWS(Trades),
    FILTER(
        Trades,
        Trades[Identifier] = CurrentIdentifier &&  
        Trades[Buy/Sell] = OppositeBuySell &&  
        Trades[Order] >= StartDate && 
        Trades[Order] < CurrentDate  
    )
)
RETURN
IF(ISBLANK(ExceptionCount), 0, ExceptionCount)

 

AmiraBedh_0-1742762232492.png

If you want it dynamically, create a table that contains the number of days you want to use as a slicer :

DaysTable = GENERATESERIES(1, 10, 1)

Then another calculated column :

No. of Exceptions v2 = 
VAR Selection = SELECTEDVALUE(DaysTable[Value], 3) 
VAR CurrentDate = Trades[Order] 
VAR CurrentIdentifier = Trades[Identifier] 
VAR CurrentBuySell = Trades[Buy/Sell]
VAR OppositeBuySell = IF(CurrentBuySell = "Buy", "Sell", "Buy") 
VAR StartDate = CurrentDate - Selection
VAR ExceptionCount = 

CALCULATE(
    COUNTROWS(Trades),
    FILTER(
        Trades,
        Trades[Identifier] = CurrentIdentifier &&  
        Trades[Buy/Sell] = OppositeBuySell &&  
        Trades[Order] >= StartDate &&  
        Trades[Order] < CurrentDate  
    )
)
RETURN
IF(ISBLANK(ExceptionCount), 0, ExceptionCount)

 

AmiraBedh_1-1742762727413.png

 


Proud to be a Power BI Super User !

Microsoft Community : https://docs.microsoft.com/en-us/users/AmiraBedhiafi
Linkedin : https://www.linkedin.com/in/amira-bedhiafi/
StackOverflow : https://stackoverflow.com/users/9517769/amira-bedhiafi
C-Sharp Corner : https://www.c-sharpcorner.com/members/amira-bedhiafi
Power BI Community :https://community.powerbi.com/t5/user/viewprofilepage/user-id/332696

View solution in original post

4 REPLIES 4
v-mdharahman
Community Support
Community Support

Hi @powerbidu,

Thanks for reaching out to the Microsoft fabric community forum.

 

It looks like you want to calculate no of rows for some given data. As @AmiraBedh and @lbendlin both responded to your query, please go through the responses and mark the helpful reply as solution.

 

I would also take a moment to thank @AmiraBedh and @lbendlin, for actively participating in the community forum and for the solutions you’ve been sharing in the community forum. Your contributions make a real difference.

 

If I misunderstand your needs or you still have problems on it, please feel free to let us know.  

Best Regards,
Hammad.
Community Support Team

 

If this post helps then please mark it as a solution, so that other members find it more quickly.

Thank you.

AmiraBedh
Super User
Super User

You need to create a calculated column if you are looking for a static solution :

No. of Exceptions = 
VAR CurrentDate = Trades[Order] 
VAR CurrentIdentifier = Trades[Identifier] 
VAR CurrentBuySell = Trades[Buy/Sell] 
VAR OppositeBuySell = IF(CurrentBuySell = "Buy", "Sell", "Buy") 
VAR StartDate = CurrentDate - 3  

VAR ExceptionCount = 

CALCULATE(
    COUNTROWS(Trades),
    FILTER(
        Trades,
        Trades[Identifier] = CurrentIdentifier &&  
        Trades[Buy/Sell] = OppositeBuySell &&  
        Trades[Order] >= StartDate && 
        Trades[Order] < CurrentDate  
    )
)
RETURN
IF(ISBLANK(ExceptionCount), 0, ExceptionCount)

 

AmiraBedh_0-1742762232492.png

If you want it dynamically, create a table that contains the number of days you want to use as a slicer :

DaysTable = GENERATESERIES(1, 10, 1)

Then another calculated column :

No. of Exceptions v2 = 
VAR Selection = SELECTEDVALUE(DaysTable[Value], 3) 
VAR CurrentDate = Trades[Order] 
VAR CurrentIdentifier = Trades[Identifier] 
VAR CurrentBuySell = Trades[Buy/Sell]
VAR OppositeBuySell = IF(CurrentBuySell = "Buy", "Sell", "Buy") 
VAR StartDate = CurrentDate - Selection
VAR ExceptionCount = 

CALCULATE(
    COUNTROWS(Trades),
    FILTER(
        Trades,
        Trades[Identifier] = CurrentIdentifier &&  
        Trades[Buy/Sell] = OppositeBuySell &&  
        Trades[Order] >= StartDate &&  
        Trades[Order] < CurrentDate  
    )
)
RETURN
IF(ISBLANK(ExceptionCount), 0, ExceptionCount)

 

AmiraBedh_1-1742762727413.png

 


Proud to be a Power BI Super User !

Microsoft Community : https://docs.microsoft.com/en-us/users/AmiraBedhiafi
Linkedin : https://www.linkedin.com/in/amira-bedhiafi/
StackOverflow : https://stackoverflow.com/users/9517769/amira-bedhiafi
C-Sharp Corner : https://www.c-sharpcorner.com/members/amira-bedhiafi
Power BI Community :https://community.powerbi.com/t5/user/viewprofilepage/user-id/332696

Hi thank you so much! Your solution works (almost perfectly), except that the numbers of exceptions do not change when I click any other value on the slicer (the formula always runs based on the default value of '3' instead of the number marked in the slicer. Do you happen to know how to fix this?

lbendlin
Super User
Super User

The second trade line

Power BI does not guarantee any sort order.  You need to bring your own index

 

i.e. this is within three days prior

To do date math you need to provide correctly formatted dates.

 

lbendlin_0-1742759571369.png

 

lbendlin_1-1742759827694.png

 

To dynamically change the date range adjust the measure formula.

 

 

 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 community update carousel

Fabric Community Update - June 2025

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