Forum Discussion
How to count open cases by selected date without generating all intermediate dates?
I’m trying to build a Power BI dashboard that shows how many cases were open on any given day/week, without generating a full list of dates between the Open Date and Close Date for every case.
Current approach
Right now, I generate all dates between:
- Case Open Date
- Case Close Date
This works, but the dataset becomes very large and refresh/update times are very slow.
What I want
I want to calculate dynamically which cases were open on a selected date (or during a selected week) using a measure, instead of creating all intermediate dates.
Example:
| A | 01.04.2025 | 22.05.2025 |
If the report is filtered to:
- 01.05.2025 → Case A should be counted as Open
- Any week between April and May 2025 → Case A should also appear as open
Questions
- Is it possible to do this efficiently with a DAX measure instead of generating all dates?
- What is the best practice for this scenario in Power BI?
- Which date table relationship should be used:
- Open Date
- Close Date
- Or no active relationship at all?
- How would the DAX measure look for counting “cases open on selected date”?
I’m looking for a scalable solution because the dataset is large and performance is important. Thank you so much for your help
| Case | open Date | Close Date |
| 0000463681 | 02.12.2016 | 08.05.2026 |
| 0000463687 | 02.12.2016 | 08.05.2026 |
| 0000572557 | 16.12.2016 | 08.05.2026 |
| 0000572558 | 16.12.2016 | 08.05.2026 |
| 0000574630 | 19.12.2016 | 08.05.2026 |
| 0000575372 | 19.12.2016 | 08.05.2026 |
| 0000647478 | 22.12.2016 | 08.05.2026 |
| 0000661133 | 30.12.2016 | 08.05.2026 |
| 0000825124 | 30.01.2017 | 08.05.2026 |
| 0001047913 | 06.03.2017 | 08.05.2026 |
| 0001132024 | 28.03.2017 | 08.05.2026 |
| 0001140763 | 07.04.2017 | 08.05.2026 |
| 0001399807 | 07.06.2017 | 08.05.2026 |
| 0001522944 | 17.07.2017 | 08.05.2026 |
| 0001522945 | 17.07.2017 | 08.05.2026 |
| 0001857045 | 10.10.2017 | 08.05.2026 |
| 0001857047 | 10.10.2017 | 08.05.2026 |
| 0001857147 | 24.10.2017 | 08.05.2026 |
| 0001857168 | 26.10.2017 | 08.05.2026 |
| 0001857170 | 26.10.2017 | 08.05.2026 |
| 0001857359 | 09.11.2017 | 08.05.2026 |
| 0001913407 | 24.11.2017 | 08.05.2026 |
| 0001971450 | 28.11.2017 | 08.05.2026 |
| 0001984037 | 30.11.2017 | 08.05.2026 |
| 0001985836 | 06.12.2017 | 08.05.2026 |
| 0002003063 | 13.12.2017 | 08.05.2026 |
| 0002109739 | 18.12.2017 | 08.05.2026 |
| 0002109953 | 18.12.2017 | 08.05.2026 |
| 0002146521 | 22.12.2017 | 08.05.2026 |
| 0002154560 | 06.01.2018 | 08.05.2026 |
| 0002290097 | 17.01.2018 | 08.05.2026 |
| 0002292748 | 18.01.2018 | 08.05.2026 |
| 0002303528 | 04.02.2018 | 08.05.2026 |
| 0002307607 | 06.02.2018 | 08.05.2026 |
| 0002620478 | 14.03.2018 | 08.05.2026 |
| 0002642428 | 16.03.2018 | 08.05.2026 |
| 0002642570 | 16.03.2018 | 08.05.2026 |
| 0002680144 | 19.03.2018 | 08.05.2026 |
| 0002695955 | 22.03.2018 | 08.05.2026 |
| 0002866033 | 16.04.2018 | 08.05.2026 |
| 0002944421 | 18.04.2018 | 08.05.2026 |
| 0003094912 | 28.05.2018 | 08.05.2026 |
| 0003094913 | 28.05.2018 | 08.05.2026 |
| 0003244075 | 16.06.2018 | 08.05.2026 |
| 0003254943 | 05.07.2018 | 08.05.2026 |
| 0003254950 | 05.07.2018 | 08.05.2026 |
| 0003254953 | 05.07.2018 | 08.05.2026 |
| 0003254967 | 05.07.2018 | 08.05.2026 |
| 0003254971 | 05.07.2018 | 08.05.2026 |
| 0003254975 | 05.07.2018 | 08.05.2026 |
| 0003254976 | 05.07.2018 | 08.05.2026 |
| 0003254991 | 05.07.2018 | 08.05.2026 |
| 0003255006 | 05.07.2018 | 08.05.2026 |
| 0003255023 | 05.07.2018 | 08.05.2026 |
| 0003255025 | 05.07.2018 | 08.05.2026 |
| 0003256782 | 14.07.2018 | 08.05.2026 |
| 0003256789 | 14.07.2018 | 08.05.2026 |
| 0003256809 | 16.07.2018 | 08.05.2026 |
| 0003256825 | 16.07.2018 | 08.05.2026 |
| 0003256828 | 16.07.2018 | 08.05.2026 |
| 0003256830 | 16.07.2018 | 08.05.2026 |
| 0003256837 | 16.07.2018 | 08.05.2026 |
| 0003256844 | 16.07.2018 | 08.05.2026 |
| 0003256878 | 17.07.2018 | 08.05.2026 |
| 0003256883 | 17.07.2018 | 08.05.2026 |
| 0003256884 | 17.07.2018 | 08.05.2026 |
| 0003256899 | 17.07.2018 | 08.05.2026 |
| 0003256904 | 17.07.2018 | 08.05.2026 |
| 0003256923 | 17.07.2018 | 08.05.2026 |
| 0003256953 | 17.07.2018 | 08.05.2026 |
| 0003256954 | 17.07.2018 | 08.05.2026 |
| 0003256970 | 18.07.2018 | 08.05.2026 |
| 0003265046 | 20.07.2018 | 08.05.2026 |
| 0003265063 | 21.07.2018 | 08.05.2026 |
| 0003375992 | 24.07.2018 | 08.05.2026 |
| 0003396343 | 26.07.2018 | 08.05.2026 |
| 0003396349 | 26.07.2018 | 08.05.2026 |
| 0003396350 | 26.07.2018 | 08.05.2026 |
| 0003396351 | 26.07.2018 | 08.05.2026 |
| 0003396352 | 26.07.2018 | 08.05.2026 |
| 0003396579 | 27.07.2018 | 08.05.2026 |
| 0003396581 | 27.07.2018 | 08.05.2026 |
| 0003396592 | 27.07.2018 | 08.05.2026 |
| 0003396602 | 27.07.2018 | 08.05.2026 |
| 0003396608 | 27.07.2018 | 08.05.2026 |
| 0003396610 | 27.07.2018 | 08.05.2026 |
| 0003396612 | 27.07.2018 | 08.05.2026 |
| 0003396646 | 28.07.2018 | 08.05.2026 |
| 0003396670 | 31.07.2018 | 08.05.2026 |
| 0003432658 | 01.08.2018 | 08.05.2026 |
| 0003432660 | 01.08.2018 | 08.05.2026 |
| 0003432827 | 01.08.2018 | 08.05.2026 |
| 0003446673 | 01.08.2018 | 08.05.2026 |
| 0003446686 | 02.08.2018 | 08.05.2026 |
| 0003451940 | 04.08.2018 | 08.05.2026 |
| 0003452145 | 03.08.2018 | 08.05.2026 |
| 0003452150 | 04.08.2018 | 08.05.2026 |
| 0003452277 | 06.08.2018 | 08.05.2026 |
| 0003452278 | 06.08.2018 | 08.05.2026 |
| 0003452279 | 06.08.2018 | 08.05.2026 |
Make sure there is no relationship between your date table and your cases table, then write a measure like
Num open cases = VAR MaxDate = MAX ( 'Date'[Date] ) VAR Result = CALCULATE ( COUNTROWS ( Cases ), Cases[Open Date] <= MaxDate && ( ISBLANK ( Cases[Close Date] ) || Cases[CloseDate] >= MaxDate ) ) RETURN ResultThis assumes that there is 1 row per case in your Cases table. If that's not the case then you will need to do a DISTINCTCOUNT on Case ID, but that could be significantly slower than a COUNTROWS.
The measure should work at any granularity of date. It counts the number of cases where the open date is on or before the end of the period and the close date is either blank ( presumably still open ) or is on or after the end of the period.
Use no active relationship between the Date table and the Cases table. Then use the following measure:
Open Cases = VAR _SelectedStart = MIN ( 'Date'[Date] ) VAR _SelectedEnd = MAX ( 'Date'[Date] ) RETURN CALCULATE ( COUNTROWS ( Cases ), FILTER ( ALL ( Cases ), Cases[Open Date] <= _SelectedEnd && ( ISBLANK ( Cases[Close Date] ) || Cases[Close Date] >= _SelectedStart ) ) )
6 Replies
- johnt75Super User
Make sure there is no relationship between your date table and your cases table, then write a measure like
Num open cases = VAR MaxDate = MAX ( 'Date'[Date] ) VAR Result = CALCULATE ( COUNTROWS ( Cases ), Cases[Open Date] <= MaxDate && ( ISBLANK ( Cases[Close Date] ) || Cases[CloseDate] >= MaxDate ) ) RETURN ResultThis assumes that there is 1 row per case in your Cases table. If that's not the case then you will need to do a DISTINCTCOUNT on Case ID, but that could be significantly slower than a COUNTROWS.
The measure should work at any granularity of date. It counts the number of cases where the open date is on or before the end of the period and the close date is either blank ( presumably still open ) or is on or after the end of the period.
- cengizhanarslanSuper User
Use no active relationship between the Date table and the Cases table. Then use the following measure:
Open Cases = VAR _SelectedStart = MIN ( 'Date'[Date] ) VAR _SelectedEnd = MAX ( 'Date'[Date] ) RETURN CALCULATE ( COUNTROWS ( Cases ), FILTER ( ALL ( Cases ), Cases[Open Date] <= _SelectedEnd && ( ISBLANK ( Cases[Close Date] ) || Cases[Close Date] >= _SelectedStart ) ) )- luzruedaHelper I
Thank you so much cengizhanarslan I got great results with your approach!
- v-echaithraCommunity Support
Hi luzrueda ,
Thank you cengizhanarslan , johnt75 for your inputs.
We’d like to follow up regarding the recent concern. Kindly confirm whether the issue has been resolved, or if further assistance is still required. We are available to support you and are committed to helping you reach a resolution.
Thank you.- luzruedaHelper I
Thank you for your reminder and support!