Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
Nicci
Helper I
Helper I

Get total miles driven in a table

Hi Everyone,

I have a table in power bi which is rows for each driving trip users enter. They only enter the ending mileage of the car. What I need to do is show in the table how many total miles driven based on taking the ending trip mileage and subtracting it from the last ending trip entered for that car.

 

Here is the table so far for 1 vehicle. As you can see you can have multiple trips entered on the same day. So if I look at the second one in the list the miles driven should be 22 because 114319 the ending mileage - the ending mileage of the trip before it is 22 miles driven. How would I do this?

 

Nicci_0-1715010330777.png

 

2 ACCEPTED SOLUTIONS

Here is one example using a measure. Frankly, this could be a calculated column too.

 

lbendlin_2-1715086313405.png

 

 

View solution in original post

I think a calculated column can solve a lot of your issues.

 

Try this:

 

Eff. Mileage = 
var em = [Ending Mileage]
var veh = [VehicleID]
var pem = maxx(filter(all(Mileage),[VehicleID]=veh && [Ending Mileage]<em),[Ending Mileage])
return em-COALESCE(pem,RELATED(Vehicles[StartingMileage]))

lbendlin_0-1715090515433.png

 

View solution in original post

10 REPLIES 10
lbendlin
Super User
Super User

You would not be able to compute that for the first day (3/6/2024).  For all others you would need to assume that trips do never go overnight?

I have the starting mileage for the vehicle elsewhere so i can calculate that for the first day once I know how to calculate in general for all of these. They do not go overnight, but since you can have multiple trips in a day and the data is in sharepoint I figured instead of using the date since there are duplicates you can use the row ID that sharepoint creates to know the 'last' one. I'm just struggling on how to actually do the calculated DAX to say give me the last one's ending to use as the starting and subtract from the ending of that records.

You can sort trips by Ending Mileage (barring any odometer manipulations).

 

Please provide sample data that covers your issue or question completely, in a usable format (not as a screenshot).

Do not include sensitive information or anything not related to the issue or question.

If you are unsure how to upload data please refer to https://community.fabric.microsoft.com/t5/Community-Blog/How-to-provide-sample-data-in-the-Power-BI-...

Please show the expected outcome based on the sample data you provided.

Want faster answers? https://community.fabric.microsoft.com/t5/Desktop/How-to-Get-Your-Question-Answered-Quickly/m-p/1447...

Hello, Here you go!

Mileage List data:

IDDateVehicle Plate #VehicleIDEnding Mileage
3602/1/2024123451359,098
3612/1/2024123451359,102
3642/2/2024123451359,107
3652/5/2024123451359,112
3662/5/2024123451359,179
3692/5/2024123451359,224
3712/6/2024123451359,307
3952/6/2024123451359,350
3972/6/2024123451359,373
3992/9/2024123451359,437
4012/14/2024123451359,519
4032/14/2024123451359,572
4112/15/2024123451359,665
4132/15/2024123451359,689
4152/15/2024123451359,732
4182/20/2024123451359,805
4202/20/2024123451359,847
4212/21/2024123451359,908
4282/27/2024123451359,986
4312/28/2024123451360,098
4332/28/2024123451360,134
4352/29/2024123451360,289
4362/29/2024123451360,302
4613/4/2024123451360,431
4923/8/2024111119526,317
4933/6/2024111119526,246
5263/18/2024111119526,634.80
5313/18/2024111119526,652.80
5393/15/2024111119526,619
5403/11/2024111119526,391.80
5523/19/2024111119526,728.60

 

 

Vehicle Registration Data:

IDPlate #StartingMileage
131234555637
951111126233

 

Expected outcome:

 

I'm trying to build a table in power bi that lists all the mileage just like the first data list but I also want a column that totals how many miles the driver drove for that trip. Each row in the first table represents a trip they drove with a car. 

 

For the first record where there is no 'earlier trip' in the data then you should use the 'startingmileage' from the vehicle registration to know the start mileage before any trips were documented, otherwise use the last entry for the vehicle based on vehicle ID.

NOTE: there are multiple months in the real data, so if its 4/1/2024 trip entered and there exists a 3/31/24 record trip use that, otherwise if it doesnt exist at all, use the starting mileage for the very first in the database.

 

So some math examples for the table should look like this:

IDDateVehicle Plate #VehicleIDEnding MileageTotal Miles
3602/1/2024123451359,0983, 461 (since in the sample there is none before this so we use starting miles in the vehicle reg data)
3612/1/2024123451359,1024
3642/2/2024123451359,1075
3652/5/2024123451359,1125
3662/5/2024123451359,17967


Thank you!

Here is one example using a measure. Frankly, this could be a calculated column too.

 

lbendlin_2-1715086313405.png

 

 

To add, I think its something to do with the dates because in my table i'm listing out the 2 trips individually (which is required because different people drove and we cant combine it so need to know person A drove 27 miles on that day and then later person B drove 32 miles, so cant be combined which I think why yours is working if i combined the display dates together.


I think a calculated column can solve a lot of your issues.

 

Try this:

 

Eff. Mileage = 
var em = [Ending Mileage]
var veh = [VehicleID]
var pem = maxx(filter(all(Mileage),[VehicleID]=veh && [Ending Mileage]<em),[Ending Mileage])
return em-COALESCE(pem,RELATED(Vehicles[StartingMileage]))

lbendlin_0-1715090515433.png

 

That did the trick, THANK YOU !!!!!!!!!!!!!!!!

So I think its close, I am struggling to understand why yours works but when I create a measure and add it to my table its not doing the same thing LOL Here is the crazy numbers its giving me:

Nicci_0-1715089296343.png


The first number 27 is actually correct but thats because its the first and using the starting mileage in the vehicle registration table (yay) but as you can see the numbers just keep multiplying up and up. The second number, 59, is incorrect, it should be 32 and something I found that if you add 32 plus the mileage before (27) it equals 59, but no idea why that is. Is it possible its becuase of the dates being the same so its calculating and adding twice?

Same with the third number, it should be 117, but its giving 172 which is the 117 plus the total before it (59) thats displayed. Aaaaaa LOL

A calculated column would be ideal then I can do like averages for that vehicle if its a column 🙂 Let me take a look thank you!

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

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