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

July 7 - July 17 | Round 2 of the Power BI Dataviz World Championships. Don't miss your chance! Learn more

Reply
Wickin
Frequent Visitor

Calculate difference between two rows by using Index column

Hi al,

 

I know this question has been asked before, but I tried them all and wasn't able to compete. Sorry!

 

I've got the below table (4 colums) within Power BI (just created a table in Excel to edit values). I would like to calculate the difference between values in column "Kilometers".

The calculation should only be applied for rows with the same value for column "Reference", so I guess we need to calculate the difference between a row and a row where [Index] = ([Index] -1) ?

 

Anybody got any tips on the DAX or Power Query code to be applied? I've created a column "Expected result" in Excel, just to clarify things.

 

Thanks in advance!

 

Table1

1 ACCEPTED SOLUTION
Stachu
Community Champion
Community Champion

try this

Column =
VAR Index = 'Table'[Index]
VAR Reference = 'Table'[Reference]
VAR PrevKilometers =
    CALCULATE (
        FIRSTNONBLANK ( 'Table'[Kilometers], TRUE () ),
        FILTER ( 'Table', 'Table'[Index] = Index - 1 && 'Table'[Reference] = Reference )
    )
RETURN
    IF (
        ISBLANK ( PrevKilometers ),
        BLANK (),
        'Table'[Kilometers] - PrevKilometers
    )


Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

View solution in original post

9 REPLIES 9
Stachu
Community Champion
Community Champion

try this

Column =
VAR Index = 'Table'[Index]
VAR Reference = 'Table'[Reference]
VAR PrevKilometers =
    CALCULATE (
        FIRSTNONBLANK ( 'Table'[Kilometers], TRUE () ),
        FILTER ( 'Table', 'Table'[Index] = Index - 1 && 'Table'[Reference] = Reference )
    )
RETURN
    IF (
        ISBLANK ( PrevKilometers ),
        BLANK (),
        'Table'[Kilometers] - PrevKilometers
    )


Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

Anonymous
Not applicable

Hello Mr. Stachu,
I tried the above formula and it returned the incorrect values. All the valules in the column are taking the first value as a reference. Please find the attached image for the reference.

Vk787_0-1648194256507.png

 

based on the formula provided below i have done on my data set but error is coming. kinldy guide me urgently. 

 

BI1.JPG

I want to know the incremental number of column 
Death Cases, confirmed Cases,Recovered CAses, Active Cases.

Stachu
Community Champion
Community Champion

@usmanaziz you try to create a measure, the code you posted will only work as a calculated column



Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

Anonymous
Not applicable

How can I adjust your formula to include data also as a criteria ?

i.e.,compare the values between current timestamp and previous time stamp,say 17-08-2019,10:25 P.M and 17-08-2019,10:30 P.M.

I have an ID Field and the glucose values,I should be capable of comparing the glucose_value,every now and then to indicate the episodes or spike in glucose level.

Thanks

@Stachu 

Stachu
Community Champion
Community Champion

the ID can be the same for multiple dates?
if yes then the difference in glucose for a given ID should be something like this:

Column =
VAR __ID = 'Table'[ID]
VAR __DateTime = 'Table'[DateTime]
VAR __PreviousDateTime =
    CALCULATE (
        MAX ( 'Table'[DateTime] ),
        FILTER ( 'Table', 'Table'[ID] = __ID && 'Table'[DateTime] < __DateTime)
    )
VAR __PreviousGlucose = 
    CALCULATE (
        MAX ( 'Table'[Glucose] ),
        FILTER ( 'Table', 'Table'[ID] = __ID && 'Table'[DateTime] = __PreviousDateTime)
    )
RETURN
    IF (
        ISBLANK ( __PreviousDateTime ),
        BLANK (),
        'Table'[Glucose] - __PreviousGlucose
    )


Did I answer your question? Mark my post as a solution!
Thank you for the kudos 🙂

Wickin
Frequent Visitor

Thank you @Stachu, that's exactly what I needed!

Anonymous
Not applicable

Hi @Wickin

 

The image attached is not visible.

It would be great if you provide the Excel with dummy data.

 

Regards,

Suguna.

Helpful resources

Announcements
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

60 days of Data Days Carousel

Data Days 2026

Join Fabric Data Days 2026: 60 days of free live/on-demand sessions, challenges, study groups, and certification opportunities.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.