Forum Discussion

Mr_Triongl's avatar
Mr_Triongl
Frequent Visitor
3 years ago

Create a live case count line graph

Hi,

 

I've got a table that collects info on cases. I'm trying to create a line graph chart showing the number of cases active per month by creating new columns for each month and trying to create something similiar to below to populate the values in each column month for the case.

 

Check Case Start Date, if it is <= March. If it's greater than this set the value to 0. But if it is <= March then check Case Completion Date to see if the date is => March then set value of 1. 

 

So it should look like the below chart

 

case IDCase Start DateCase Complete DateJanuaryFebMarchAprilMayJuneJuly
1234501/03/202324/06/20230011110
1010102/02/202305/05/20230111100

 

Is creating a new column for each month the best way to do this or is there a better way

10 Replies

  • Hi Mr_Triongl ,

     

    Creating new columns for this is pretty much the worst way to do this.

    All you need to do is:

     

    -1- Create a calendar table - plenty of resources online on how to do this in Power BI.

    -2- Send your table to the data model, ensuring you keep at least the [case ID], [Case Start Date], and [Case Complete Date] columns. Also send the calendar table if you've built it in Power Query (which I would recommend).

    -3- Create a measure like this:

    _noofOpenCases =
    VAR __cDate = MAX(calendar[date])
    RETURN
    CALCULATE(
        DISTINCTCOUNT(yourTable[caseID]),
        FILTER(
            yourTable,
            yourTable[Case Start Date] <= __cDate
            && (__cDate < yourTable[Case Complete Date] || ISBLANK(yourTable[Case Complete Date]))
        )
    )

     

    Now put any date column from your calendar table(date, month, year etc.) on the x-axis, and this measure on the y-axis, and it will show you the number of open cases at any given point in time.

    As the measure uses MAX(calendar[date]) in the variable, it will tell you the open cases at the END of the axis time period. If you want it from the start, just change this to MIN(calendar[date]).

     

    Pete

  • Mr_Triongl's avatar
    Mr_Triongl
    Frequent Visitor

    Thanks for the help and advice.

     

    With this measure does it make a live count of cases per whatever calendar (day,month, year) you choose. Say for example will it show 10 cases open in June but in July 7 if the number has dropped?

    • BA_Pete's avatar
      BA_Pete
      Super User

       

      Yes. Set it up as I've described and it will give you the open cases at each point in time based on the time period on the x-axis.

      E.g. If you put calendar[Date] on the axis, it will tell you the number of open cases at the end of each date.

      If you put calendar[Month] on the axis, it will tell you the number of cases that were open on June 30th, July 31st, August 31st etc.

       

      Pete

  • Mr_Triongl's avatar
    Mr_Triongl
    Frequent Visitor

    BA_Pete Looking at this it's not doing a live count it's just counting the number of cases opened for that specific time. What I'm trying to do(Not sure if it is possible in Power BI) is to count the number of cases open up until there is a completed date, something similar to this type of counting:

     

    Case IDStart DateCompleted DateJanFebMarApriMayJunJulAug
    00000105/02/202308/07/202301111110
    00000211/01/202314/03/202311100000
    00000318/04/202327/06/202300011100
    00000422/02/202310/10/202301111111
    00000508/03/202307/05/202300111000
    Count of open cases per month  13444321

     

    • BA_Pete's avatar
      BA_Pete
      Super User

       

      It's giving you the open case balance as at the END of the axis time period:

       

      The way that you've calculated your columns it looks like you want a case to be included in the open cases count for the month, even if it's been closed during that month. Is that correct?

       

      Pete

       

      • BA_Pete's avatar
        BA_Pete
        Super User

         

        To match the counting criteria that you've used for your columns, you would use this measure instead:

        _noofOpenCases = 
        VAR __minDate = MIN(cal[date])
        VAR __maxDate = MAX(cal[date])
        RETURN
        CALCULATE(
            DISTINCTCOUNT(caseTable[Case ID]),
            FILTER(
                caseTable,
                caseTable[Start Date] <= __maxDate
                && (__minDate < caseTable[Completed Date] || ISBLANK(caseTable[Completed Date]))
            )
        )

         

        Which gives you this data shape:

         

        Pete