Forum Discussion

luzrueda's avatar
luzrueda
Icon for Helper I rankHelper I
4 months ago
Solved

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. Curre...
  • johnt75's avatar
    4 months ago

    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
        Result
    

    This 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.

  • cengizhanarslan's avatar
    4 months ago

    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
                    )
            )
        )