Hi @NickvanDijk,
Paste this into a new blank query and see if that meets your requirement.
This solution assumes a discount value always directly follows the item line it should be applied to.
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("nVTbCoMwDP0Xnx00Ta+vYzLGQEHGBhP//zdWbQdrUqVO8Mo5J8lJ4jQ1rwH8SSgrRdM2IMIR7stpbLgs30A0c0uAMgHxCzwP/X0YH7f+Gl5OiJyCiSLD+ezGd9dfYgAiDyQPkMZHoC0g5Q8S1zjLg+bA2vCShgedwgMWoLJSFqm7sOEuUneBuas1p9SWp2h5uNVmRRKJyDwT41qrOa02GU2SQXDlBuo9p03rCdywEUIVsU5wZFZkROZVhpoYCXP51E1TkFe5PG9nKJqRdL4AdlveZPKFXSzJ22p594+8rzZn7dJhdwAqB8yyZQaTxp1Ogj2wzI7JCrWxRe6ArK//9Xg6txFK/sKek/ZWc/4A", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Document Nr." = _t, Orderline = _t, Type = _t, #"Item Nr." = _t, #"Discount Code" = _t, #" Line Amount " = _t]),
ChType = Table.TransformColumnTypes(Source,{{"Document Nr.", type text}, {"Orderline", Int64.Type}, {"Type", Int64.Type}, {"Item Nr.", type text}, {"Discount Code", type text}, {" Line Amount ", type number}}),
AddIndex1 = Table.AddIndexColumn(ChType, "Index", 0, 1, Int64.Type),
AddIndex2 = Table.AddIndexColumn(AddIndex1, "Index.1", 1, 1, Int64.Type),
AddCustom = Table.FillUp( Table.AddColumn(AddIndex2, "Custom", each if ChType[Type]{[Index]} = 0 and ChType[Type]{[Index.1]} =3 then null else [Index] ), { "Custom"}),
AddNettAmt = Table.AddColumn(AddCustom, "Nett Amt", each if [Type] =0 then List.Sum( Table.SelectRows( AddCustom, (IT)=> IT[Custom] = [Custom] )[#" Line Amount "] ) else if [Type] =3 then null else [#" Line Amount "], type number),
RemoveColumns = Table.RemoveColumns(AddNettAmt,{"Index", "Index.1", "Custom"})
in
RemoveColumns
.
I hope this is helpful.