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

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes! Register now.

Reply
Anonymous
Not applicable

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
Anonymous
Not applicable

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


Hi  @Anonymous ,

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
Anonymous
Not applicable

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


Hi  @Anonymous ,

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.

Anonymous
Not applicable

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

Greg_Deckler
Community Champion
Community Champion

@Anonymous 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/339586.
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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

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
September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.