Forum Discussion
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
- Anonymous2 years ago
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
5 Replies
- ValtteriN
Community Champion
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]) returnIF(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!
My LinkedIn: https://www.linkedin.com/in/n%C3%A4ttiahov-00001/- Water
Helper II
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
- AnonymousNot applicable
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
- Water
Helper II
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