帶條件的前一行值的滾動總和我的資料以兩列開頭:批次號碼和數量。
我需要幫助創建腳本來計算前一行值的滾動總和(當前行 + 前 2 行)。
邏輯解釋如下表。

進階統計建模自動化和腳本編寫大規模客製化回复:具有條件的前一行值的滾動總和感謝您發布問題。 這是一個有趣但需要解決的曲折問題。
我向 JSL 添加了一些註釋,希望它是有意義的,而不只是看起來像一個巨大的魔術公式。
老實說,起初我認為這幾乎是不可能解決的,但喝了一大杯咖啡後,……還不錯。
回复:具有條件的前一行值的滾動總和謝謝拜倫。
回复:具有條件的前一行值的滾動總和將此腳本添加到您的數據表並試一試
dt1=current data table();
//I want to get the max quantity by lot, so I'm adding a new column to change all the lots back to their non-decimal value.
dt1<< New Column("Lot Actual", formula(If( :Qty == Col Maximum( :Qty, Char( Round( Num( :Lot# ), 0 ) ) ),
:Lot#,
.
)));<BR />//this next formula column excludes rows that aren't the max Qty by lot
dt1<< New Column( "Lot Values in One Formula",
Formula(
If( :Lot Actual == "",
Row State( row() ) = Excluded State( 1 );
Excluded( Row State( row() ) );
1;
)
)
);
dt1<<RUN formulas=""></RUN>//once all the formulas for tagging the data are added, we can use Tabulate to get a subset of just the important rows.<BR />//There are lots of other ways to do this, this just happened to be the method of the day
obj1=dt1 << Tabulate(
Add Table(
Column Table( Analysis Columns( :Qty ) ),
Row Table( Grouping Columns( :Lot# ) )
)
);
dt2=obj1 << Make Into Data Table;
column(dt2,2)<<SET name=""></SET><BR />//Finally here is the rolling sum of the last three lots<BR />//there is moving average function we could have used, it would have looked simpler but more abstract
dt2<<NEW column=""></NEW>//<BR />//This part joins the values from the separate table back to the original table<BR />
dt1 << Update(
With( dt2 ),
Match Columns( :Lot# = :Lot# )
);
close(dt2, nosave);
obj1<<CLOSE window=""></CLOSE>//I feel like this step is cheating a little. This step just repeats the missing values down to the next non-missing<BR />//row. Its some code I got from Brady Brady a ways back. Different iterations of come in handy from time to time.
<BR />dtnr=current data table();
dtcollist=dtnr<<GET column="" names=""></GET>回复:具有條件的前一行值的滾動總和對就這樣。
但是我想維護原始數據表而不排除一些行來得到這個結果。
這可以做到嗎?
回复:具有條件的前一行值的滾動總和這是正確的嗎。 (圖中最右邊一欄)?

回复:具有條件的前一行值的滾動總和我會弄清楚如何得到這個。
以防萬一有人對此有解決方案,也請分享。
謝謝。
回复:具有條件的前一行值的滾動總和對,我現在明白了。
需要找到每手的最大值,然後取當前和最後兩個最大手數的滾動總和。
我覺得有一個潛在的解決方案,可能需要多考慮一下。
回复:具有條件的前一行值的滾動總和感謝您證明使用公式的想法。
我的數據集無法直接使用公式進行滾動求和,因為一些批次數據未被計算在內。
例子,
Lot# 3 和 Lot# 3.2,只有 Lot# 3.2 數量被計算在滾動總和中,因為它是最終批次數量。
Lot#3 中添加了額外的數量。 它成為 Lot#3.2。
Lot# 7、7,2、7,3 和 Lot# 10、10.3 相同。
Lot# 7.3 數量和 10.3 計入滾動總和,因為它是最終批次數量。
需要社區的指導。

回复:具有條件的前一行值的滾動總和一種方法可能是使用這樣的公式:

If( Row() == 1,
:Qty,
If( Row() == 2,
Lag( :Qty, 1 ) + :Qty,
:Qty + Lag( :Qty, 1 ) + Lag( :Qty, 2 )
)
)
對於第一行,沒有什麼可以求和的,所以它只使用第 1 行
對於第二行只有兩個值,當前值和上一個值,所以它的 row+lag(1)
第二個之後的所有行都採用當前行加上前兩行的總和:lag(1) 和 lag(2)