Вы можете сделать это, используя M (Power Query Language):
#"Sorted Rows" = Table.Sort(#"Prior Step",{{"Product Number", Order.Ascending}, {"Date", Order.Ascending}}),
#"Added Index" = Table.AddIndexColumn(#"Sorted Rows", "Index", 0, 1),
#"Added Qty Change" = Table.AddColumn(#"Added Index", "Qty Change", each try if #"Added Index"[Product Number]{[Index]-1} = [Product Number] then [Qty] - #"Added Index"[Qty]{[Index]-1} else null otherwise null, type number),
#"Removed Columns" = Table.RemoveColumns(#"Added Qty Change",{"Index"})
Я думаю, что было бы более эффективно сделать это с помощью DAX, в вашей модели данных, однако.Загрузите таблицу в модель данных, затем добавьте вычисляемый столбец:
Qty Change:=
VAR CurrentDate = 'Inventory Balance'[Date]
VAR CurrentProduct = 'Inventory Balance'[Product Number]
VAR PreviousDate =
CALCULATE (
MAX ( 'Inventory Balance'[Date] ),
FILTER (
ALL ( 'Inventory Balance' ),
'Inventory Balance'[Date] < CurrentDate
&& 'Inventory Balance'[Product Number] = CurrentProduct
)
)
VAR PreviousQty =
CALCULATE (
SUM ( 'Inventory Balance'[Qty] ),
FILTER (
ALL ( 'Inventory Balance' ),
'Inventory Balance'[Date] = PreviousDate
&& 'Inventory Balance'[Product Number] = CurrentProduct
)
)
RETURN
'Inventory Balance'[Qty] - PreviousQty