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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
AsnateKalnina
Frequent Visitor

Count cases with open status on selected periods

Hi,

I prepared Excel file with sample dataset, manual calculations with Excel formulas and the desired result. Please see screen shot of the first rows of the table.

Part of the table itself is at the bottom.

 

AsnateKalnina_0-1676385422909.png

 

In Power Bi I have a separate dimension table for general Calendar dates.

And I have DAX expression to calculate the desiderd result:

 

Count in Period =

CALCULATE (

    DISTINCTCOUNT( 'Fact'[Case Number]),

    FILTER (

        'Dim Calendar',

        MAX('Dim Calendar'[Date]) >= MIN( 'Fact'[D_VALID_FROM] )

            && MIN('Dim Calendar'[Date]) <= MAX ( 'Fact'[D_VALID_TO])

    )

)


It seems that the DAX formula is calculating correcly, but I do not quite understand how to link the desired result to the Calendar.

I can not link the Calendar table (Date) to D_VALID_FROM or D_VALID_TO fields.

If there are several weeks in between those dates, and user selects one of those interim weeks, the particular Case Number would not show in the result. 

 

 

But the overall process and problem is this:

I have a list of Case Numbers with their respective validity periods from- to.

Case Numbers may repeat as periods differ. 

The periods for one Case Number can be continuous- one period immediately follows the previous period, or a Case Number can have a gap in between two separate periods. 

 

At the end I want to have a chart with all week numbers on X axis (and week numbers can be filtered by normal Calendar table), and count number of cases- how many were valid in that week (or generally- selected period).

 

In the Excel file I highlight Case Numbers that have several lines,

If these periods overlap, they should not be counted as 2 cases per that week, but only as 1 case for the particular week.

So I erased the unnecessary results and marked those cells red.

 

Initially, it would be enough to have the result on weekly basis. So user can select one or several weeks in Calendar table and see number of active Case Numbers on that week, and also the specific case numbers that were active on that period.

Ideally, the calculation would work also for other manually selected periods of time, e.g. specific date periods. 

 

If there are any discussions about similar problems with solutions, I would appreciate if you could share those, as I have not managed to find yet.

 

This is the desired output ->Week Number232425262728293031323334353637383940414243444546474849505152123456
Count131213161513971423232221232121191820141615121314151612111191010111111
   week start06.06.202213.06.202220.06.202227.06.202204.07.202211.07.202218.07.202225.07.202201.08.202208.08.202215.08.202222.08.202229.08.202205.09.202212.09.202219.09.202226.09.202203.10.202210.10.202217.10.202224.10.202231.10.202207.11.202214.11.202221.11.202228.11.202205.12.202212.12.202219.12.202226.12.202202.01.202309.01.202316.01.202323.01.202330.01.202306.02.2023
these three columns are the initial datasetweek end12.06.202219.06.202226.06.202203.07.202210.07.202217.07.202224.07.202231.07.202207.08.202214.08.202221.08.202228.08.202204.09.202211.09.202218.09.202225.09.202202.10.202209.10.202216.10.202223.10.202230.10.202206.11.202213.11.202220.11.202227.11.202204.12.202211.12.202218.12.202225.12.202201.01.202308.01.202315.01.202322.01.202329.01.202305.02.202312.02.2023
D_VALID_FROMD_VALID_TOCase Numberduplicate                                    
08.08.202207.09.202210520000000001111 0000000000000000000000
08.09.202208.09.20221052000000000000010000000000000000000000
08.06.202203.07.202238831111100000000000000000000000000000000
17.10.202217.10.20221115620000000000000000000 0000000000000000
18.10.202224.10.2022111562000000000000000000011000000000000000
08.08.202225.08.2022121293000000000111000000000000000000000000
07.09.202207.09.2022121293000000000000010000000000000000000000
08.09.202208.09.20221212930000000000000 0000000000000000000000
08.06.202231.12.2999140721111111111111111111111111111111111111
30.06.202207.07.20221652920001 0000000000000000000000000000000
08.07.202212.07.2022165292000011000000000000000000000000000000
14.12.202227.12.2022374230300000000000000000000000000011 000000
28.12.202228.12.2022374230300000000000000000000000000000 000000
29.12.202229.12.20223742303000000000000000000000000000001000000
08.06.202221.07.2022166791111111100000000000000000000000000000
08.06.202221.07.2022166961111111100000000000000000000000000000
30.09.202212.10.2022202104152000000000000000011100000000000000000
08.09.202208.09.2022202104152000000000000010000000000000000000000
08.06.202212.10.2022166971111111111111111111100000000000000000
03.08.202222.09.2022168121000000001111111100000000000000000000
1 ACCEPTED SOLUTION
OwenAuger
Super User
Super User

Hi there @AsnateKalnina 

 

Take a look at this article that covers this type of "events in progress" calculation:

https://www.daxpatterns.com/events-in-progress/

 

Your measure is following the right sort of logic, but needs to be written a bit differently. I have created a sample PBIX using your data above.

In my dataset, there is no relationship between 'Dim Date' and 'Fact'. 

Case Count = 
VAR MinDate =
    MIN ( 'Dim Calendar'[Date] )
VAR MaxDate =
    MAX ( 'Dim Calendar'[Date] )
VAR Result =
    CALCULATE (
        DISTINCTCOUNT ( 'Fact'[Case Number] ),
        'Fact'[D_VALID_FROM] <= MaxDate,
        'Fact'[D_VALID_TO] >= MinDate
    )
RETURN
    Result

If there is a relationship between one of the date columns of 'Fact' and 'Dim Date' in your model:

  • You would need to add REMOVEFILTETRS ( 'Dim Date' ) within CALCULATE.
  • You would also want to create a Case Number dimension if Case Number is to be shown in the visuals

In my model, my Week Numbers are off by one compared to yours, but illustrate the idea.

 

Does something like this work in your model?

Regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

View solution in original post

2 REPLIES 2
OwenAuger
Super User
Super User

Hi there @AsnateKalnina 

 

Take a look at this article that covers this type of "events in progress" calculation:

https://www.daxpatterns.com/events-in-progress/

 

Your measure is following the right sort of logic, but needs to be written a bit differently. I have created a sample PBIX using your data above.

In my dataset, there is no relationship between 'Dim Date' and 'Fact'. 

Case Count = 
VAR MinDate =
    MIN ( 'Dim Calendar'[Date] )
VAR MaxDate =
    MAX ( 'Dim Calendar'[Date] )
VAR Result =
    CALCULATE (
        DISTINCTCOUNT ( 'Fact'[Case Number] ),
        'Fact'[D_VALID_FROM] <= MaxDate,
        'Fact'[D_VALID_TO] >= MinDate
    )
RETURN
    Result

If there is a relationship between one of the date columns of 'Fact' and 'Dim Date' in your model:

  • You would need to add REMOVEFILTETRS ( 'Dim Date' ) within CALCULATE.
  • You would also want to create a Case Number dimension if Case Number is to be shown in the visuals

In my model, my Week Numbers are off by one compared to yours, but illustrate the idea.

 

Does something like this work in your model?

Regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
LinkedIn

Yes, this works perfectly! Thank you very much!

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 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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