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

Grow your Fabric skills and prepare for the DP-600 certification exam by completing the latest Microsoft Fabric challenge.

Reply
Anonymous
Not applicable

Calculated columns for last and second last values for a measurement

Hello,

I am working on a project where is needed the last and second last readings for an asset and get the difference between values, so we can make some decisions afteerwards based on the difference. I created two calculated columns, but they are very expensive, especially the secondlastvalue formula. I shared a screenshot with the readings (value), dates (taken_date), where(taken_at_location), reading type (UT Reading Minimum) and asset. Also, the formulas for my last and second last values.

latestvalue =
VAR last_date =
CALCULATE ( MAX ( Readings[taken_date] ), Readings[value] <> BLANK () )
RETURN
CALCULATE (
SELECTEDVALUE ( Readings[value] ),
Readings[taken_date] = last_date
)

secondlastvalue =
VAR second_last_date =
    CALCULATE (
        MAX ( Readings[taken_date] ),
        Readings[value] <> BLANK ()
            && Readings[taken_date] < MAX ( Readings[taken_date] )
    )
RETURN
    CALCULATE (
        SELECTEDVALUE ( Readings[value] ),
        Readings[taken_date] = second_last_date
    )

Cristian1_0-1667918690279.png

You can see that for each location there are two readings (now they are the same, but is not the standard) for two dates. Can somebody take a look at the formulas, maybe there is another way to get those values? 

Much appreciated!

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi daXtreme, I manipulated the formulas to work, it is a bit faster. I like using the lastdate and secondlastdate measures. Thank you for your input.

View solution in original post

4 REPLIES 4
Anonymous
Not applicable

Tried to format it in DAX Formatter and get the error at Return

Cristian1_1-1667929571336.png

 

daXtreme
Solution Sage
Solution Sage

Here are some measures you can try... Measures, not calculated colums.

 

// Hidden helper measure; easier to debug
[_LatestDate] =
var Output =
    CALCULATE(
        MAX( Readings[taken_date] ),
        keepfilters(
            not isblank( Readings[value] )
    )
return
    Output

// Hidden helper measure
[_SecondLatestDate] =
var LatestDate = [_LatestDate]
var Output =
    CALCULATE (
        MAX ( Readings[taken_date] ),
        keepfilters(
            Readings[taken_date] < LatestDate
        )
    )
return
    Output

// This works on the assumption that
// there will only be at most 1 value
// for the LatestDate. If there are
// many, BLANK will be returned since
// this is the behaviour of SELECTEDVALUE.
// Same applies to the second measure.
[LatestValue] =
VAR LastestDate = [_LatestDate]
var Output = 
    CALCULATE(
        SELECTEDVALUE( Readings[value] ),
        keepfilters( 
            Readings[taken_date] = LatestDate
        )
    )
return
    Output
    
[SecondLatestValue] =
VAR SecondLastestDate = [_SecondLatestDate]
var Output =
    CALCULATE (
        SELECTEDVALUE ( Readings[value] ),
        keepfilters(
            Readings[taken_date] = SecondLatestDate
        )
    )
return
    Output
    
[Difference] = [LatestValue] - [SecondLatestValue]

 

Anonymous
Not applicable

Hi daXtreme, I manipulated the formulas to work, it is a bit faster. I like using the lastdate and secondlastdate measures. Thank you for your input.

Anonymous
Not applicable

Hi daXtreme, for some reason I get an error regarding the Return syntax!? 

_LatestDate] =
var Output =
    CALCULATE(
        MAX( Readings[taken_date] ),
        keepfilters(
            not isblank( Readings[value] )
    )
return
    Output

Cristian1_0-1667929369719.png

 

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

RTI Forums Carousel3

New forum boards available in Real-Time Intelligence.

Ask questions in Eventhouse and KQL, Eventstream, and Reflex.

MayPowerBICarousel1

Power BI Monthly Update - May 2024

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