cancel
Showing results for
Did you mean:

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a \$100 discount. Register Now

Helper I

## DAX for difference between each value (km) and its previous value (km) per selection

Hi,

Please help? I really struggle to create dax to accomplish the following:

For  each vehicle many jobs are performed at various mileage intervals.  Each time a job is performed, the mileage (Meter KM) is captured. Below in the table you can see the mileage when different jobs in the JOB column were performed at various dates for one selected vehicle. (There are of course many more vehicles.)

Question: I don't want to display the actual mileage when each job was performed as you can see in the below table, but rather the difference between each value (km) and its previous value (km) for each job.  This is to see clearly the interval in which the jobs were performed. This means e.g. for the first Job 06-02-005, 2021 June should display (147,584-136,011)= 11,573, and for 2022 July (162,981-147,584=) 15,397.

All the data is captured in the same WO WITH JOB DETAIL table. Here sample data from this table: Sample dataset . The data captured in Power BI is here.

You will note the interval between the various jobs differs.

One would typically filter on a specific vehicle to monitor service intervals for its many jobs.

With thanks,

Water

1 ACCEPTED SOLUTION
Community Support

Hi @Water ,

You can update the formula of measure [Value diff] as below and check if it can return the correct result...

``````Value diff =
VAR _date =
MAX ( 'WO_JOBS'[JOB_OPEN_DATE_USE] )
VAR _predate =
CALCULATE (
MAX ( 'WO_JOBS'[JOB_OPEN_DATE_USE] ),
FILTER ( ALLSELECTED ( 'WO_JOBS' ), 'WO_JOBS'[JOB_OPEN_DATE_USE] < _date )
)
VAR _lastValue =
CALCULATE (
MAX ( 'WO_JOBS'[Meter_KM] ),
FILTER ( ALLSELECTED ( 'WO_JOBS' ), 'WO_JOBS'[JOB_OPEN_DATE_USE] = _predate )
)
VAR _value =
SUM ( 'WO_JOBS'[METER_KM] )
RETURN
IF ( ISBLANK ( _lastValue ), BLANK (), _value - _lastValue )``````

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
5 REPLIES 5
Super User

Hi,

Here is one way to do this:

Data:

Dax: (add calendar table to your model if you don't have one)

Value diff =
var _date = MAX('Table'[Date])
Var _lastValue = CALCULATE(MAX('Table'[value]),ALL('Calendar'),'Calendar'[Date]<_date)
var _value = MAX('Table'[value]) return
IF(ISBLANK(_lastValue),BLANK(),_value-_lastValue)

End result:

I hope this post helps to solve your issue and if it does consider accepting it as a solution and giving the post a thumbs up!

Did I answer your question? Mark my post as a solution!

Proud to be a Super User!

Helper I

Hi,

Thank you again for your help! I am very excited about your solution!

Picked up just one issue as below:

If a previous km entry was (admittedly incorrect) as below (the 5580 for Dec 2021), the resultant difference in this instance should be 101,788 - 5,580 = 96,208.

However, you will see in the below screenshot, using your DAX it displays (incorrectly) as 14,338, which looks like it comes from 96,208-81,870.

Question: Is there an update to the DAX possible to get rid of this small error?

The Power BI file is here, and the Excel dataset here.

(Footnote: Sorry for the slow reply. I am still learning and had to spend time to better understand the date table. Could not initially get your solution to work as I had incompatible date formats. Also, did not know about the about the "Sort by column" option.  Now that I understand this better, your DAX seems to work great (other than just the little issue above!)

Thanks and best regards,

W

Community Support

Hi @Water ,

You can update the formula of measure [Value diff] as below and check if it can return the correct result...

``````Value diff =
VAR _date =
MAX ( 'WO_JOBS'[JOB_OPEN_DATE_USE] )
VAR _predate =
CALCULATE (
MAX ( 'WO_JOBS'[JOB_OPEN_DATE_USE] ),
FILTER ( ALLSELECTED ( 'WO_JOBS' ), 'WO_JOBS'[JOB_OPEN_DATE_USE] < _date )
)
VAR _lastValue =
CALCULATE (
MAX ( 'WO_JOBS'[Meter_KM] ),
FILTER ( ALLSELECTED ( 'WO_JOBS' ), 'WO_JOBS'[JOB_OPEN_DATE_USE] = _predate )
)
VAR _value =
SUM ( 'WO_JOBS'[METER_KM] )
RETURN
IF ( ISBLANK ( _lastValue ), BLANK (), _value - _lastValue )``````

Best Regards

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

Hi,

Thank you very much for trying to help!

Unfortunately this is correcting that one issue, but now most of the other figures are wrong.

Only the figures in red below are correct. All the others are now incorrect. With the previous DAX version higher up in this post all figures except that one was correct (see post above).

If we can get a DAX that is working perfectly, the output will look like the below screenshot I made in Excel.

Is there maybe something else you could try?

With hope,

Water

Helper I

Hi,

Thank you so much for trying to help! Really appreciate it!

I added a calendar/date table and created your measure, but unfortunately it seems not to work?

Would you mind to please have a look at this pbix file with a small subset of data here? Not sure if it has to do with how my date table connects to the data table, but the selecting any date/year from the calender table produces absolute nothing to display over any time span selected.

Thanks again!

W

Announcements

#### Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

#### Power BI Monthly Update - February 2024

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

#### Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

#### Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors
Top Kudoed Authors