Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
1 year ago

year differences calculations

Hello ,

 

I have this challenge , I need to make calculations for three different columns  <2 years, 2-5 years and >5 years using following measure below:

 

 

EnhancLead = VAR SelectedYear = SELECTEDVALUE('Calendar'[Date].[Year])
VAR FirstDayOfNextYear = DATE(SelectedYear + 1, 1, 1)
RETURN
    COUNTROWS(
        FILTER(
            CecCaseDetail,
            VAR TopToStateID =
                CALCULATE(
                    SELECTEDVALUE(CecCaseEvent[ToStateOfProceedingID]),
                    TOPN(
                        1,
                        FILTER(
                            ALL(CecCaseEvent),
                            CecCaseEvent[CecCaseID] = CecCaseDetail[CaseID] &&
                            CecCaseEvent[ToStateOfProceedingID] <> CecCaseEvent[fromstateofproceedingid] &&
                            CecCaseEvent[EventDate] < FirstDayOfNextYear
                        ),
                        CecCaseEvent[EventOrder],
                        DESC
                    )
                )
            RETURN
                TopToStateID IN {4} &&
                COUNTROWS(
                    FILTER(
                        'CecLeadingCase',
                        'CecLeadingCase'[CecCaseId] = CecCaseDetail[CaseID]))
                    )
                )

 

4 Replies

    • Anonymous's avatar
      Anonymous
      Not applicable

      Thank you for help but the link you attached I tried and did not get what I need can you help me to implement it in my measure?

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi Anonymous ,

     

    Please create a new enter table and use [Period range] as the column in the matrix:

    Table = 
    DATATABLE(
        "Period range", STRING,
        "Order", INTEGER,
        {
            {"<2 years", 1},
            {"2-5 years", 2},
            {">5 years", 3}
        }
    )

    Then use this measure as the value of the matrix.

    EnhancLead0 =
    VAR SelectedYear =
        YEAR ( TODAY () ) -- Replace 'Calendar'[Date] with TODAY() if it is relative to today's date.
    VAR PrevYear = SelectedYear - 1
    VAR FiveYear = SelectedYear - 4
    VAR CurrPeriodRange =
        SELECTEDVALUE ( 'Table'[Order] )
    VAR StartYear =
        SWITCH ( CurrPeriodRange, 1, PrevYear, 2, FiveYear, 3, 1900 )
    VAR EndYear =
        SWITCH ( CurrPeriodRange, 1, SelectedYear, 2, PrevYear - 1, 3, FiveYear - 1 )
    VAR Result =
        COUNTROWS (
            FILTER (
                CecCaseDetail,
                VAR TopToStateID =
                    CALCULATE (
                        SELECTEDVALUE ( CecCaseEvent[ToStateOfProceedingID] ),
                        TOPN (
                            1,
                            FILTER (
                                ALL ( CecCaseEvent ),
                                CecCaseEvent[CecCaseID] = CecCaseDetail[CaseID]
                                    && CecCaseEvent[ToStateOfProceedingID] <> CecCaseEvent[fromstateofproceedingid]
                                    && RELATED ( 'Calendar'[Year] ) >= StartYear
                                    && RELATED ( 'Calendar'[Year] ) <= EndYear
                            ),
                            CecCaseEvent[EventOrder], DESC
                        )
                    )
                RETURN
                    TopToStateID
                        IN { 4 }
                            && COUNTROWS (
                                FILTER ( 'CecLeadingCase', 'CecLeadingCase'[CecCaseId] = CecCaseDetail[CaseID] )
                            )
            )
        )
    RETURN
        Result

     

    Best Regards,
    Gao

    Community Support Team

     

    If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
    If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

    How to get your questions answered quickly --  How to provide sample data in the Power BI Forum

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hello Gao,

       

      Thank you for response,

      I have question why did you change it (red cicle on the picture) should be there CecCaseEvent[EventDate] 

      your solution calculates but deliver incorrect values. I believe it has something to do with variable for date and what I highlighted with red circle. Can you look at it again and double check?