<?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 'n' row average of a column based on a condition from other column - in Discussions</title>
    <link>https://community.jmp.com/t5/Discussions/n-row-average-of-a-column-based-on-a-condition-from-other-column/m-p/446805#M69383</link>
    <description>&lt;P&gt;Hi All,&amp;nbsp;&lt;BR /&gt;I am looking for&amp;nbsp; 'n' row average (in the attached example, n == 3) based on other coulmn (-- "Customer_ID"). If my row number for each group (customer) is integer multiplier of 'n' i.e. [Row == Integer Constant x n]. My basic formula works fine. But if Row != Integer Constant x n, it takes remaining rows from other group. Which is giving me wrong results.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;What I want -&lt;BR /&gt;Best Case - Calculate 'n' row average on sort of subset of the data based on Cutomer_ID and for the last remaining rows put the average value from previous averaged value.&lt;BR /&gt;Otherwise - leave the empty values as it is.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;I tried to put&amp;nbsp; &amp;nbsp;:Customer_ID != Lag(:Customer_ID, -2) but it did not work for me. I can split the table on Customer_ID and then do the calculation but - there are more than 5000 ID and around 20 columns for average to be done. It doesn't seems the best idea !&lt;BR /&gt;&lt;BR /&gt;Any help please ?&lt;BR /&gt;Thanks.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="HSS_0-1639861386038.png" style="width: 788px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/38516i3302E91D80975ABA/image-dimensions/788x385?v=v2" width="788" height="385" role="button" title="HSS_0-1639861386038.png" alt="HSS_0-1639861386038.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Fri, 09 Jun 2023 18:07:40 GMT</pubDate>
    <dc:creator>HSS</dc:creator>
    <dc:date>2023-06-09T18:07:40Z</dc:date>
    <item>
      <title>'n' row average of a column based on a condition from other column -</title>
      <link>https://community.jmp.com/t5/Discussions/n-row-average-of-a-column-based-on-a-condition-from-other-column/m-p/446805#M69383</link>
      <description>&lt;P&gt;Hi All,&amp;nbsp;&lt;BR /&gt;I am looking for&amp;nbsp; 'n' row average (in the attached example, n == 3) based on other coulmn (-- "Customer_ID"). If my row number for each group (customer) is integer multiplier of 'n' i.e. [Row == Integer Constant x n]. My basic formula works fine. But if Row != Integer Constant x n, it takes remaining rows from other group. Which is giving me wrong results.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;What I want -&lt;BR /&gt;Best Case - Calculate 'n' row average on sort of subset of the data based on Cutomer_ID and for the last remaining rows put the average value from previous averaged value.&lt;BR /&gt;Otherwise - leave the empty values as it is.&lt;/P&gt;&lt;P&gt;&lt;BR /&gt;I tried to put&amp;nbsp; &amp;nbsp;:Customer_ID != Lag(:Customer_ID, -2) but it did not work for me. I can split the table on Customer_ID and then do the calculation but - there are more than 5000 ID and around 20 columns for average to be done. It doesn't seems the best idea !&lt;BR /&gt;&lt;BR /&gt;Any help please ?&lt;BR /&gt;Thanks.&lt;BR /&gt;&lt;BR /&gt;&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="HSS_0-1639861386038.png" style="width: 788px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/38516i3302E91D80975ABA/image-dimensions/788x385?v=v2" width="788" height="385" role="button" title="HSS_0-1639861386038.png" alt="HSS_0-1639861386038.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Jun 2023 18:07:40 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/n-row-average-of-a-column-based-on-a-condition-from-other-column/m-p/446805#M69383</guid>
      <dc:creator>HSS</dc:creator>
      <dc:date>2023-06-09T18:07:40Z</dc:date>
    </item>
    <item>
      <title>Re: 'n' row average of a column based on a condition from other column -</title>
      <link>https://community.jmp.com/t5/Discussions/n-row-average-of-a-column-based-on-a-condition-from-other-column/m-p/446825#M69384</link>
      <description>&lt;P&gt;Here is the formula that I came up with&lt;/P&gt;
&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="txnelson_0-1639887031326.png" style="width: 701px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/38517i6D162C8BC765C1E3/image-dimensions/701x275?v=v2" width="701" height="275" role="button" title="txnelson_0-1639887031326.png" alt="txnelson_0-1639887031326.png" /&gt;&lt;/span&gt;&lt;/P&gt;
&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;If( Row() == 1 | :Customer_ID != Lag( :Customer_ID ),
	count = 0
);
If( count == 3, count = 0 );
count = count + 1;
If(
	count == 1,
		currentAvg = Col Moving Average( :Value, 1, before = 0, after = 2, :Customer_ID );
		display = currentAvg;,
	:Customer_ID == Lag( :Customer_ID, -2 ), display = .,
	display = currentAvg
);
display;&lt;/CODE&gt;&lt;/PRE&gt;
&lt;P&gt;You need to take the time to read the Scripting Guide found in the JMP Documentation Library available under the Help pull down menu!&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 19 Dec 2021 04:12:33 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/n-row-average-of-a-column-based-on-a-condition-from-other-column/m-p/446825#M69384</guid>
      <dc:creator>txnelson</dc:creator>
      <dc:date>2021-12-19T04:12:33Z</dc:date>
    </item>
    <item>
      <title>Re: 'n' row average of a column based on a condition from other column -</title>
      <link>https://community.jmp.com/t5/Discussions/n-row-average-of-a-column-based-on-a-condition-from-other-column/m-p/446832#M69385</link>
      <description>&lt;P&gt;No idea if this will work in all cases, but it does work with the provided example data:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;PRE&gt;&lt;CODE class=" language-jsl"&gt;If(Col Cumulative Sum(1, :Customer_ID) &amp;gt; Col Number(:Value, :Customer_ID) - 1,
	Lag(Col Moving Average(:Value, 1, 0, 2, 0, :Customer_ID), 1),
	If(Modulo(Col Cumulative Sum(1, :Customer_ID), 3) == 1,
		Col Moving Average(:Value, 1, 0, 2, 0, :Customer_ID),
		.
	)
)&lt;/CODE&gt;&lt;/PRE&gt;&lt;P&gt;Check what each of the functions/formulas do from scripting index / scripting guide &lt;STRONG&gt;while&lt;/STRONG&gt; checking selected subexpression result from formula editor to understand what it is doing as it is quite messy:&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="jthi_0-1639898755780.png" style="width: 400px;"&gt;&lt;img src="https://community.jmp.com/t5/image/serverpage/image-id/38518i84CCA5B58698C580/image-size/medium?v=v2&amp;amp;px=400" role="button" title="jthi_0-1639898755780.png" alt="jthi_0-1639898755780.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sun, 19 Dec 2021 07:28:43 GMT</pubDate>
      <guid>https://community.jmp.com/t5/Discussions/n-row-average-of-a-column-based-on-a-condition-from-other-column/m-p/446832#M69385</guid>
      <dc:creator>jthi</dc:creator>
      <dc:date>2021-12-19T07:28:43Z</dc:date>
    </item>
  </channel>
</rss>

