<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Calculations between rows based on conditions in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Calculations-between-rows-based-on-conditions/m-p/864677#M102825</link>
    <description>&lt;P&gt;Welcome to the Community.&lt;/P&gt;
&lt;P&gt;Here is an example of how I would approach your problem&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="txnelson_0-1743592985021.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/74489i51BBE4D597BC3C6F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="txnelson_0-1743592985021.png" alt="txnelson_0-1743592985021.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
dt = Current Data Table();

// Separate out the Cuvette==12 rows into a separate data table
// and then join the Pass==Pass rows to the Cuvette==11 rows
// Then calculate the Output column

// Select the Cuvette==12 rows
dt &amp;lt;&amp;lt; Select Where( :Cuvette == 12 );

// Create a new subsetted data table with those rows
dt12 = dt &amp;lt;&amp;lt; Subset( Selected Rows( 1 ), columns( :Pass, :"250 nm"n ) );

// Change 250 nm column name so when joined back to original table
// it will have a unique column name
dt12:"250 nm"n &amp;lt;&amp;lt; Set Name( "Divisor" );

// For alignment reasons, add a new column called Cuvette and set
// it equal to 11
dt12 &amp;lt;&amp;lt; New Column( "Cuvette", Set Each Value( 11 ) );

// Join the tables using the Update Platform
dt &amp;lt;&amp;lt; Update(
	With( dt12 ),
	Match Columns( :Pass = :Pass, :Cuvette = :Cuvette )
);

// Create the new Output column
dt &amp;lt;&amp;lt; New Column( "Output", Set Each Value( :"850 nm"n / :Divisor ) );

// Remove Divisor column since it is no longer needed
dt &amp;lt;&amp;lt; delete columns( Divisor );

// Close the subsetted data table since it is no longer needed
Close( dt12, nosave );

// Remove the Selected RowStates from the data table
dt &amp;lt;&amp;lt; clear rowstates;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Below is a more direct approach, but a far more inefficient method, which yields the same results.&amp;nbsp; On a large data table this method will be quite slow&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
dt = Current Data Table();

dt &amp;lt;&amp;lt; New Column( "Output",
	Formula(
		foundRow = [];
		If( :Cuvette == 11,
			curPass = :Pass;
			foundRow = Current Data Table() &amp;lt;&amp;lt;
			get rows where( :Cuvette == 12 &amp;amp; :Pass == curPass );
			If( N Items( foundRow ) &amp;gt; 0,
				:"850 nm"n / :"250 nm"n[foundRow[1]]
			);
		);
	)
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
    <pubDate>Wed, 02 Apr 2025 11:43:25 GMT</pubDate>
    <dc:creator>txnelson</dc:creator>
    <dc:date>2025-04-02T11:43:25Z</dc:date>
    <item>
      <title>Calculations between rows based on conditions</title>
      <link>https://community.jmp.com/t5/Discussions/Calculations-between-rows-based-on-conditions/m-p/864605#M102819</link>
      <description>&lt;P&gt;Hey, I'm new to JMP scripting, but somewhat used to python language. I'm trying to achieve the following with my dummy test table below:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;1) Cycle through all rows (both before and after the row) and do calculations only on those rows where Cuvette == 11, remaining rows where Cuvette != 11 the "Output" can be set to NA or whatever.&amp;nbsp;&lt;/P&gt;
&lt;P&gt;2) The calcuation itself is between two separate rows. In rows with Cuvette == 11, the 850 nm cell of the row has to be divided with the 250 nm cell from the row that meets two conditions: &lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;a) Cuvette == 12&lt;BR /&gt;&amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp; &amp;nbsp;b) The Pass value has to match up between the rows, e.g. Pass value for cuvette 11 == Pass value for Cuvette 12&amp;nbsp; &amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;TABLE border="1" width="100%"&gt;
&lt;TBODY&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;Pass&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;850 nm&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;250 nm&lt;/TD&gt;
&lt;TD width="18.35390946502058%" height="30px"&gt;Cuvette&lt;/TD&gt;
&lt;TD width="21.64609053497942%" height="30px"&gt;&lt;FONT color="#FF0000"&gt;Output&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;&lt;FONT color="#FF0000"&gt;100&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;200&lt;/TD&gt;
&lt;TD width="18.35390946502058%" height="30px"&gt;11&lt;/TD&gt;
&lt;TD width="21.64609053497942%" height="30px"&gt;&lt;FONT color="#FF0000"&gt;100&lt;/FONT&gt;/&lt;FONT color="#FF99CC"&gt;500 &lt;FONT color="#000000"&gt;= 0,2&lt;/FONT&gt;&lt;/FONT&gt;&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;&lt;FONT color="#FF6600"&gt;400&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;500&lt;/TD&gt;
&lt;TD width="18.35390946502058%" height="30px"&gt;11&lt;/TD&gt;
&lt;TD width="21.64609053497942%" height="30px"&gt;&lt;FONT color="#FF6600"&gt;400&lt;/FONT&gt;/&lt;FONT color="#0000FF"&gt;400&lt;/FONT&gt; = 1&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;&lt;FONT color="#00FFFF"&gt;300&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;100&lt;/TD&gt;
&lt;TD width="18.35390946502058%" height="30px"&gt;11&lt;/TD&gt;
&lt;TD width="21.64609053497942%" height="30px"&gt;&lt;FONT color="#00FFFF"&gt;300&lt;/FONT&gt; / &lt;FONT color="#00FF00"&gt;900&lt;/FONT&gt; = 0.33&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;700&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;&lt;FONT color="#FF99CC"&gt;500&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="18.35390946502058%" height="30px"&gt;12&lt;/TD&gt;
&lt;TD width="21.64609053497942%" height="30px"&gt;NA&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;200&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;&lt;FONT color="#0000FF"&gt;400&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="18.35390946502058%" height="30px"&gt;12&lt;/TD&gt;
&lt;TD width="21.64609053497942%" height="30px"&gt;NA&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;400&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;&lt;FONT color="#00FF00"&gt;900&lt;/FONT&gt;&lt;/TD&gt;
&lt;TD width="18.35390946502058%" height="30px"&gt;12&lt;/TD&gt;
&lt;TD width="21.64609053497942%" height="30px"&gt;NA&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;1&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;400&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;800&lt;/TD&gt;
&lt;TD width="18.35390946502058%" height="30px"&gt;13&lt;/TD&gt;
&lt;TD width="21.64609053497942%" height="30px"&gt;NA&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;2&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;500&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;700&lt;/TD&gt;
&lt;TD width="18.35390946502058%" height="30px"&gt;13&lt;/TD&gt;
&lt;TD width="21.64609053497942%" height="30px"&gt;NA&lt;/TD&gt;
&lt;/TR&gt;
&lt;TR&gt;
&lt;TD width="20%" height="30px"&gt;3&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;300&lt;/TD&gt;
&lt;TD width="20%" height="30px"&gt;700&lt;/TD&gt;
&lt;TD width="18.35390946502058%" height="30px"&gt;13&lt;/TD&gt;
&lt;TD width="21.64609053497942%" height="30px"&gt;NA&lt;/TD&gt;
&lt;/TR&gt;
&lt;/TBODY&gt;
&lt;/TABLE&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;</description>
      <pubDate>Wed, 02 Apr 2025 07:49:59 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Calculations-between-rows-based-on-conditions/m-p/864605#M102819</guid>
      <dc:creator>PaleoHunter</dc:creator>
      <dc:date>2025-04-02T07:49:59Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations between rows based on conditions</title>
      <link>https://community.jmp.com/t5/Discussions/Calculations-between-rows-based-on-conditions/m-p/864633#M102820</link>
      <description>&lt;P&gt;To do this in JMP like fashion, you generally would split your data and then perform the calculation (&lt;LI-MESSAGE title="JMP is Not a Spreadsheet" uid="214307" url="https://community.jmp.com/t5/Discussions/JMP-is-Not-a-Spreadsheet/m-p/214307#U214307" discussion_style_icon_css="lia-mention-container-editor-message lia-img-icon-forum-thread lia-fa-icon lia-fa-forum lia-fa-thread lia-fa"&gt;&lt;/LI-MESSAGE&gt;), then join it back or continue with the split data.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;But you can use also JSL or Formula to do this calculation, but first consider the splitting option.&lt;/P&gt;</description>
      <pubDate>Wed, 02 Apr 2025 08:09:26 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Calculations-between-rows-based-on-conditions/m-p/864633#M102820</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2025-04-02T08:09:26Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations between rows based on conditions</title>
      <link>https://community.jmp.com/t5/Discussions/Calculations-between-rows-based-on-conditions/m-p/864677#M102825</link>
      <description>&lt;P&gt;Welcome to the Community.&lt;/P&gt;
&lt;P&gt;Here is an example of how I would approach your problem&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="txnelson_0-1743592985021.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/74489i51BBE4D597BC3C6F/image-size/medium?v=v2&amp;amp;px=400" role="button" title="txnelson_0-1743592985021.png" alt="txnelson_0-1743592985021.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
dt = Current Data Table();

// Separate out the Cuvette==12 rows into a separate data table
// and then join the Pass==Pass rows to the Cuvette==11 rows
// Then calculate the Output column

// Select the Cuvette==12 rows
dt &amp;lt;&amp;lt; Select Where( :Cuvette == 12 );

// Create a new subsetted data table with those rows
dt12 = dt &amp;lt;&amp;lt; Subset( Selected Rows( 1 ), columns( :Pass, :"250 nm"n ) );

// Change 250 nm column name so when joined back to original table
// it will have a unique column name
dt12:"250 nm"n &amp;lt;&amp;lt; Set Name( "Divisor" );

// For alignment reasons, add a new column called Cuvette and set
// it equal to 11
dt12 &amp;lt;&amp;lt; New Column( "Cuvette", Set Each Value( 11 ) );

// Join the tables using the Update Platform
dt &amp;lt;&amp;lt; Update(
	With( dt12 ),
	Match Columns( :Pass = :Pass, :Cuvette = :Cuvette )
);

// Create the new Output column
dt &amp;lt;&amp;lt; New Column( "Output", Set Each Value( :"850 nm"n / :Divisor ) );

// Remove Divisor column since it is no longer needed
dt &amp;lt;&amp;lt; delete columns( Divisor );

// Close the subsetted data table since it is no longer needed
Close( dt12, nosave );

// Remove the Selected RowStates from the data table
dt &amp;lt;&amp;lt; clear rowstates;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;&amp;nbsp;Below is a more direct approach, but a far more inefficient method, which yields the same results.&amp;nbsp; On a large data table this method will be quite slow&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here( 1 );
dt = Current Data Table();

dt &amp;lt;&amp;lt; New Column( "Output",
	Formula(
		foundRow = [];
		If( :Cuvette == 11,
			curPass = :Pass;
			foundRow = Current Data Table() &amp;lt;&amp;lt;
			get rows where( :Cuvette == 12 &amp;amp; :Pass == curPass );
			If( N Items( foundRow ) &amp;gt; 0,
				:"850 nm"n / :"250 nm"n[foundRow[1]]
			);
		);
	)
);&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Wed, 02 Apr 2025 11:43:25 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Calculations-between-rows-based-on-conditions/m-p/864677#M102825</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2025-04-02T11:43:25Z</dc:date>
    </item>
    <item>
      <title>Re: Calculations between rows based on conditions</title>
      <link>https://community.jmp.com/t5/Discussions/Calculations-between-rows-based-on-conditions/m-p/864782#M102829</link>
      <description>&lt;P&gt;Here is one matrix option, but using JMP features is generally easier to understand&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;Names Default To Here(1);

dt = Current Data Table();

cuvette_start = 11;
cuvette_end = 12;

cuvettes = dt[0, "Cuvette"];

// Data is sorted -&amp;gt; we can just take values and make comparison
start_idx = Loc(cuvettes, cuvette_start);
end_idx = Loc(cuvettes, cuvette_end);

// differences
output = J(1, N Rows(dt), .);
output[start_idx] = dt[start_idx, "850 nm"] :\&lt;/img&gt; dt[end_idx, "250 nm"];
dt &amp;lt;&amp;lt; new column("R", Numeric, Continuous, Values(output));&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;If you can rely on the order heavily, you can use formula like this&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;If(:Cuvette == 11,
	:"850 nm"n / :"250 nm"n[Col Min(If(:Cuvette == 12, Row(), .)) + Row() - 1];
);&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;The type of final formula will heavily depend on your real use case (is data sorted "properly", are you looking for more than one value comparison and so on).&lt;/P&gt;</description>
      <pubDate>Wed, 02 Apr 2025 13:25:26 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Calculations-between-rows-based-on-conditions/m-p/864782#M102829</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2025-04-02T13:25:26Z</dc:date>
    </item>
  </channel>
</rss>

