<?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: Formula - How to Find Most Recent Row (Based on Conditions) and Retrieve Value from a different Column in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/Formula-How-to-Find-Most-Recent-Row-Based-on-Conditions-and/m-p/549269#M76577</link>
    <description>&lt;P&gt;I have corrected the format of DateTime in this next version. thanks for your help.&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 26 Sep 2022 14:35:37 GMT</pubDate>
    <dc:creator>altug_bayram</dc:creator>
    <dc:date>2022-09-26T14:35:37Z</dc:date>
    <item>
      <title>Formula - How to Find Most Recent Row (Based on Conditions) and Retrieve Value from a different Column</title>
      <link>https://community.jmp.com/t5/Discussions/Formula-How-to-Find-Most-Recent-Row-Based-on-Conditions-and/m-p/548957#M76560</link>
      <description>&lt;P&gt;Hi - I need help to put a formula to find a most recent row based on conditions and retrieve a different column value / perform calculations between two row parameters (DateTime).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Below data table (also attached) has : Asset, DateTime, Discrete and Action columns .&amp;nbsp;&lt;/P&gt;&lt;P&gt;Data is sorted based on Asset first and then DateTime (ascending)&lt;/P&gt;&lt;P&gt;There is also a Table variable days_threshold = 4.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Action(Desired) is the desired form of Action in which the current formula&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="altug_bayram_1-1664153002694.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/45739i468489FE7FE772BA/image-size/medium?v=v2&amp;amp;px=400" role="button" title="altug_bayram_1-1664153002694.png" alt="altug_bayram_1-1664153002694.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;needs to be modified.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any row that gets a &lt;STRONG&gt;Discrete&lt;/STRONG&gt; = 1 (which is normally determined by a formula rules skipped here and instead sample values provided) , &lt;STRONG&gt;Action&lt;/STRONG&gt; need to start looking to its own prior rows (in &lt;STRONG&gt;DateTime&lt;/STRONG&gt; descending form). Two conditions in that search are :&amp;nbsp;&lt;/P&gt;&lt;P&gt;1- Looking for the same&amp;nbsp;&lt;STRONG&gt;Asset&lt;/STRONG&gt;&amp;nbsp;for which &lt;STRONG&gt;Discrete&lt;/STRONG&gt; =1 (i.e. either XY01 or XY02)&amp;nbsp;&lt;/P&gt;&lt;P&gt;2- Look until most recent value of &lt;STRONG&gt;Action = 1&lt;/STRONG&gt; is found .&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Compute number of days between last time Action was 1 versus DateTime of current row.&amp;nbsp;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;If the difference (in days) &amp;gt;= days_threshold = 4&amp;nbsp; ---&amp;gt; then Action of current row = 1&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;Else if&amp;nbsp;the difference (in days) &amp;lt;&amp;nbsp; days_threshold&amp;nbsp;---&amp;gt; then Action of current row = 0&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;Only interested in finding most recent time when Action was "1" ... Once that single value is found (if exists), search can stop at that point. The limit to search is until the first occurrence of that Asset (ie. oldest record). If Action was never set to "1" earlier, then Action of current row can be set to "1"&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;e.g. row 2 is the first time XY01 gets Action =1 (based on Discrete, which itself is tied to some rules , just showing w/ values in this example) .&amp;nbsp;&lt;/P&gt;&lt;P&gt;next at row 3, Discrete is set to 1 again, but this time Action needs to be 0 as the number of days difference between prior and current rows is 0 (&amp;lt; days_threshold).&amp;nbsp;&lt;/P&gt;&lt;P&gt;Similarly, for row 5, Action still remains 0 as only 3 days elapsed from last time Action was 1.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Finally Action sets to "1" at row 6.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Provided two assets following this idea- set the desired values to Action(Desired).&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I prefer this to be a formula rather than a script.&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks in advance JMP team.&amp;nbsp;&lt;/P&gt;&lt;P&gt;Altug Bayram&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="altug_bayram_0-1664152814899.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/45738i7FAB2AF806FC9C42/image-size/medium?v=v2&amp;amp;px=400" role="button" title="altug_bayram_0-1664152814899.png" alt="altug_bayram_0-1664152814899.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 10 Jun 2023 23:54:40 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Formula-How-to-Find-Most-Recent-Row-Based-on-Conditions-and/m-p/548957#M76560</guid>
      <dc:creator>altug_bayram</dc:creator>
      <dc:date>2023-06-10T23:54:40Z</dc:date>
    </item>
    <item>
      <title>Re: Formula - How to Find Most Recent Row (Based on Conditions) and Retrieve Value from a different Column</title>
      <link>https://community.jmp.com/t5/Discussions/Formula-How-to-Find-Most-Recent-Row-Based-on-Conditions-and/m-p/549025#M76562</link>
      <description>&lt;P&gt;Something like this might work:&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;dt &amp;lt;&amp;lt; New Column("Action", Numeric, Nominal, Formula(
	As Constant(
		latest_ac = 0;
		ret_val = 0;
	);

	If(:Asset == Lag(:Asset),
		If(:Discrete &amp;amp; Date Difference(latest_ac, :DateTime, "day") &amp;gt;= :days_threshold,
			latest_ac = :DateTime;
			ret_val = 1;
		,
			ret_val = 0;
		);
	, 
		latest_ac = 0;
		ret_val = 0;
	);
	ret_val;
));&lt;/CODE&gt;&lt;/PRE&gt;</description>
      <pubDate>Mon, 26 Sep 2022 05:26:11 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Formula-How-to-Find-Most-Recent-Row-Based-on-Conditions-and/m-p/549025#M76562</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2022-09-26T05:26:11Z</dc:date>
    </item>
    <item>
      <title>Re: Formula - How to Find Most Recent Row (Based on Conditions) and Retrieve Value from a different Column</title>
      <link>https://community.jmp.com/t5/Discussions/Formula-How-to-Find-Most-Recent-Row-Based-on-Conditions-and/m-p/549136#M76571</link>
      <description>&lt;P&gt;I have a question about your DateTime column.&amp;nbsp; As you have it defined, all of the values for the Asset = "XY01" were measured on the 6th day of various months for the year 2022.&lt;/P&gt;
&lt;P&gt;row 3 = February 6th, 2022&lt;/P&gt;
&lt;P&gt;row 5 = May 6th, 2022&lt;/P&gt;
&lt;P&gt;which is 89 days appart, yet you indicate the desired action is 0.&lt;/P&gt;
&lt;P&gt;&lt;a href="https://community.jmp.com/t5/user/viewprofilepage/user-id/14366"&gt;@jthi&lt;/a&gt;&amp;nbsp;response shows an Action of 1, as does my own independent calculation.&lt;/P&gt;
&lt;P&gt;What are we missing?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Sep 2022 11:33:33 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Formula-How-to-Find-Most-Recent-Row-Based-on-Conditions-and/m-p/549136#M76571</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2022-09-26T11:33:33Z</dc:date>
    </item>
    <item>
      <title>Re: Formula - How to Find Most Recent Row (Based on Conditions) and Retrieve Value from a different Column</title>
      <link>https://community.jmp.com/t5/Discussions/Formula-How-to-Find-Most-Recent-Row-Based-on-Conditions-and/m-p/549263#M76575</link>
      <description>&lt;P&gt;The "days" constraint measures in calendar time - "how many days elapsed since time Action was 1" is the question.&amp;nbsp;&lt;/P&gt;&lt;P&gt;I realize I put the format on DateTime column wrong as D/M/Y&amp;nbsp;&lt;/P&gt;&lt;P&gt;It should have been M/D/Y ....&amp;nbsp;&lt;/P&gt;&lt;P&gt;My apologies for the mistake.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;so rows 3 and 5 are in June 2nd and 5th respectively.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Would it be possible to create a formula for this ?&lt;/P&gt;&lt;P&gt;thx JMP team.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Sep 2022 14:25:53 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Formula-How-to-Find-Most-Recent-Row-Based-on-Conditions-and/m-p/549263#M76575</guid>
      <dc:creator>altug_bayram</dc:creator>
      <dc:date>2022-09-26T14:25:53Z</dc:date>
    </item>
    <item>
      <title>Re: Formula - How to Find Most Recent Row (Based on Conditions) and Retrieve Value from a different Column</title>
      <link>https://community.jmp.com/t5/Discussions/Formula-How-to-Find-Most-Recent-Row-Based-on-Conditions-and/m-p/549269#M76577</link>
      <description>&lt;P&gt;I have corrected the format of DateTime in this next version. thanks for your help.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Sep 2022 14:35:37 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Formula-How-to-Find-Most-Recent-Row-Based-on-Conditions-and/m-p/549269#M76577</guid>
      <dc:creator>altug_bayram</dc:creator>
      <dc:date>2022-09-26T14:35:37Z</dc:date>
    </item>
    <item>
      <title>Re: Formula - How to Find Most Recent Row (Based on Conditions) and Retrieve Value from a different Column</title>
      <link>https://community.jmp.com/t5/Discussions/Formula-How-to-Find-Most-Recent-Row-Based-on-Conditions-and/m-p/549270#M76578</link>
      <description>&lt;P&gt;Pls see below for my next version w/ the corrected DateTime format (it should have been M/D/Y ... )&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am looking for a formula that can do this, if possible. thx.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Sep 2022 14:36:46 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Formula-How-to-Find-Most-Recent-Row-Based-on-Conditions-and/m-p/549270#M76578</guid>
      <dc:creator>altug_bayram</dc:creator>
      <dc:date>2022-09-26T14:36:46Z</dc:date>
    </item>
    <item>
      <title>Re: Formula - How to Find Most Recent Row (Based on Conditions) and Retrieve Value from a different Column</title>
      <link>https://community.jmp.com/t5/Discussions/Formula-How-to-Find-Most-Recent-Row-Based-on-Conditions-and/m-p/549318#M76580</link>
      <description>&lt;P&gt;My formula should still work just fine, as it was already using days instead of months (I did conversion in my test table, but forgot about it). Action2 is the result from the formula I did provide and there is no difference between it and the Action(Desired) column:&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jthi_1-1664205365805.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/45775i57B5472E1366A8B4/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jthi_1-1664205365805.png" alt="jthi_1-1664205365805.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;P&gt;I suggest checking out Scripting Index for As Constant, Date Difference and Lag. Those should help in with the understanding of the formula&lt;/P&gt;</description>
      <pubDate>Mon, 26 Sep 2022 15:17:41 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Formula-How-to-Find-Most-Recent-Row-Based-on-Conditions-and/m-p/549318#M76580</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2022-09-26T15:17:41Z</dc:date>
    </item>
    <item>
      <title>Re: Formula - How to Find Most Recent Row (Based on Conditions) and Retrieve Value from a different Column</title>
      <link>https://community.jmp.com/t5/Discussions/Formula-How-to-Find-Most-Recent-Row-Based-on-Conditions-and/m-p/549327#M76581</link>
      <description>&lt;P&gt;Hi , could you pls attach your version so I can see how the script is integrated into the formula. I am novice in putting scripts into formulas.&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Sep 2022 15:36:16 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Formula-How-to-Find-Most-Recent-Row-Based-on-Conditions-and/m-p/549327#M76581</guid>
      <dc:creator>altug_bayram</dc:creator>
      <dc:date>2022-09-26T15:36:16Z</dc:date>
    </item>
    <item>
      <title>Re: Formula - How to Find Most Recent Row (Based on Conditions) and Retrieve Value from a different Column</title>
      <link>https://community.jmp.com/t5/Discussions/Formula-How-to-Find-Most-Recent-Row-Based-on-Conditions-and/m-p/549328#M76582</link>
      <description>&lt;P&gt;Just the formula part would be this and you should be able to add it directly to the Formula from Column Info after you have created new column&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;As Constant(
	latest_ac = 0;
	ret_val = 0;
);

If(:Asset == Lag(:Asset),
	If(:Discrete &amp;amp; Date Difference(latest_ac, :DateTime, "day") &amp;gt;= :days_threshold,
		latest_ac = :DateTime;
		ret_val = 1;
	,
		ret_val = 0;
	);
, 
	latest_ac = 0;
	ret_val = 0;
);
ret_val;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;I have also attached the table. And if you are very unfamiliar with formulas, you can read some parts of this &lt;A href="https://www.jmp.com/support/help/en/16.2/index.shtml#page/jmp/create-formulas-in-jmp.shtml" target="_blank" rel="noopener"&gt; Using JMP &amp;gt; Create Formulas in JMP&lt;/A&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Sep 2022 15:43:56 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Formula-How-to-Find-Most-Recent-Row-Based-on-Conditions-and/m-p/549328#M76582</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2022-09-26T15:43:56Z</dc:date>
    </item>
    <item>
      <title>Re: Formula - How to Find Most Recent Row (Based on Conditions) and Retrieve Value from a different Column</title>
      <link>https://community.jmp.com/t5/Discussions/Formula-How-to-Find-Most-Recent-Row-Based-on-Conditions-and/m-p/549329#M76583</link>
      <description>&lt;P&gt;Also, I use Lag, Date Difference plenty times in formulas . But as this does become a bit involved as a code and due to my lack of experience in integrating scripts into formulas, I struggle a bit on that front. If the intent was on number of records rather than calendar days, I could do it easily due to available index function. "Day" requirement makes it harder.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Sep 2022 15:46:06 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Formula-How-to-Find-Most-Recent-Row-Based-on-Conditions-and/m-p/549329#M76583</guid>
      <dc:creator>altug_bayram</dc:creator>
      <dc:date>2022-09-26T15:46:06Z</dc:date>
    </item>
    <item>
      <title>Re: Formula - How to Find Most Recent Row (Based on Conditions) and Retrieve Value from a different Column</title>
      <link>https://community.jmp.com/t5/Discussions/Formula-How-to-Find-Most-Recent-Row-Based-on-Conditions-and/m-p/549375#M76585</link>
      <description>&lt;P&gt;Jarmo&amp;nbsp;&lt;/P&gt;&lt;P&gt;This worked excellent.&amp;nbsp; It also opened my understanding of&amp;nbsp; formulas to contain more complex logic when separated by ";".&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, it is interesting that 0 in this case works as a starting reset for a DateTime parameter.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Jarmo/Jim - thanks for extremely quick help on this... appreciate the support.&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 26 Sep 2022 16:45:02 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/Formula-How-to-Find-Most-Recent-Row-Based-on-Conditions-and/m-p/549375#M76585</guid>
      <dc:creator>altug_bayram</dc:creator>
      <dc:date>2022-09-26T16:45:02Z</dc:date>
    </item>
  </channel>
</rss>

