Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
julesdude
Post Partisan
Post Partisan

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?

1 ACCEPTED SOLUTION

wdx223_Daniel_0-1676515623994.png

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
    )

View solution in original post

9 REPLIES 9
wdx223_Daniel
Super User
Super User

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
)

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   

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
    )

@wdx223_Daniel 

This is beyond brilliant, so thank you.

Of the 20 assets I've tested this against, I am now getting the correct Leased Area for all but one.

Let me just unpick that a little to see where the issue lies (looks like just one unit reference) and I'll post back in a few hours. 

Thanks again for the skilled approach.

@wdx223_Daniel 

Hi Daniel, there was just one case I found so far where the solution doesn't quite handle well.

Sample dataset:

Asset Reference  Unit Reference  Leased Area  Status  Commencement Date  Expiration Date  Termination Date  
A200BR470 Active11-Oct-2110-Oct-22 
A200BR470 Active05-Jan-2110-Oct-21 
A200BR470 Active11-Nov-2024-Dec-20 
A200BR470 Active02-Jul-2201-Jul-3003-Jul-22
A200BR88051400Active28-Jan-2127-Jan-36 
A200BR880 Active03-Dec-2027-Jan-21 
A200BR52018295Active04-Jan-2103-Jan-24 
A200BR520 Terminated03-Dec-2002-Dec-3003-Jan-21
A200BR600115869Active24-Jun-2123-Jun-23 
A200BR600 Active01-Jul-2230-Jun-23 
A200BR600 Active03-Dec-2023-Jun-21 

In this instance, the last Unit Reference (bottom three rows) BR600 causes the issue. It has three entries. The logic above chooses the item with a Leased Area of 115869 and sums this as part of the total Leased Area giving a total of 185,564. However, the item BR600 has other entries. If we have an [As Of Date] of 31/12/2022, this should actually qualify two items (Commencement Date 24-Jun-21 to Expiration Date 23-Jun-23 and the item in row underneath Commencement Date 01-Jul-22 to Expiration Date 30-Jun-23). It should only ever qualify one row per unit reference. In this case for BR600, as there are three rows for it and two which qualify within the Commencement and Expiration dates, the Leased Area should always be taken for the row where the Commencement Date is closest to the As Of Date AND is in the past, even if the Leased Area is blank and there is nothing to total.

So for the above, the total I should be getting is 69,695 (51400 + 18295) for this asset A200

wdx223_Daniel_0-1676515623994.png

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
    )

Hi @wdx223_Daniel 

Yes there was actually no typo, just odd data from the system I'm extracting from unfortunately. Sometimes date commencement and expiration overlap with commencement and expiration dates against the same unit reference. It is just how it is.

Thanks for the amended solution. It corrected a number of additional totals I was expecting. However it is not totalling correctly for the below asset reference

Asset Reference  Unit Reference  Leased Area  StatusCommencement DateExpiration DateTermination DateSUM?
C16104401Active04-Jul-1603-Jul-31 Y
C16205309Active28-Nov-1627-Nov-41 Y
C16304447Active26-Nov-2125-Nov-36 Y
C16304447Terminated04-Nov-1603-Nov-2612-Jan-21 
C1630 Active13-Jan-2131-Dec-99  
C16401331Active30-Sep-2029-Sep-30 Y
C16401331Terminated13-Apr-1712-Apr-3229-Sep-20 
C16505595Active16-Dec-1615-Dec-24 Y

 The total Leased Area for this one should be 21083 but I am getting 16636. I have created a column for the sample above to indicate the rows that qualify for summing based on the rules above.
The item that is being left out but that should be included is the item 3rd row down from the top.

Hi @wdx223_Daniel 

Thankfully in the end it was a small reference adjustment to call on the order of the Commencement Date column rather than the Expiration Date in the TOPN function:

 

=
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'[Commencement 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
    )

 

I think we can tie up the thread nicely. Thanks for all your help.

Just a question on the benefit of using the COALESCE line rather than putting in its place:

 

        OR( 
            Lease_Unit[Termination Date] >= _asOfDate, 
        isblank(Lease_Unit[Termination Date])
        )

 

Your method looks cleaner, shorter. I was wondering if there were other benefits.

i think it just improves code-readability. more information about this function you can read this artical

https://www.sqlbi.com/articles/the-coalesce-function-in-dax/

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.