Forum Discussion

AbhinavJoshi's avatar
AbhinavJoshi
Icon for Responsive Resident rankResponsive Resident
2 years ago
Solved

Calculate Year over Year Count

Hello,

I have the following dataset 
Clients Table
ClientID, MonthsActive, YearsActive, IsTerminated, TotalLives, SizeBand, TerminationDate, EnrollmentDate.
If the client is still active the IsTerminated would be false and TerminationDate would be blank.

Date Table - which is joined to Clients table on Enrollment date using one-many relationship. 

I am looking to calculate the count of Clients Year over Year. For example, for the year 2020 total number of rows in Clients are 20. 15 of them were enrolled in 2019, 2 in 2020, and the 3 are terminated. For the year 2021, we got 5 more clients, but lost 1, so I am looking to see the count of clients year over year. Condition can be Errolled date is before the year passed and if they are terminated, termination date is greater than the passed year so the client can be accounted for that year. 

Thank you,

 

  • danextian's avatar
    danextian
    2 years ago

    Hi AbhinavJoshi ,

     

    Create a separate dates table:

    Dates = 
    ADDCOLUMNS (
        CALENDAR ( DATE ( 2007, 1, 1 ), TODAY () ),
        "Year", YEAR ( [Date] ),
        "YYYYMM", FORMAT ( [Date], "YYYYMM" )
    )
    

    Link this to Enrollment date (active relationship) and Termination Date (inactive) usingi a one to many single direction relationship.

     

    Create these measures:

    Enrolled = 
    CALCULATE (
        COUNTROWS ( 'Table' ),
        FILTER ( ALL ( Dates ), Dates[Date] <= MAX ( Dates[Date] ) )
    )
    
    Terminated = 
    CALCULATE (
        COUNTROWS ( 'Table' ),
        FILTER (
            ALL ( Dates ),
            Dates[Date] <= MAX ( Dates[Date] )
                && NOT ( ISBLANK ( Dates[Date] ) )
        ),
        USERELATIONSHIP ( Dates[Date], 'Table'[TerminationDate] )
    )
    
    Active = 
    [Enrolled] - [Terminated]

     

    Or you can put them in one measure instead. I separate them so you can see what happens for each.

    I modified your sample data a bit by adding an extra termination date for Account2. Please refer to the attached pbix for the details.

     

     

     

3 Replies

  • AbhinavJoshi's avatar
    AbhinavJoshi
    Icon for Responsive Resident rankResponsive Resident

    Hello danextian. Please see dataset attached 

    Client IDEnrollmentDateLivesYearsActiveMonthsActiveSizeBandTerminated

    TerminationDate

    Account12022-06-01 0:0012.001368925393566241: 3 - 10FALSE 
    Account22007-03-01 0:00717.2539356605065032071: 3 - 10FALSE 
    Account32010-11-01 0:0011013.5824777549623551634: 101 - 200FALSE 
    Account42007-03-01 0:00917.2539356605065032071: 3 - 10FALSE 
    Account52023-09-01 0:001910.750171115674195894: 101 - 200FALSE 
    Account62021-05-01 0:00333.0855578370978782372: 11 - 50FALSE 
    Account72007-03-01 0:00215.9166661911: 3 - 10TRUE2023-02-01 0:00
    Account82022-02-01 0:001352.329911019849418284: 101 - 200FALSE 
    Account92022-05-01 0:0052.086242299794661251: 3 - 10FALSE 
    Account102022-06-01 0:0022.001368925393566241: 3 - 10FALSE 
    Account112023-03-01 0:0041.2539356605065024151: 3 - 10FALSE 
    Account122023-07-01 0:00100.919917864112: 11 - 50FALSE 
    Account132023-12-01 0:0030.501026694045174561: 3 - 10FALSE 
    Account142024-03-01 0:0080.251882272416153331: 3 - 10FALSE 
    Account152023-09-01 0:0010.750171115674195891: 3 - 10FALSE 
            

     

    Here Clients Active for 2007 shoud be 3, (Account7 was terminated on 2023 so should be counted as active until then). For 2008 it should be 3, For 2009 should be 3, For 2010 should be 4, For 2021 shoud be 5, For 20202 shoud be 9, For 2023 should be 13 ( Total 14 but remove Account 7 as it got terminated that year), For 2024 shoud be 14. Thank you! 

     

     

     

    • danextian's avatar
      danextian
      Icon for Super User rankSuper User

      Hi AbhinavJoshi ,

       

      Create a separate dates table:

      Dates = 
      ADDCOLUMNS (
          CALENDAR ( DATE ( 2007, 1, 1 ), TODAY () ),
          "Year", YEAR ( [Date] ),
          "YYYYMM", FORMAT ( [Date], "YYYYMM" )
      )
      

      Link this to Enrollment date (active relationship) and Termination Date (inactive) usingi a one to many single direction relationship.

       

      Create these measures:

      Enrolled = 
      CALCULATE (
          COUNTROWS ( 'Table' ),
          FILTER ( ALL ( Dates ), Dates[Date] <= MAX ( Dates[Date] ) )
      )
      
      Terminated = 
      CALCULATE (
          COUNTROWS ( 'Table' ),
          FILTER (
              ALL ( Dates ),
              Dates[Date] <= MAX ( Dates[Date] )
                  && NOT ( ISBLANK ( Dates[Date] ) )
          ),
          USERELATIONSHIP ( Dates[Date], 'Table'[TerminationDate] )
      )
      
      Active = 
      [Enrolled] - [Terminated]

       

      Or you can put them in one measure instead. I separate them so you can see what happens for each.

      I modified your sample data a bit by adding an extra termination date for Account2. Please refer to the attached pbix for the details.