cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
Choose Language Hide Translation Bar
UserID16644
Level V

How to properly use LAG function in Conditional Statements?

I have this formula but it is adding the value on the same row.

 

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

 

 

 

 

What I would want to do is something like this:

Column 1 and Column 2 are given values. If Column 2 value changes, the result should be like the starting value in Column 1.

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 - 12 +2)
3203Column 2 - Column 1 rows 1 to 3 (20 - 12 +2 +5)
2152 
11513 
31512 
4159 

How can I create a table equation like this?

1 ACCEPTED SOLUTION

Accepted Solutions
jthi
Super User

Re: How to properly use LAG function in Conditional Statements?

By vars are arguments which you can use with Col statistical functions Statistical Functions (jmp.com)

jthi_0-1708584518911.png

 

Names Default To Here(1);

dt = New Table("Untitled 4",
	Add Rows(8),
	Compress File When Saved(1),
	New Column("Column 1", Numeric, "Continuous", Format("Best", 12), Set Values([10, 2, 5, 3, 2, 1, 3, 4])),
	New Column("Column 2", Numeric, "Continuous", Format("Best", 12), Set Values([20, 20, 20, 20, 15, 15, 15, 15])),
	New Column("Result", Numeric, "Continuous", Format("Best", 12), Set Values([10, 10, 8, 3, 2, 13, 12, 9]))
);

dt << New Column("Calc",
	Numeric,
	Continuous,
	Formula(
		If(Row() == Col Min(Row(), :Column 2),
			:Column 1
		,  
			:Column 2 - Col Cumulative Sum(:Column 1, :Column 2) + :Column 1
		)
	)
);
-Jarmo

View solution in original post

6 REPLIES 6
jthi
Super User

Re: How to properly use LAG function in Conditional Statements?

Where is the value 12 coming from to row 3+ calculations?

-Jarmo
UserID16644
Level V

Re: How to properly use LAG function in Conditional Statements?

Sorry, that's 10

 

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 
jthi
Super User

Re: How to properly use LAG function in Conditional Statements?

You can use col cumulative sum

If(
	Row() == 1, :Column 1
,  
	:Column 2 - Col Cumulative Sum(:Column 1) + :Column 1
)
Names Default To Here(1);

dt = New Table("Untitled 4",
	Add Rows(8),
	Compress File When Saved(1),
	New Column("Column 1", Numeric, "Continuous", Format("Best", 12), Set Values([10, 2, 5, 3, 2, 1, 3, 4])),
	New Column("Column 2", Numeric, "Continuous", Format("Best", 12), Set Values([20, 20, 20, 20, 15, 15, 15, 15])),
	New Column("Result", Numeric, "Continuous", Format("Best", 12), Set Values([10, 10, 8, 3, 2, 13, 12, 9]))
);

dt << New Column("Calc",
	Numeric,
	Continuous,
	Formula(
		If(
			Row() == 1, :Column 1
		,  
			:Column 2 - Col Cumulative Sum(:Column 1) + :Column 1
		)
	)
);

jthi_0-1708582418422.png

I guess Column 2 is some sort of grouping which should reset your "counter"? You can use Col Min to detect that change and use that column as byVar if needed

-Jarmo
UserID16644
Level V

Re: How to properly use LAG function in Conditional Statements?

Yes, Column 2 acts like a reset counter. How do you use byVar in this? Haven't heard and used that function before

jthi
Super User

Re: How to properly use LAG function in Conditional Statements?

By vars are arguments which you can use with Col statistical functions Statistical Functions (jmp.com)

jthi_0-1708584518911.png

 

Names Default To Here(1);

dt = New Table("Untitled 4",
	Add Rows(8),
	Compress File When Saved(1),
	New Column("Column 1", Numeric, "Continuous", Format("Best", 12), Set Values([10, 2, 5, 3, 2, 1, 3, 4])),
	New Column("Column 2", Numeric, "Continuous", Format("Best", 12), Set Values([20, 20, 20, 20, 15, 15, 15, 15])),
	New Column("Result", Numeric, "Continuous", Format("Best", 12), Set Values([10, 10, 8, 3, 2, 13, 12, 9]))
);

dt << New Column("Calc",
	Numeric,
	Continuous,
	Formula(
		If(Row() == Col Min(Row(), :Column 2),
			:Column 1
		,  
			:Column 2 - Col Cumulative Sum(:Column 1, :Column 2) + :Column 1
		)
	)
);
-Jarmo
mmarchandTSI
Level V

Re: How to properly use LAG function in Conditional Statements?

@UserID16644, if your :Column2 ever has the same value again, you'll get undesired results.

 

New Table( "Untitled 155",
	Add Rows( 12 ),
	New Column( "Column 1",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Set Values( [10, 2, 5, 3, 2, 1, 3, 4, 6, 2, 5, 3] )
	),
	New Column( "Column 2",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Set Values( [20, 20, 20, 20, 15, 15, 15, 15, 20, 20, 20, 20] )
	),
	New Column( "Result",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Formula(
			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 )
			)
		)
	),
	New Column( "Result 2",
		Numeric,
		"Continuous",
		Format( "Best", 12 ),
		Formula(
			If( Row() == Col Min( Row(), :Column 2 ),
				:Column 1,
				(:Column 2 - Col Cumulative Sum( :Column 1, :Column 2 )) + :Column 1
			)
		)
	)
)

mmarchandTSI_0-1708608906052.png