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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
julesdude
Post Partisan
Post Partisan

DAX column required to return previous value based on second to last date

Hi,

I have a table a little like this:

Asset Reference  Valuation Date  Valuation Amount
aaa12/08/2022100
aaa12/04/2021200
bbb22/02/2021300
bbb15/09/2022400

I am trying to apply a DAX column that will take the asset reference for the row and the valuation date, and display the valuation amount for the previous valuation date for that asset reference.

I have this:

 

Previous Valuation = 
VAR CurrentAssetRef = Asset_Valuation[Asset Reference]
VAR CurrentValuationDate = Asset_Valuation[Valuation Date]
VAR secondtolastdate = CALCULATE(MAX(Asset_Valuation[Valuation Date]), Asset_Valuation[Asset Reference] = CurrentAssetRef, Asset_Valuation[Valuation Date] < CurrentValuationDate)
RETURN
CALCULATE (
    MAX (Asset_Valuation[Asset Value] ),
    Asset_Valuation[Asset Reference] = CurrentAssetRef,
    Asset_Valuation[Valuation Date] = secondtolastdate
)

 

However, this is returning a column with blank values, and I am not sure why.

I have adapted to include the ALL() command after the MAX statement, but this returns blanks also.

Not sure what I am doing wrong but any help appreciated.

1 ACCEPTED SOLUTION
johnt75
Super User
Super User

You can use

Previous Valuation = 
VAR CurrentAssetRef = Asset_Valuation[Asset Reference]
VAR CurrentValuationDate = Asset_Valuation[Valuation Date]
VAR secondtolastdate =
    CALCULATE (
        MAX ( Asset_Valuation[Valuation Date] ),
        ALLEXCEPT ( Asset_Valuation, Asset_Valuation[Asset Reference] ),
        Asset_Valuation[Valuation Date] < CurrentValuationDate
    )
RETURN
    CALCULATE (
        MAX ( Asset_Valuation[Valuation Amount] ),
        ALLEXCEPT ( Asset_Valuation, Asset_Valuation[Asset Reference] ),
        Asset_Valuation[Valuation Date] = secondtolastdate
    )

View solution in original post

6 REPLIES 6
johnt75
Super User
Super User

You can use

Previous Valuation = 
VAR CurrentAssetRef = Asset_Valuation[Asset Reference]
VAR CurrentValuationDate = Asset_Valuation[Valuation Date]
VAR secondtolastdate =
    CALCULATE (
        MAX ( Asset_Valuation[Valuation Date] ),
        ALLEXCEPT ( Asset_Valuation, Asset_Valuation[Asset Reference] ),
        Asset_Valuation[Valuation Date] < CurrentValuationDate
    )
RETURN
    CALCULATE (
        MAX ( Asset_Valuation[Valuation Amount] ),
        ALLEXCEPT ( Asset_Valuation, Asset_Valuation[Asset Reference] ),
        Asset_Valuation[Valuation Date] = secondtolastdate
    )

Hi @johnt75 

Thanks for this. Unfortunately still returns blanks.

It works for me, as in the attached. What other columns do you have in the table ?

Hi @johnt75 

My bad - i needed to scroll down a good few rows to start getting the rows where there actually was a previous value available! Your solutions works.

Would you mind a quick comment on what your solution is doing that mine wasn't previously?

The key is the ALLEXCEPT, which removes any filters on the table except for the reference. When you do a CALCULATE from a row context, like you have in a calculated column, every column from the table is used as a filter which means that the valuation itself would be part of the filter, as well as any other columns the table might have.

It also best practice to use ALLEXCEPT in calculated columns because if you add multiple calculated columns you can get a circular dependency error.

Makes sense, Thanks for the explanation and the solution.

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.