cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
  • JMP will suspend normal business operations for our Winter Holiday beginning on Wednesday, Dec. 24, 2025, at 5:00 p.m. ET (2:00 p.m. ET for JMP Accounts Receivable).
    Regular business hours will resume at 9:00 a.m. EST on Friday, Jan. 2, 2026.
  • We’re retiring the File Exchange at the end of this year. The JMP Marketplace is now your destination for add-ins and extensions.

Discussions

Solve problems, and share tips and tricks with other JMP users.
%3CLINGO-SUB%20id%3D%22lingo-sub-711204%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3EConditional%20Formula%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-711204%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EI%20am%20trying%20to%20add%20a%20column%20formula%20that%20is%20conditional%20to%20other%20columns.%20I%20have%20data%20where%20%E2%80%9CResults%E2%80%9D%20Column%20is%20stacked%20by%20%E2%80%9CSubject%E2%80%9D%20and%20%E2%80%9CProduct%E2%80%9D%20with%20multiple%20values%20over%20%E2%80%9CTime%E2%80%9D.%20I%E2%80%99m%20trying%20to%20calculate%20the%20baseline%20for%20each%20value%20in%20%E2%80%9CResults%E2%80%9D%20so%20that%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENew%20Column%20%3D%20%22Results%22%20%E2%80%93%20%5B%22Results%22%20value%20where%20Time%3D(1)%20for%20that%20particular%20Subject%2FProduct%20combination%5D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EThank%20you%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-711204%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CLINGO-LABEL%3EAutomation%20and%20Scripting%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-711220%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Conditional%20Formula%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-711220%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3ECan%20you%20give%20an%20example%20of%20the%20data%3F%26nbsp%3B%20That%20will%20help%20with%20finding%20a%20proper%20solution.%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-711237%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Conditional%20Formula%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-711237%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EIf%20your%20data%20are%20sorted%20by%20Subject%2C%20Product%2C%20Time%20then%20this%20example%20will%20work%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3ENames%20Default%20To%20Here(%201%20)%3B%0Adt%20%3D%20Open(%20%22%24SAMPLE_DATA%2Fbig%20class.jmp%22%20)%3B%0Adt%3Aage%20%26lt%3B%26lt%3B%20set%20name(%20%22Subject%22%20)%3B%0Adt%3Asex%20%26lt%3B%26lt%3B%20set%20name(%20%22Product%22%20)%3B%0Adt%3Aweight%20%26lt%3B%26lt%3B%20set%20name(%20%22Results%22%20)%3B%0A%0Adt%20%26lt%3B%26lt%3B%20New%20Column(%20%22baseline%22%2C%0A%20formula(%0A%20%20If(%20Row()%20%3D%3D%201%2C%0A%20%20%20valueTime1%20%3D%20%3AResults%2C%0A%20%20%20If(%20%3ASubject%20!%3D%20Lag(%20%3Asubject%20)%20%7C%20%3AProduct%20!%3D%20Lag(%20%3Aproduct%20)%2C%0A%20%20%20%20valueTime1%20%3D%20%3AResults%0A%20%20%20)%0A%20%20)%3B%0A%20%20baseline%20%3D%20%3AResults%20-%20valueTime1%3B%0A%20)%0A)%3B%3C%2FCODE%3E%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-711335%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Conditional%20Formula%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-711335%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EI've%20included%20sample%20data.%20I%20need%20to%20add%20a%20new%20column%20where%20the%20formula%20takes%20the%20Time(-5)%20Results%20value%20for%20each%20Subject%2FProduct%20combination%20and%20subtracts%20that%20value%20from%20each%20Result%20in%20the%20Subject%2FProduct%20Combination.%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-711362%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Conditional%20Formula%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-711362%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3ESometimes%20it's%26nbsp%3B%20easier%20to%20attack%20problems%20like%20this%20in%20several%20steps%2C%20rather%20than%20writing%20complex%20jsl%20for%20a%20column%20formula.%20Here's%20an%20approach%20that%20uses%20%22Subset%22%20to%20extract%20the%20Time%3D-5%20rows%2C%20joins%20them%20back%20onto%20the%20original%20data%20table%20with%20%22Update%22%2C%20and%20then%20performs%20a%20simple%20subtraction%20in%20a%20column%20formula.%20If%20you're%20running%20JMP%2017%2C%20workflows%20are%20a%20great%20way%20to%20build%20and%20share%20this%20kind%20of%20thing.%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-711367%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Conditional%20Formula%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-711367%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EThe%20JSL%20I%20came%20up%20with%20isn't%20too%20complicated.%26nbsp%3B%20I%20didn't%20write%20a%20formula%20but%20a%20table%20script.%26nbsp%3B%20You%20could%20run%20this%20whenever%20you%20add%20more%20data.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Edt%20%3D%20Current%20Data%20Table()%3B%0AOffsetArray%20%3D%20Associative%20Array()%3B%0ATimeRows%20%3D%20dt%20%26lt%3B%26lt%3B%20Get%20Rows%20Where(%20%3ATime%20%3D%3D%20-5%20)%3B%0AFor%20Each(%20%7Bval%2C%20idx%7D%2C%20TimeRows%2C%20OffsetArray%5B%3ASubject%5Bval%5D%20%7C%7C%20%3AProduct%5Bval%5D%5D%20%3D%20%3AResults%5Bval%5D%20)%3B%0AFor%20Each%20Row(%20dt%2C%20%3ANew%20Column%20%3D%20%3AResults%20-%20OffsetArray%5B%3ASubject%20%7C%7C%20%3AProduct%5D%20)%3B%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3Eedit%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EAfter%20looking%20at%20the%20table%20more%2C%20I%20noticed%20the%20missing%20values%20in%20some%20%3AResults%20cells.%26nbsp%3B%20You%20should%20recode%20those%20to%200%20or%20alter%20the%20script%20a%20bit%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3Edt%20%3D%20Current%20Data%20Table()%3B%0AOffsetArray%20%3D%20Associative%20Array()%3B%0ATimeRows%20%3D%20dt%20%26lt%3B%26lt%3B%20Get%20Rows%20Where(%20%3ATime%20%3D%3D%20-5%20)%3B%0AFor%20Each(%20%7Bval%2C%20idx%7D%2C%20TimeRows%2C%20OffsetArray%5B%3ASubject%5Bval%5D%20%7C%7C%20%3AProduct%5Bval%5D%5D%20%3D%20If(%20!Is%20Missing(%20%3AResults%5Bval%5D%20)%2C%20%3AResults%5Bval%5D%2C%200%20)%20)%3B%0AFor%20Each%20Row(%20dt%2C%20%3AResults%20with%20Offset%20%3D%20%3AResults%20-%20OffsetArray%5B%3ASubject%20%7C%7C%20%3AProduct%5D%20)%3B%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-711236%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3ERe%3A%20Conditional%20Formula%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-711236%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EJust%20a%20guess%20as%20there%20is%20no%20data%20to%20test%20this%20with%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3E%3AResult%20-%20Col%20Min(If(%3ATime%20%3D%3D%201%2C%20%3AResult%2C%20.)%2C%20%3AGroup)%3B%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%0A%3CP%3EEdit%3A%3C%2FP%3E%0A%3CP%3EUpdated%20formula%20after%20we%20received%20sample%20data%3C%2FP%3E%0A%3CPRE%3E%3CCODE%20class%3D%22%20language-jsl%22%3E%3AResults%20-%20Col%20Min(If(%3ATime%20%3D%3D%20-5%2C%20%3AResults%2C%20.)%2C%20%3ASubject%2C%20%3AProduct)%3C%2FCODE%3E%3C%2FPRE%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Choose Language Hide Translation Bar
JMPUser9
Level II

Conditional Formula

I am trying to add a column formula that is conditional to other columns. I have data where “Results” Column is stacked by “Subject” and “Product” with multiple values over “Time”. I’m trying to calculate the baseline for each value in “Results” so that:

 

New Column = "Results" – ["Results" value where Time=(1) for that particular Subject/Product combination]

 

Thank you

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Conditional Formula

Sometimes it's  easier to attack problems like this in several steps, rather than writing complex jsl for a column formula. Here's an approach that uses "Subset" to extract the Time=-5 rows, joins them back onto the original data table with "Update", and then performs a simple subtraction in a column formula. If you're running JMP 17, workflows are a great way to build and share this kind of thing.

 

View solution in original post

6 REPLIES 6
mmarchandTSI
Level V

Re: Conditional Formula

Can you give an example of the data?  That will help with finding a proper solution.

jthi
Super User

Re: Conditional Formula

Just a guess as there is no data to test this with

:Result - Col Min(If(:Time == 1, :Result, .), :Group);

 

Edit:

Updated formula after we received sample data

:Results - Col Min(If(:Time == -5, :Results, .), :Subject, :Product)

 

-Jarmo
txnelson
Super User

Re: Conditional Formula

If your data are sorted by Subject, Product, Time then this example will work

Names Default To Here( 1 );
dt = Open( "$SAMPLE_DATA/big class.jmp" );
dt:age << set name( "Subject" );
dt:sex << set name( "Product" );
dt:weight << set name( "Results" );

dt << New Column( "baseline",
	formula(
		If( Row() == 1,
			valueTime1 = :Results,
			If( :Subject != Lag( :subject ) | :Product != Lag( :product ),
				valueTime1 = :Results
			)
		);
		baseline = :Results - valueTime1;
	)
);
Jim
JMPUser9
Level II

Re: Conditional Formula

I've included sample data. I need to add a new column where the formula takes the Time(-5) Results value for each Subject/Product combination and subtracts that value from each Result in the Subject/Product Combination. 

Re: Conditional Formula

Sometimes it's  easier to attack problems like this in several steps, rather than writing complex jsl for a column formula. Here's an approach that uses "Subset" to extract the Time=-5 rows, joins them back onto the original data table with "Update", and then performs a simple subtraction in a column formula. If you're running JMP 17, workflows are a great way to build and share this kind of thing.

 

mmarchandTSI
Level V

Re: Conditional Formula

The JSL I came up with isn't too complicated.  I didn't write a formula but a table script.  You could run this whenever you add more data.

 

dt = Current Data Table();
OffsetArray = Associative Array();
TimeRows = dt << Get Rows Where( :Time == -5 );
For Each( {val, idx}, TimeRows, OffsetArray[:Subject[val] || :Product[val]] = :Results[val] );
For Each Row( dt, :New Column = :Results - OffsetArray[:Subject || :Product] );

edit

 

After looking at the table more, I noticed the missing values in some :Results cells.  You should recode those to 0 or alter the script a bit:

 

dt = Current Data Table();
OffsetArray = Associative Array();
TimeRows = dt << Get Rows Where( :Time == -5 );
For Each( {val, idx}, TimeRows, OffsetArray[:Subject[val] || :Product[val]] = If( !Is Missing( :Results[val] ), :Results[val], 0 ) );
For Each Row( dt, :Results with Offset = :Results - OffsetArray[:Subject || :Product] );

 

Recommended Articles