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
vrocca
Advocate IV
Advocate IV

Reference previous row to get value

Hi all,

 

I have a dataset with Vehicles, Dates, and Odometer. I'm trying to create a calculation that returns the prior odometer reading for each row. I have followed several examples on the web and they work for the most part, but my issue is that I have some bad data where the odometer readings are incorrect and therefore not incremental (i.e. for 4/1/17 it may read 23,500  and then for 4/5/17 it may read 22,500). 

 

The formula I've been working on looks like this:

 

Previous Odometer := CALCULATE(MAX(Transactions[Odometer #]),ALL(Transactions),Transactions[Vehicle #] = EARLIER(Transactions[Vehicle #]),Transactions[Transaction Date] < EARLIER(Transactions[Transaction Date])  )

 

 

What happens in the scenario where the odometer has a bad reading is that it keeps using that value for all future readings, and throws off the calculations from that point forward.

 

Example:

 

Transaction Date          Odometer#          Previous Odometer

02/08/17                      66770                   66320

02/15/17                      97315 (bad data)  66770                              

02/20/17                      67630                   97315

02/27/17                      68056                   97315   <-- here is my issue. this should read 67630. Value keeps repeating for all rows below after this

03/04/17                      68483                   97315

 

 

Any suggestions on how I need to modify my DAX formula so that I don't get the same value being repeated?

Thanks,
Victor Rocca | www.victorrocca.com
1 ACCEPTED SOLUTION
vrocca
Advocate IV
Advocate IV

I was able to get something to work by creating a Row ID using RANKX, and then another column (Prior Row ID) using EARLIER to get the previous Row ID. I then added this to my original Previous Odometer calculation as an additional filter:

 

Row ID := RANKX( ALL(Transactions[Transaction Date]), T ransactions[Transaction Date], , ASC, Dense)

 

Previous Row ID := CALCULATE

                                      (  

                                       MAX(Transactions[Row ID]),

                                       ALL(Transactions),Transactions[Vehicle #] = EARLIER(Transactions[Vehicle #]),

                                       Transactions[Transaction Date] < EARLIER(Transactions[Transaction Date])

                                      )

 

Previous Odoometer =
CALCULATE(

                    MAX(Transactions[Odometer #]),

                    ALL(Transactions),

                    Transactions[Vehicle #] = EARLIER(Transactions[Vehicle #]),

                    Transactions[Transaction Date] < EARLIER(Transactions[Transaction Date]),

                    Transactions[Row ID] = EARLIER(Transactions[Previous Row ID]) )
)

 

Curious to see if there is a much simpler way to achieve the same results....

Thanks,
Victor Rocca | www.victorrocca.com

View solution in original post

12 REPLIES 12

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.