Forum Discussion
Engine Telematics for Distance Travelled - EARLIER function?
Good Day Folks,
Need some assistance here, I think using the Earlier function. I have a dataset of Engine Telematics and Diagnostics.
My first step is to calculate the Distance Travelled the previous day for each Vehicle in the fleet, given the Odometer Reading at the beginning of each Day. An example for one vehicle as follows:
| DateUTC | DeviceName | Odometer | Distance Travelled |
| 31-Mar-22 | HC-06-03 | 428 | |
| 30-Mar-22 | HC-06-03 | 409 | 19 |
| 29-Mar-22 | HC-06-03 | 391 | 18 |
| 28-Mar-22 | HC-06-03 | 389 | 2 |
End Result will be going into a Matrix Visual. So am open to using a Measure or Calculated Column, but preference I think would be a Measure.
Any and all assistance appreciated.
Thanks and Best Regards,
- Anonymous4 years ago
Hi rsbin ,
We will use EARLIER() to catch current value in calculated column. In measure we will use SUM()/MAX() to catch current value.
You can try this code to create a measure.
Distance Travelled = VAR _NextDay = CALCULATE ( SUM ( 'Table'[Odometer] ), FILTER ( ALLEXCEPT ( 'Table', 'Table'[DeviceName] ), 'Table'[DateUTC] = MAX ( 'Table'[DateUTC] ) + 1 ) ) VAR _CurrentDay = CALCULATE ( SUM ( 'Table'[Odometer] ) ) VAR _DIFF = _NextDay - _CurrentDay RETURN IF ( _DIFF < 0, BLANK (), _DIFF )Result is as below.
Best Regards,
Rico ZhouIf this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
5 Replies
- AlexisOlsonSuper User
EARLIER refers to an outer row context rather than having anything to do with time.
There are several solutions to calculating differences from cumulative totals in this post:
https://community.powerbi.com/t5/Desktop/Calculating-Daily-values-from-Cumulative-Total/m-p/2198969
- rsbinCommunity Champion
Good Morning AlexisOlson ,
Thanks much for the reply. I do understand that EARLIER refers to a method to obtain a row context.
I have just never used it before.
I believe the model I am creating will be an SSAS Tabular, so Power Query won't be available to me. I did see there were Calc Column solutions in the link you sent me. I will play with those and see if I can make it work in my case.
Thanks again, much appreciated!
- AnonymousNot applicable
Hi rsbin ,
We will use EARLIER() to catch current value in calculated column. In measure we will use SUM()/MAX() to catch current value.
You can try this code to create a measure.
Distance Travelled = VAR _NextDay = CALCULATE ( SUM ( 'Table'[Odometer] ), FILTER ( ALLEXCEPT ( 'Table', 'Table'[DeviceName] ), 'Table'[DateUTC] = MAX ( 'Table'[DateUTC] ) + 1 ) ) VAR _CurrentDay = CALCULATE ( SUM ( 'Table'[Odometer] ) ) VAR _DIFF = _NextDay - _CurrentDay RETURN IF ( _DIFF < 0, BLANK (), _DIFF )Result is as below.
Best Regards,
Rico ZhouIf this post helps, then please consider Accept it as the solution to help the other members find it more quickly.