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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
chris2
Regular Visitor

[Solved] Counting rows with conditions and context

Hi! 

I have a list of events with an activation and inactivation date. I would like to create a measure which counts the number of active events (i.e. events where this condtion is true: ActiveDate < GivenDate && GivenDate < InactivDate || ISBLANK(InactivDate)) for any give date within the current context of the view.

 

Example Data for eventframes:

NameActiveDateInactiveDate

ABC

2022-01-012022-02-01
DEF2022-03-042022-07-01
GHI2022-04-06 

 

Example Data for dim_date:

Date
2022-01-01
2022-01-02
...

 

There are 1:n relationships between Date and ActiveDate (active) and between Date and InactiveDate (inactive).

 

This is my current approach:

 

 

Standing_events = 
VAR __currentDate = LASTDATE(dim_Date[Date])
RETURN
    (
        CALCULATE(COUNTROWS(eventframes),
            FILTER(
                eventframes,
                eventframes[ActiveDate] < __currentDate &&  (__currentDate < eventframes[InactiveDate] || ISBLANK(eventframes[InactiveDate]))
            )
    ))

 

 It works fine if I create a new table with a row for every date but returns an empty result if I use it as a measure.

 

Edit: 

This worked for me:

Standing_events = 
CALCULATE(
    COUNTX(
        FILTER(eventframes,eventframes[ActiveDate]<=max('dim_date'[Date]) && (ISBLANK(eventframes[InactiveDate]) || eventframes[InactiveDate]>max('dim_date'[Date]))),
        (raise_recover_eventframes[Eventname])),CROSSFILTER(eventframes[ActiveDate],'dim_date'[Date],None))

 

@amitchandak : Thanks!

1 ACCEPTED SOLUTION
amitchandak
Super User
Super User

@chris2 , Seem the case is very similar to my HR blog. Refer if blog or attached files can help

https://community.powerbi.com/t5/Community-Blog/HR-Analytics-Active-Employee-Hire-and-Termination-tr...

 

 

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

View solution in original post

3 REPLIES 3
Jihwan_Kim
Super User
Super User

Hi,

Please check the below picture and the attached pbix file.

 

Picture1.png

 

Standing_events =
COUNTROWS (
    FILTER (
        ALL ( eventframes ),
        eventframes[ActiveDate] <= MAX ( dim_Date[Date] )
            && OR (
                eventframes[InactiveDate] >= MIN ( dim_Date[Date] ),
                eventframes[InactiveDate] = BLANK ()
            )
    )
) + 0

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

This returned a count of all events. Anyway thank you for your effort.

amitchandak
Super User
Super User

@chris2 , Seem the case is very similar to my HR blog. Refer if blog or attached files can help

https://community.powerbi.com/t5/Community-Blog/HR-Analytics-Active-Employee-Hire-and-Termination-tr...

 

 

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.