Forum Discussion

julesdude's avatar
julesdude
Post Partisan
3 years ago
Solved

Need help summing only latest value for a reference when calculating SUM result

I am piecing together a solution provided here with my own. Here is part of my table in my data model, there are many Asset References but here are the rows corresponding to one of them: Asset R...
  • wdx223_Daniel's avatar
    wdx223_Daniel
    3 years ago

    maybe you make a typo in the third row from bottom, the commencement date should be 01-JUL-21, if not, that row will not be selected. anyway, after change that date, i have made all the sample data you provided to get a correct result as the snapshoot.

    =
    VAR asOfDate =
        DATE ( 2021, 12, 31 )
    RETURN
        CALCULATE (
            SUMX (
                VALUES ( 'Lease_Unit'[Unit Reference] ),
                SUMX (
                    TOPN (
                        1,
                        FILTER (
                            'Lease_Unit',
                            'Lease_Unit'[Unit Reference] = EARLIER ( Lease_Unit[Unit Reference] )
                        ),
                        'Lease_Unit'[Expiration Date]
                    ),
                    'Lease_Unit'[Leased Area]
                )
            ),
            AND (
                Lease_Unit[Expiration Date] >= asOfDate,
                Lease_Unit[Commencement Date] <= asOfDate
            )
                || Lease_Unit[Status] IN { "Holding Over", "Month-to-Month" },
            COALESCE ( Lease_Unit[Termination Date], TODAY () ) >= asOfDate
        )