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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
user35131
Helper III
Helper III

How to subtract row values by previous row within a group and set the result in a new column

I would like to set the result in the first row as opposed to the later row. For example 

 

GroupValueIndex
A101
A52
A33
B191
B152
C151
C102
C73
C174

 

Which would look like this

 

GroupValueIndexResult
A1015
A522
A33NULL
B1914
B152NULL
C1515
C1023
C73-10
C174NULL

 

note that many of the similar questions will send the result in the next row. I want it to stay in the first row.

1 ACCEPTED SOLUTION
v-yiruan-msft
Community Support
Community Support

Hi @user35131 ,

You can create a measure as below to get it, please find the details in the attachment.

Result = 
VAR _selgroup =
    SELECTEDVALUE ( 'Table'[Group] )
VAR _selindex =
    SELECTEDVALUE ( 'Table'[Index] )
VAR _nextvalue =
    CALCULATE (
        SUM ( 'Table'[Value] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[Group] = _selgroup
                && 'Table'[Index] = _selindex + 1
        )
    )
RETURN
    IF ( ISBLANK ( _nextvalue ), BLANK (), SUM ( 'Table'[Value] ) - _nextvalue )

yingyinr_0-1675737787616.png

In addition, you can create a calculated column as below to get it.

Column = 
VAR _nextvalue =
    CALCULATE (
        SUM ( 'Table'[Value] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[Group] = EARLIER('Table'[Group])
                && 'Table'[Index] = EARLIER('Table'[Index]) + 1
        )
    )
RETURN
    IF ( ISBLANK ( _nextvalue ), BLANK (),  'Table'[Value]  - _nextvalue )

yingyinr_1-1675737946203.png

Best Regards

Community Support Team _ Rena
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
Ashish_Mathur
Super User
Super User

Hi,

This calculated column formula works

Column = if(ISBLANK(LOOKUPVALUE(Data[Value],Data[Index],CALCULATE(MIN(Data[Index]),FILTER(Data,Data[Group]=EARLIER(Data[Group])&&Data[Index]>EARLIER(Data[Index]))),Data[Group],Data[Group])),BLANK(),Data[Value]-LOOKUPVALUE(Data[Value],Data[Index],CALCULATE(MIN(Data[Index]),FILTER(Data,Data[Group]=EARLIER(Data[Group])&&Data[Index]>EARLIER(Data[Index]))),Data[Group],Data[Group]))

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
v-yiruan-msft
Community Support
Community Support

Hi @user35131 ,

You can create a measure as below to get it, please find the details in the attachment.

Result = 
VAR _selgroup =
    SELECTEDVALUE ( 'Table'[Group] )
VAR _selindex =
    SELECTEDVALUE ( 'Table'[Index] )
VAR _nextvalue =
    CALCULATE (
        SUM ( 'Table'[Value] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[Group] = _selgroup
                && 'Table'[Index] = _selindex + 1
        )
    )
RETURN
    IF ( ISBLANK ( _nextvalue ), BLANK (), SUM ( 'Table'[Value] ) - _nextvalue )

yingyinr_0-1675737787616.png

In addition, you can create a calculated column as below to get it.

Column = 
VAR _nextvalue =
    CALCULATE (
        SUM ( 'Table'[Value] ),
        FILTER (
            ALLSELECTED ( 'Table' ),
            'Table'[Group] = EARLIER('Table'[Group])
                && 'Table'[Index] = EARLIER('Table'[Index]) + 1
        )
    )
RETURN
    IF ( ISBLANK ( _nextvalue ), BLANK (),  'Table'[Value]  - _nextvalue )

yingyinr_1-1675737946203.png

Best Regards

Community Support Team _ Rena
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.
amitchandak
Super User
Super User

@user35131 , for that we need to have some date of index column

 

Add an index column in the power query

Index Column: https://youtu.be/NS4esnCDqVw

 

Then create a new column in dax like

 

diff = [result] - max(filter(Table, [group] = earlier([group] ) && [Index] = earlier([index]) +1 ), [result])

 

Power BI DAX- Earlier, I should have known Earlier: https://youtu.be/CVW6YwvHHi8

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

You have result in the formula before it is defined. Will this work? The second table is the table I want. The first table is the one I have.

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.