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! Learn more

Reply
Anonymous
Not applicable

Calculate difference between values in same column

VLoyen_1-1706786853408.png

 

I need to calculate the difference in mileages and quantity, to calculate the fuel consumption per vehicle and date. Can someone please help me? 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

I found the solution! 

 

thought I'd share it with you, sice you've all been so helpfull. 

Gereden km's tussen tankbeurten =
VAR CurrentVehicle = 'F: Tankgegevens'[Voertuignummer]
VAR CurrentDate = 'F: Tankgegevens'[Datum]
VAR CurrentKM = 'F: Tankgegevens'[KM-stand]

VAR PreviousKM =
CALCULATE(
MAX('F: Tankgegevens'[KM-stand]),
FILTER(
'F: Tankgegevens',
'F: Tankgegevens'[Voertuignummer] = CurrentVehicle &&
'F: Tankgegevens'[Datum] < CurrentDate
)
)

RETURN
IF(
NOT ISBLANK(PreviousKM),
CurrentKM - PreviousKM,
BLANK()
)

View solution in original post

8 REPLIES 8
Anonymous
Not applicable

I found the solution! 

 

thought I'd share it with you, sice you've all been so helpfull. 

Gereden km's tussen tankbeurten =
VAR CurrentVehicle = 'F: Tankgegevens'[Voertuignummer]
VAR CurrentDate = 'F: Tankgegevens'[Datum]
VAR CurrentKM = 'F: Tankgegevens'[KM-stand]

VAR PreviousKM =
CALCULATE(
MAX('F: Tankgegevens'[KM-stand]),
FILTER(
'F: Tankgegevens',
'F: Tankgegevens'[Voertuignummer] = CurrentVehicle &&
'F: Tankgegevens'[Datum] < CurrentDate
)
)

RETURN
IF(
NOT ISBLANK(PreviousKM),
CurrentKM - PreviousKM,
BLANK()
)

Ashish_Mathur
Super User
Super User

Hi,

Share the download link of the PBI file.


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
Anonymous
Not applicable

Hi @Anonymous ,

Please follow these steps:
1. For the first column of your expected result, you can create the metric and write the following expression.

diff =

VAR _date = SELECTEDVALUE('Table'[date ])

VAR _maxdate = CALCULATE(LASTNONBLANK('Table'[date ],CALCULATE(SUM('Table'[mileage]))),FILTER(ALLSELECTED('Table'),'Table'[vehicle] = MAX('Table'[vehicle]) && 'Table'[date ] < _date))

VAR _per = IF(NOT(ISBLANK(SUM('Table'[mileage]))),

CALCULATE(SUM('Table'[mileage]),FILTER(ALLSELECTED('Table'),'Table'[date ] = _maxdate)),

BLANK())

RETURN

IF(_per = BLANK(),BLANK(),MAX('Table'[mileage]) - _per)

2.The result obtained is shown below.

vkaiyuemsft_0-1707096364930.png

For the second column of your expected result, I'm not really sure how I got it, I tried to calculate it in several ways, but all of them are different from your expected result, could you please describe again in detail how to get this result.

If your Current Period does not refer to this, please clarify in a follow-up reply.

 

Best Regards,

Clara Gong

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Anonymous
Not applicable

At VAR _per i receive the following error:The syntax for 'VAR' is incorrect. (DAX(VAR _date = SELECTEDVALUE('F: Tankgegevens'[Datum])VAR _maxdate = CALCULATE(LASTNONBLANK('F: Tankgegevens'[Datum],CALCULATE(SUM('F: Tankgegevens'[KM-stand]))),FILTER(ALLSELECTED('F: Tankgegevens'),'F: Tankgegevens'[Voertuignummer] = MAX('F: Tankgegevens'[Voertuignummer] && 'F: Tankgegevens'[Datum] < _date))VAR _per = IF(NOT(ISBLANK(SUM('F: Tankgegevens'[KM-stand]))),CALCULATE(SUM('F: Tankgegevens'[KM-stand]),FILTER(ALLSELECTED('F: Tankgegevens'),'F: Tankgegevens'[Datum] = _maxdate)),BLANK())RETURNIF(_per = BLANK(),BLANK(),MAX('F: Tankgegevens'[KM-stand]) - _per))). 

BIswajit_Das
Super User
Super User

Hello @Anonymous  can you provide an example of your expected result.

Anonymous
Not applicable

vehicledate quantitymileagediff in mileageconsumption 
837428/09/202346,699212   
83742/10/202329,069960539313,52374398 
83745/10/202330,099997036512,13027584 
       

first, I need the difference in mileage (per vehicle number!/date), so i can make a measure for the consumption (difference in mileage/quantity). 


Is that enough info?

Please try the below measure, change the table name accordingly.

 

Difference in Mileage = VAR Off_ = CALCULATE(SUM('Table (2)'[mileage]),OFFSET(-1,ALLSELECTED('Table (2)'[date ]),ORDERBY('Table (2)'[date ])))
VAR Cur_ = CALCULATE(SUM('Table (2)'[mileage]),OFFSET(0,ALLSELECTED('Table (2)'[date ]),ORDERBY('Table (2)'[date ])))

Return IF(ISBLANK(Off_),BLANK(),  Cur_ - Off_)

 

A detailed video on this functions are on my channel, do check it out.

 

New Dax Functions in PowerBI | Index, Offset & Window | MiTutorials (youtube.com)

 

Regards

Ismail 

Anonymous
Not applicable

The measure doesn't give any errors, but it doesn't work. 

Helpful resources

Announcements
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!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

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