有條件的前一行值的總和我的數據以兩列開始,批號和數量。
我需要幫助來創建腳本來計算前一行值的滾動總和(當前行+前2行)。
邏輯說明如下表所示。

腳本編寫回复:帶條件的前一行值的總和一種方法是使用如下公式:

If( Row() == 1,
:Qty,
If( Row() == 2,
Lag( :Qty, 1 ) + :Qty,
:Qty + Lag( :Qty, 1 ) + Lag( :Qty, 2 )
)
)
對於第一行,沒有什麼可累加的,因此僅使用第一行
對於第二行,只有兩個值,即當前值和上一個值,因此其row + lag(1)
第二行之後的所有行將取當前行加上前兩行的總和:lag(1)和lag(2)
回复:帶條件的前一行值的總和感謝您證明使用公式的想法。
我的數據集無法直接使用公式進行累加總和,因為一些手腳數據沒有計算在內。
例,
批號3和批號3.2,由於批號為最終批號,因此只有批號3.2數量計入滾動總和。
批號3中增加了額外數量。 成為拍品編號3.2。
批號7、7、2、7、3和批號10、10.3相同。
批號7.3數量和10.3由於是最終批數量而計入滾動總和。
需要社區的指導。

回复:帶條件的前一行值的總和是的,我現在明白了。
需要找到每個批次的最大值,然後取當前和最後兩個最大批次值的滾動總和。
我覺得有一個潛伏的解決方案,可能需要對此進行更多思考。
回复:帶條件的前一行值的總和我會弄清楚如何得到這個。
萬一有人對此有解決方案,請也分享。
謝謝。
回复:帶條件的前一行值的總和這是正確的嗎。 (圖片中最右邊的列)?

回复:帶條件的前一行值的總和是的,這是正確的。
但是我想保持原始數據表而不排除一些行來獲得此結果。
能做到嗎?
回复:帶條件的前一行值的總和將此腳本添加到數據表中並進行旋轉
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>