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
_n_MarianLein
Helper II
Helper II

Calculate based on earlier values

Hi all,

 

I'm trying to translate a report that was previously in Excel to PowerBI.

In this report, there are a number of columns that are calculated based on other rows, i.e. the row above the actual record.

I've created a dummy dataset with the following raw columns:

DateKM totalKM tripFuel Price per literFuel amount

2020-06-19

489134,21,28934,91
2020-07-041081299,41,20927,87
2020-07-2418442671,23932,87
2020-09-2033591224,91,18934,84
2020-09-273940339,71,18930,91

 

The columns I want to add are:

  • Trip Percentage - [KM trip] / (current [KM total] - last [KM total])
  • Fuel per 100KM - [Fuel amount] / (current [KM total] - last [KM total]) * 100
  • Weighted KM(current [KM total] - last [KM total]) / largest [KM total] in the whole table
  • Days since last refuelingDate difference (from last [Date] to current [Date]) in days

I've marked the fixed values so that they're easier to distinguish.

I've also marked the parts I'm not clear how to execute.

 

If someone could point me to the correct formula, that'd be great. My main problem is: how do I address the last row in the table, i.e. from row 4, how to take a value from row 3, and so on.

1 ACCEPTED SOLUTION
Greg_Deckler
Super User
Super User

@_n_MarianLein See my article on Mean Time Between Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/3395....
The basic pattern is:
Column = 
  VAR __Current = [Value]
  VAR __PreviousDate = MAXX(FILTER('Table','Table'[Date] < EARLIER('Table'[Date])),[Date])

  VAR __Previous = MAXX(FILTER('Table',[Date]=__PreviousDate),[Value])
RETURN
  __Current - __Previous


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

2 REPLIES 2
Greg_Deckler
Super User
Super User

@_n_MarianLein See my article on Mean Time Between Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/3395....
The basic pattern is:
Column = 
  VAR __Current = [Value]
  VAR __PreviousDate = MAXX(FILTER('Table','Table'[Date] < EARLIER('Table'[Date])),[Date])

  VAR __Previous = MAXX(FILTER('Table',[Date]=__PreviousDate),[Value])
RETURN
  __Current - __Previous


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Thanks, this worked!

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.