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 Reference  Unit Reference  Leased Area   Status  Commencement Date    Expiration Date  Termination Date
100AAA14654Active20-Jun-1719-Jun-27 
100BBB16387Active11-Aug-1411-Feb-33 
100CCC6597Active28-Jan-1827-Jan-28 
100DDD4747Active30-Apr-2129-Apr-31 
100EEE12366Active16-Aug-1315-Aug-23 
100CCC6597Holding Over28-Jan-0827-Jan-18 
100FFF2912Holding Over30-Jan-1829-Jan-28 
100GGG5177Holding Over06-Feb-1805-Feb-28 
100DDD4747Terminated15-Jul-1414-Jul-2429-Apr-21
100DDD Terminated21-Nov-2029-Jan-2122-Nov-20
100FFF Active27-Jan-2326-Jan-33 

My matrix visual tries to sum this table by Leased Area total for each Asset Reference - so one row for each Asset Reference

This particular Asset Reference is not working well with my DAX measure.

The rules in the measure are listed below. [as of date]  is the date a user has chosen in my report:

1. Filter keeping results where Commencement Date is <= [as of date] and Expiration Date >= [as of date]
OR
either Lease Status = "Holding Over" or Lease Status = "Month-to-Month"

2. Filter further keeping rows where Termination Date = blank or >= [as of date] if there is a date there

3. Filter further keeping the Leased Area value with the latest Commencement Date IF there is a duplicate Unit Reference. So in the case of Unit Reference CCC with the [as of date] being 31/12/2022 you'd want to keep the row with the Commencement Date of 28-Jan-18 and discard the earlier dated one. 

4. SUM the above filtered results to give a total Leased Area for this particular Asset Reference

My DAX is succesful with the first two items, but I'm not sure it is correctly applying the 3rd because for the above I am getting a total Leased Area of 69.437 but it should be 62,840.
My DAX:

 

 

 

Total Leased Area (Current) = 
VAR asOfDate = [As Of Date]

VAR filtTblOne =
    FILTER (
        Lease_Unit,
        AND(Lease_Unit[Expiration Date] >= asOfDate, Lease_Unit[Commencement Date] <= asOfDate)
            || ( Lease_Unit[Lease Status] = "Holding Over"
            || Lease_Unit[Lease Status] = "Month-to-Month" )
    )
VAR filtTblTwo =
    FILTER (
        filtTblOne,
        Lease_Unit[Termination Date] = BLANK ()
            || Lease_Unit[Termination Date] >= asOfDate
    )

VAR filtTblThree = 
    FILTER (
        filtTblTwo,         
        Lease_Unit[Commencement Date] <= asOfDate
        && LASTNONBLANKVALUE (
            Lease_Unit[Unit Reference],max(Lease_Unit[Commencement Date])
        )
    )

RETURN


    CALCULATE ( SUM ( Lease_Unit_DST[Leased Area] ), filtTblThree )

 

 

 

Any suggestions on where I'm going wrong?

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

9 Replies

  • wdx223_Daniel's avatar
    wdx223_Daniel
    Community Champion

    Total Leased Area (Current)=VAR asOfDate =
    DATE ( 2021, 12, 31 )
    RETURN
    CALCULATE (
    SUMX (
    VALUES ( 'Lease_Unit'[Unit Reference] ),
    LASTNONBLANKVALUE (
    'Lease_Unit'[Commencement Date],
    SUM ( 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
    )

    • julesdude's avatar
      julesdude
      Post Partisan

      Hi wdx223_Daniel 

      Thank you so much for coming back!

      I was just checking though but it is not working for the date against the below asset's data. Based on the rules, With an as of date of 31/12/2022 I would expect a total Leased Area for this asset to be 27,309 but it is returning a 45,476 and I can't figure out why:

      Asset Reference    Unit Reference  Leased Area  Status    Commencement Date    Expiration Date   Termination Date
      A100  030  9142  Active  20-Feb-18  19-Feb-33   
      A100  020    9142  Active  26-Feb-18  25-Feb-28   
      A100  010    9025  Active  20-Feb-18  19-Feb-33   
      • wdx223_Daniel's avatar
        wdx223_Daniel
        Community Champion

        because in the grand total, there are two values at 20-Feb-18, and lastnonblankvalue did not get the filters comes from unit reference, so, 9142 and 9025 was calculated twice.

        please try this code

        =
        VAR asOfDate =
            DATE ( 2021, 12, 31 )
        RETURN
            CALCULATE (
                SUMX (
                    VALUES ( 'Lease_Unit'[Unit Reference] ),
                    LASTNONBLANKVALUE (
                        'Lease_Unit'[Commencement Date],
                        CALCULATE ( SUM ( 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
            )