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
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.


Go to My LinkedIn Page


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.


Go to My LinkedIn Page


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.


Go to My LinkedIn Page


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.


Go to My LinkedIn Page


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
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.