cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • Don’t miss your chance to experience Discovery Summit Europe at our best available rate! Early bird registration through 31 Oct.
  • Graph Builder Toolbar streamlines interactive graphing. Creates shortcuts on Report and GraphBuilder Helpers toolbars. Download and install the extension.
  • JMP will suspend normal business operations for our Rest and Recharge Day on Friday, October 2, 2026.
    Regular business hours will resume on Monday, October 5, 2026.

Discussions

Solve problems, and share tips and tricks with other JMP users.
Choose Language Hide Translation Bar
UserID16644
Level V

How to subtract values from previous row values?

Hi all

 

Can someone help me how to formulate a formula that would subtract Column 2 to the previous value in Column 1? It should look like this. The column Equation only shows how the subtraction of values should be.TIA

 

UserID16644_0-1708499081883.png

 

1 ACCEPTED SOLUTION

Accepted Solutions
David_Burnham
Super User (Alumni)

Re: How to subtract values from previous row values?

Then you need some conditional logic.  Something like:

 

If( :Column 2 != Lag( :Column 2, 1 ) | Row() == 1,
	:Column 1,
	:Column 2 - Lag( :Column 1, 1 )
)
-Dave

View solution in original post

7 REPLIES 7
David_Burnham
Super User (Alumni)

Re: How to subtract values from previous row values?

You can use the LAG function:

 

:Column 1 - Lag( :Column 2, 1 )
-Dave
UserID16644
Level V

Re: How to subtract values from previous row values?

Hi, how about if I wanted to retain the row value before the values in Column 2 changes? Like this one

UserID16644_0-1708500899623.png

 

David_Burnham
Super User (Alumni)

Re: How to subtract values from previous row values?

Then you need some conditional logic.  Something like:

 

If( :Column 2 != Lag( :Column 2, 1 ) | Row() == 1,
	:Column 1,
	:Column 2 - Lag( :Column 1, 1 )
)
-Dave
UserID16644
Level V

Re: How to subtract values from previous row values?

Hi, I have a follow up question tho. What if I needed to add the values from Column 1 to be subtracted in Column 2?

For example, 

Column 1Column 2Result 
102010 
2208 
5203 
3200 
2155 
11513 
31512 
4155 
mmarchandTSI
Level V

Re: How to subtract values from previous row values?

Could you double-check your numbers here?  I'm not sure what's going on when :Column 2 == 15.

 

But I think this is what you want (based on David's script):

 

If(
	:Column 2 != Lag( :Column 2 ) | Row() == 1, :Column 1,
	Row() == 2 | Lag( :Column 2, 2 ) != :Column 2, :Column 2 - (:Column 1 + Lag( :Column 1 )),
	Lag( :Result ) - :Column 1
); 

 

UserID16644
Level V

Re: How to subtract values from previous row values?

Hi

Column 2 are given values. Apologies, my table is a bit messed up. Values within the same row should not be subtracted on the added values. It should be

 

Column 1Column 2Result(should be computation)
102010same value in Column 1
22010Column 2 - Column 1 row 1 (20-10)
5208Column 2 - Column 1 rows 1 to 2 (20 - 10 +2)
3203Column 2 - Column 1 rows 1 to 3 (20 - 10 +2 +5)
2152 
11513 
31512 
4159 
mmarchandTSI
Level V

Re: How to subtract values from previous row values?

In that case:

 

If(
	:Column 2 != Lag( :Column 2 ) | Row() == 1, :Column 1,
	Row() == 2 | Lag( :Column 2, 2 ) != :Column 2, :Column 2 - Lag( :Column 1 ),
	Lag( :Result ) - Lag( :Column 1 )
)

Recommended Articles