Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started
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
Solved! Go to Solution.
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
Hi,
Here is one way to do this:
Data:
Dax: (add calendar table to your model if you don't have one)
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!
Proud to be a Super User!
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
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
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
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
Check out the September 2024 Power BI update to learn about new features.
Learn from experts, get hands-on experience, and win awesome prizes.
User | Count |
---|---|
25 | |
19 | |
18 | |
18 | |
15 |
User | Count |
---|---|
38 | |
22 | |
18 | |
15 | |
11 |