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
rahul632soni
Helper I
Helper I

How to Get Difference Between Prev value and Current Value

Hey Folks !!

 

I have a Table whihc consist following Column:-

VIN (Serial Number of Vehicle)

ENG_HOURS - Engine Hours ( Cumulative Number as per Timestamp)

GPS_Timestamp ( Time and Date when Data Recorded)

 

I want to Calculate Total Engine Hours For Each VIN over the Selected Pewriod of Time , 

I am Trying to Create a New Column with the Difference of Current Value and Prev valaue for Engine Hours and Then Taking SUM of That Calculated Column to get Exact Engine Hours , I did That in Pyspark , Can we do somethoing in Power BI Directly.

 

This is The Pyspark COde which i use:-

 

# Sort the dataframe by VIN and timestamp
nn2 = nn2.orderBy('vin', 'gps_timestamp')

# Calculate the difference in engine hours between consecutive timestamps for each VIN
nn2 = nn2.withColumn('prev_engine_hours', lag('ENG_HOURS').over(
Window.partitionBy('vin').orderBy('gps_timestamp')
))

# Calculate the exact engine hours for each VIN at each timestamp
nn2 = nn2.withColumn('exact_engine_hours', when(
col('prev_engine_hours').isNull(), 0.0
).otherwise(
col('ENG_HOURS') - col('prev_engine_hours')
))

 

 

Regards

Rahul 

1 REPLY 1
amitchandak
Super User
Super User

@rahul632soni , earlier in DAX can help to take diff

 

new column=

var _max = maxx(filter(Table, [VIN] = earlier([Vin]) && [GPS_Timestamp] < earlier([GPS_Timestamp]) ), [GPS_Timestamp])

return

[ENG_HOURS] - maxx(filter(Table, [VIN] = earlier([Vin]) && [GPS_Timestamp] =_max ), [ENG_HOURS]) 

 

Power BI DAX- Earlier, I should have known Earlier: https://youtu.be/CVW6YwvHHi8

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

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
Top Kudoed Authors