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%3EF%C3%B3rmula%20condicional%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-711204%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EEstoy%20intentando%20agregar%20una%20f%C3%B3rmula%20de%20columna%20que%20sea%20condicional%20a%20otras%20columnas.%20Tengo%20datos%20donde%20la%20columna%20%22Resultados%22%20est%C3%A1%20apilada%20por%20%22Asunto%22%20y%20%22Producto%22%20con%20m%C3%BAltiples%20valores%20sobre%20%22Tiempo%22.%20Estoy%20intentando%20calcular%20la%20l%C3%ADnea%20de%20base%20para%20cada%20valor%20en%20%22Resultados%22%20de%20modo%20que%3A%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ENueva%20columna%20%3D%20%22Resultados%22%20%E2%80%93%20%5Bvalor%20de%20%22Resultados%22%20donde%20Tiempo%3D(1)%20para%20esa%20combinaci%C3%B3n%20de%20Asunto%2FProducto%20en%20particular%5D%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EGracias%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%3EAutomatizaci%C3%B3n%20y%20secuencias%20de%20comandos%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%20F%C3%B3rmula%20condicional%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-711220%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3E%C2%BFPuedes%20dar%20un%20ejemplo%20de%20los%20datos%3FEso%20ayudar%C3%A1%20a%20encontrar%20una%20soluci%C3%B3n%20adecuada.%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%20F%C3%B3rmula%20condicional%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-711237%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3ESi%20sus%20datos%20est%C3%A1n%20ordenados%20por%20Asunto%2C%20Producto%20y%20Hora%2C%20este%20ejemplo%20funcionar%C3%A1%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%20F%C3%B3rmula%20condicional%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-711335%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EHe%20incluido%20datos%20de%20muestra.%20Necesito%20agregar%20una%20nueva%20columna%20donde%20la%20f%C3%B3rmula%20toma%20el%20valor%20de%20Resultados%20de%20Tiempo%20(-5)%20para%20cada%20combinaci%C3%B3n%20de%20Asunto%2FProducto%20y%20resta%20ese%20valor%20de%20cada%20Resultado%20en%20la%20Combinaci%C3%B3n%20de%20Asunto%2FProducto.%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%20F%C3%B3rmula%20condicional%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-711362%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EA%20veces%20es%20m%C3%A1s%20f%C3%A1cil%20atacar%20problemas%20como%20este%20en%20varios%20pasos%2C%20en%20lugar%20de%20escribir%20jsl%20complejo%20para%20una%20f%C3%B3rmula%20de%20columna.%20Este%20es%20un%20enfoque%20que%20utiliza%20%22Subconjunto%22%20para%20extraer%20las%20filas%20de%20Tiempo%20%3D%20-5%2C%20las%20vuelve%20a%20unir%20a%20la%20tabla%20de%20datos%20original%20con%20%22Actualizar%22%20y%20luego%20realiza%20una%20resta%20simple%20en%20una%20f%C3%B3rmula%20de%20columna.%20Si%20est%C3%A1%20ejecutando%20JMP%2017%2C%20los%20flujos%20de%20trabajo%20son%20una%20excelente%20manera%20de%20crear%20y%20compartir%20este%20tipo%20de%20cosas.%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%20F%C3%B3rmula%20condicional%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-711367%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3EEl%20JSL%20que%20se%20me%20ocurri%C3%B3%20no%20es%20demasiado%20complicado.No%20escrib%C3%AD%20una%20f%C3%B3rmula%20sino%20un%20gui%C3%B3n%20de%20tabla.Puede%20ejecutar%20esto%20cada%20vez%20que%20agregue%20m%C3%A1s%20datos.%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%3Eeditar%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EDespu%C3%A9s%20de%20mirar%20m%C3%A1s%20la%20tabla%2C%20not%C3%A9%20que%20faltaban%20valores%20en%20algunas%20celdas%3A%20Resultados.Deber%C3%ADas%20recodificarlos%20a%200%20o%20modificar%20un%20poco%20el%20script%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%20F%C3%B3rmula%20condicional%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-711236%22%20slang%3D%22en-US%22%20mode%3D%22NONE%22%3E%3CP%3ES%C3%B3lo%20una%20suposici%C3%B3n%2C%20ya%20que%20no%20hay%20datos%20para%20probar%20esto.%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%3EEditar%3A%3C%2FP%3E%0A%3CP%3EF%C3%B3rmula%20actualizada%20despu%C3%A9s%20de%20recibir%20datos%20de%20muestra%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