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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
Anonymous
Not applicable

DAX Help - Need to find a previous row value, following a criteria

I need help in two  DAX formulas that would bring a previous row value for a certain column based on a filter criteria.

 

The formula would be for PrevFinish =  ...................................

It would consider and Repeat the Finish_Downtime for the first Index column [ID], obeying the criteria that it belongs to the same MACHINE_ID.

 

It could also be solved if we have a formula for the Rows_Above = ....................

What is the ID difference between the current row ID and the first Row above that also belongs to the same Machine_ID

 

IDMACHINE_IDFinish_DowtimePrevFinishRows_Above
289015/27/2021 10:545/27/2021 6:28 
289125/27/2021 9:595/27/2021 10:42 
289225/27/2021 10:495/27/2021 9:59    -1
289325/27/2021 11:045/27/2021 10:49    -1
289415/27/2021 11:235/27/2021 10:54    -4
289525/27/2021 11:435/27/2021 11:04    -2
289625/27/2021 12:395/27/2021 11:43    -1
289715/27/2021 14:305/27/2021 11:23    -3
289925/27/2021 14:255/27/2021 12:39    -2
 
 
 
 

 

Thanks for any help.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3 ACCEPTED SOLUTIONS
Jihwan_Kim
Super User
Super User

Hi, @Anonymous 

Please check the below for creating new measures.

 

Picture2.png

 

Prev Finish Measure =
VAR rankbyfinishtime =
RANKX (
ALLEXCEPT ( 'Table', 'Table'[MACHINE_ID] ),
CALCULATE ( MAX ( 'Table'[Finish_Dowtime] ) ),
,
DESC
)
VAR prevtime =
CALCULATE (
MAX ( 'Table'[Finish_Dowtime] ),
FILTER (
ALLEXCEPT ( 'Table', 'Table'[MACHINE_ID] ),
RANKX (
ALLEXCEPT ( 'Table', 'Table'[MACHINE_ID] ),
CALCULATE ( MAX ( 'Table'[Finish_Dowtime] ) ),
,
DESC
) > rankbyfinishtime
)
)
RETURN
IF ( ISFILTERED ( 'Table'[ID] ), prevtime )

 

 

Rows Above Measure =
VAR currentID =
MAX ( 'Table'[ID] )
VAR rankbyfinishtime =
RANKX (
ALLEXCEPT ( 'Table', 'Table'[MACHINE_ID] ),
CALCULATE ( MAX ( 'Table'[Finish_Dowtime] ) ),
,
DESC
)
VAR prevID =
CALCULATE (
MAX ( 'Table'[ID] ),
FILTER (
ALLEXCEPT ( 'Table', 'Table'[MACHINE_ID] ),
RANKX (
ALLEXCEPT ( 'Table', 'Table'[MACHINE_ID] ),
CALCULATE ( MAX ( 'Table'[Finish_Dowtime] ) ),
,
DESC
) > rankbyfinishtime
)
)
RETURN
IF ( ISFILTERED ( 'Table'[ID] ) && prevID > 0, prevID - currentID, "" )
 
 
 
 

Hi, My name is Jihwan Kim.


If this post helps, then please consider accept it as the solution to help other members find it faster, and give a big thumbs up.


Linkedin: linkedin.com/in/jihwankim1975/

Twitter: twitter.com/Jihwan_JHKIM


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

View solution in original post

Anonymous
Not applicable

The solution works Jihwan, but they come as a Measure - what should I change in those same DAX formulas for them to work as a new column and not a Measure?

 

regards,

Leonardo

 

View solution in original post

Hi, @Anonymous 

Please check the below.

 

Picture1.png

 

PrevFinish CC =
VAR currentfinishtime = 'Table'[Finish_Dowtime]
VAR prevfinishtime =
CALCULATE (
MAX ( 'Table'[Finish_Dowtime] ),
FILTER (
ALLEXCEPT ( 'Table', 'Table'[MACHINE_ID] ),
'Table'[Finish_Dowtime] < currentfinishtime
)
)
RETURN
prevfinishtime
 
Rows_Above CC =
VAR currentfinishtime = 'Table'[Finish_Dowtime]
VAR prevfinishtime =
CALCULATE (
MAX ( 'Table'[Finish_Dowtime] ),
FILTER (
ALLEXCEPT ( 'Table', 'Table'[MACHINE_ID] ),
'Table'[Finish_Dowtime] < currentfinishtime
)
)
VAR prevID =
CALCULATE (
MAX ( 'Table'[ID] ),
FILTER (
ALLEXCEPT ( 'Table', 'Table'[MACHINE_ID] ),
'Table'[Finish_Dowtime] = prevfinishtime
)
)
RETURN
IF ( NOT ISBLANK ( 'Table'[PrevFinish CC] ), prevID - 'Table'[ID] )
 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.

View solution in original post

4 REPLIES 4
Jihwan_Kim
Super User
Super User

Hi, @Anonymous 

Please check the below for creating new measures.

 

Picture2.png

 

Prev Finish Measure =
VAR rankbyfinishtime =
RANKX (
ALLEXCEPT ( 'Table', 'Table'[MACHINE_ID] ),
CALCULATE ( MAX ( 'Table'[Finish_Dowtime] ) ),
,
DESC
)
VAR prevtime =
CALCULATE (
MAX ( 'Table'[Finish_Dowtime] ),
FILTER (
ALLEXCEPT ( 'Table', 'Table'[MACHINE_ID] ),
RANKX (
ALLEXCEPT ( 'Table', 'Table'[MACHINE_ID] ),
CALCULATE ( MAX ( 'Table'[Finish_Dowtime] ) ),
,
DESC
) > rankbyfinishtime
)
)
RETURN
IF ( ISFILTERED ( 'Table'[ID] ), prevtime )

 

 

Rows Above Measure =
VAR currentID =
MAX ( 'Table'[ID] )
VAR rankbyfinishtime =
RANKX (
ALLEXCEPT ( 'Table', 'Table'[MACHINE_ID] ),
CALCULATE ( MAX ( 'Table'[Finish_Dowtime] ) ),
,
DESC
)
VAR prevID =
CALCULATE (
MAX ( 'Table'[ID] ),
FILTER (
ALLEXCEPT ( 'Table', 'Table'[MACHINE_ID] ),
RANKX (
ALLEXCEPT ( 'Table', 'Table'[MACHINE_ID] ),
CALCULATE ( MAX ( 'Table'[Finish_Dowtime] ) ),
,
DESC
) > rankbyfinishtime
)
)
RETURN
IF ( ISFILTERED ( 'Table'[ID] ) && prevID > 0, prevID - currentID, "" )
 
 
 
 

Hi, My name is Jihwan Kim.


If this post helps, then please consider accept it as the solution to help other members find it faster, and give a big thumbs up.


Linkedin: linkedin.com/in/jihwankim1975/

Twitter: twitter.com/Jihwan_JHKIM


If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.
Anonymous
Not applicable

The solution works Jihwan, but they come as a Measure - what should I change in those same DAX formulas for them to work as a new column and not a Measure?

 

regards,

Leonardo

 

Hi, @Anonymous 

Please check the below.

 

Picture1.png

 

PrevFinish CC =
VAR currentfinishtime = 'Table'[Finish_Dowtime]
VAR prevfinishtime =
CALCULATE (
MAX ( 'Table'[Finish_Dowtime] ),
FILTER (
ALLEXCEPT ( 'Table', 'Table'[MACHINE_ID] ),
'Table'[Finish_Dowtime] < currentfinishtime
)
)
RETURN
prevfinishtime
 
Rows_Above CC =
VAR currentfinishtime = 'Table'[Finish_Dowtime]
VAR prevfinishtime =
CALCULATE (
MAX ( 'Table'[Finish_Dowtime] ),
FILTER (
ALLEXCEPT ( 'Table', 'Table'[MACHINE_ID] ),
'Table'[Finish_Dowtime] < currentfinishtime
)
)
VAR prevID =
CALCULATE (
MAX ( 'Table'[ID] ),
FILTER (
ALLEXCEPT ( 'Table', 'Table'[MACHINE_ID] ),
'Table'[Finish_Dowtime] = prevfinishtime
)
)
RETURN
IF ( NOT ISBLANK ( 'Table'[PrevFinish CC] ), prevID - 'Table'[ID] )
 

If this post helps, then please consider accepting it as the solution to help other members find it faster, and give a big thumbs up.


Click here to visit my LinkedIn page

Click here to schedule a short Teams meeting to discuss your question.
Anonymous
Not applicable

Jihwan,
It worked well, most of my parameters are in Columns and formulas with Measures and Columns can always be trick.

Have a good week,

Leonardo

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors