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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
sthaya
Frequent Visitor

Calculate Miles Driven per Vehicle

Hi Power BI experts,

 

Below is a screenshot of a filtered Report. I would like to see how many miles driven between each gas fill-up. But I would like the formula to be by vechicle ID. In my screenshot below, this driver used 4 different vechicles in June. Is there a way for me to calculate in the "miles driven" column - the difference in the Mileage of each fill-up? So in row 2 of my screenshot under "Miles Driven", it should be 357,737 - 357,411. Row 3 should be 358,034 - 357,737 etc.

BUT, in row 4, since he used a different vehicle, that row should be zero or blank. Then in row 5, it should be 338,638 - 338,333.

 

Is what I am asking for possible?

 

NOTE: For the Miles Driven column, I used this formula to give me 133,277:

Miles Driven = SUMX ( VALUES ( 'Gas Data'[Card #] ), CALCULATE ( MAX ( 'Gas Data'[Mileage] ) - MIN ( 'Gas Data'[Mileage] ), NOT ISBLANK ( 'Gas Data'[Mileage] ) ) )

 screenshot6.png

 

Thank you

5 REPLIES 5
v-danhe-msft
Microsoft Employee
Microsoft Employee

Hi @sthaya,

Based on my test, you could refer to below steps:

Sample data:

1.PNG

Create three measures:

Index = RANKX(ALL(Table1),FIRSTNONBLANK(Table1[Date],Table1[Date]),,ASC,Dense)
Difference = var a=[Index]-1
var b=CALCULATE(MAX('Table1'[Mileage]),FILTER(ALL('Table1'),'Table1'[Index]=a))
return MAX([Mileage])-b
Filtered difference = var a=[Index]-1
var b= CALCULATE(max('Table1'[Vehicle]),FILTER(ALL('Table1'),'Table1'[Index]=a))
return IF(b=MAX([Vehicle]),[Difference],0)

Result:

1.PNG

 

You could also download the pbix file to have a view:

https://www.dropbox.com/s/kb31rwaipnhr5ji/Calculate%20Miles%20Driven%20per%20Vehicle.pbix?dl=0

 

Regards,

Daniel He

 

Community Support Team _ Daniel He
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Daniel,

 

thank you for your reply. I copied your measures and adjusted what I needed. Here are the 3 measures I used:

 

Index = RANKX(ALL('Gas Data'),FIRSTNONBLANK('Gas Data'[Date],'Gas Data'[Date]),,ASC,Dense)

 

 

Difference = var a=[Index]-1
var b=CALCULATE(MAX('Gas Data'[Odometer]),FILTER(ALL('Gas Data'),'Gas Data'[Index]=a))
return MAX([Odometer])-b

 

 

Miles Traveled = var a=[Index]-1
var b= CALCULATE(max('Gas Data'[Vehicle ID]),FILTER(ALL('Gas Data'),'Gas Data'[Index]=a))
return IF(b=MAX([Vehicle ID]),[Difference],0)

 

 

Here is the results when I included 2 of those measures ("Difference" and "Miles Traveled")

screenshot7.png

and here is the results when I also include the "Index" measure:

screenshot8.png

It looks like it's pulling in all the data, but using the same driver name for every row. What are your thoughts on this?

Hi @sthaya,

I noticed when you first post me the picture, the data is ordered by the date:

1.PNG

So, you could notice my measure with index, it ranked by the date.

When you post me the latest picture, I noticed the data you have ordered by the [Gallons Total]:

2.PNG

 

I have tested it, when I ordered the data by [Date] column, then create the three measures, it could work on my side:

3.PNG

In short, I suggest you to order your data by the [Date] column firstly, then you could try to create the three measures to test if it could work.

 

Regards,

Daniel He

 

Community Support Team _ Daniel He
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Daniel,

 

here's what it looks like when I sort by Date:

 

screenshot9.png

All,

I'm new to PowerBI and DAX, so forgive me if the previous posts solved this problem but I still need some help.

I have a table with several different vehicles, [EquipmentID].  I need to be able to filter the table by equipment and come up with a 'Miles per Gallon' for each row.  If there is anyone who could throw me a DAX formula for this problem I would greatly appreciate it!

Thanksscreenshot.jpg

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.