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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
rrg1010
Regular Visitor

Calculate row differences based on grouped data

Hello!  I am having some trouble creating a calculated column to find the difference in mileage between consecutive rows based on the vehicle + driver calculated field.

I created two index fields to help identify the order in which to subtract the consecutive rows, but I want to include a grouping criteria to only find the difference if the VINName (calculated column) is the same.   I'm trying to eliminate calculations for the very first row of data per VINName showed on the 1/6/2024 line item.  I tried including an EARLIER experession with the VINName, but I don't think that column is compatible with the EARLIER expression.  Below is a snip of the data in a pivot.  

 

Screenshot 2024-06-06 111635.png

 

 

 

Here's what I currently have for DAX:

 

ODOMVAR = VAR Diff = 'Odometer Helper'[Fuel Odometer] - 
    CALCULATE(SUM('Odometer Helper'[Fuel Odometer]),
    FILTER('Odometer Helper','Odometer Helper'[Index01]=EARLIER('Odometer Helper'[Index])
    )
    )
RETURN
    IF(
        Diff=VALUE('Odometer Helper'[Fuel Odometer]),
    0,
    Diff
    )
1 ACCEPTED SOLUTION
v-yangliu-msft
Community Support
Community Support

Thanks for the reply from @Greg_Deckler , please allow me to provide another insight: 


Hi  @rrg1010 ,

You can try the following calculated columns:

ODOMVAR =
var _mindate=
MINX(FILTER(ALL('Odometer Helper'),'Odometer Helper'[VINName]=EARLIER('Odometer Helper'[VINName])),[Date])
var _lastdate=
MAXX(
    FILTER(ALL('Odometer Helper'),
    'Odometer Helper'[VINName]=EARLIER('Odometer Helper'[VINName])&&
    'Odometer Helper'[Date]<EARLIER('Odometer Helper'[Date])),'Odometer Helper'[Date])
RETURN
IF(
    'Odometer Helper'[Date]=_mindate,BLANK(),
    'Odometer Helper'[Fuel Odometer]-
    SUMX(
        FILTER(ALL('Odometer Helper'),
        'Odometer Helper'[VINName]=EARLIER('Odometer Helper'[VINName])&&
        'Odometer Helper'[Date]=_lastdate),[Fuel Odometer]))

vyangliumsft_0-1718087451578.png

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

4 REPLIES 4
v-yangliu-msft
Community Support
Community Support

Thanks for the reply from @Greg_Deckler , please allow me to provide another insight: 


Hi  @rrg1010 ,

You can try the following calculated columns:

ODOMVAR =
var _mindate=
MINX(FILTER(ALL('Odometer Helper'),'Odometer Helper'[VINName]=EARLIER('Odometer Helper'[VINName])),[Date])
var _lastdate=
MAXX(
    FILTER(ALL('Odometer Helper'),
    'Odometer Helper'[VINName]=EARLIER('Odometer Helper'[VINName])&&
    'Odometer Helper'[Date]<EARLIER('Odometer Helper'[Date])),'Odometer Helper'[Date])
RETURN
IF(
    'Odometer Helper'[Date]=_mindate,BLANK(),
    'Odometer Helper'[Fuel Odometer]-
    SUMX(
        FILTER(ALL('Odometer Helper'),
        'Odometer Helper'[VINName]=EARLIER('Odometer Helper'[VINName])&&
        'Odometer Helper'[Date]=_lastdate),[Fuel Odometer]))

vyangliumsft_0-1718087451578.png

 

Best Regards,

Liu Yang

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

Ah, yes!  The Filter and SUMX at the end worked perfectly for me.  Thanks so much!

Greg_Deckler
Super User
Super User

@rrg1010 See my article on Mean Time Between Failure (MTBF) which uses EARLIER: http://community.powerbi.com/t5/Community-Blog/Mean-Time-Between-Failure-MTBF-and-Power-BI/ba-p/3395....
The basic pattern is:
Column = 
  VAR __Current = [Value]
  VAR __PreviousDate = MAXX(FILTER('Table','Table'[Date] < EARLIER('Table'[Date])),[Date])

  VAR __Previous = MAXX(FILTER('Table',[Date]=__PreviousDate),[Value])
RETURN
 ( __Current - __Previous ) * 1.


Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

This worked in the sense that the EARLIER function processed, but I am still having trouble grouping my data by the calculated column, VINName.  If there's a way to incorporate that into my existing DAX function, I think that would solve my problem more directly.

Helpful resources

Announcements
July 2024 Power BI Update

Power BI Monthly Update - July 2024

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

July Newsletter

Fabric Community Update - July 2024

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

Top Solution Authors