Forum Discussion
PhyuLayKhine333
3 years agoHelper I
FIFO Cost
Hi All Experts, I am beginner to BI and tried many methods in order to get the below result and also google about this problem but i did not get the correct answer and have no ideas how to use th...
- 3 years ago
Power Query may not be the best solution, but I made it work
let Purchase = Excel.CurrentWorkbook(){[Name="Purchase"]}[Content], Sales = Excel.CurrentWorkbook(){[Name="Sales"]}[Content], Custom1 = Table.AddColumn( Table.Group( Sales, "Product Name", { "S", each _ } ), "P", (x)=> List.Select( Table.ToRows( Purchase ), (y)=> y{1} = x[Product Name] ) ), Custom2 = Table.ToList( Custom1, (t)=> let f = (p)=> List.Accumulate( t{2}, { 0, 0 }, (x,y)=> if x{1} + y{2} < p then { x{0} + y{2} * y{3}, x{1} + y{2} } else { x{0} + List.Max( { 0, p - x{1} } ) * y{3}, x{1} + y{2} } ){0}, a = Table.AddIndexColumn( t{1}, "i", 1, 1 ), b = Table.AddColumn( a, "q", each f( List.Sum( Table.FirstN( a, [i] )[Quantity] ) ) ), c = Table.AddColumn( b, "amount", each if [i] = 1 then [q] else [q] - b[q]{[i]-2} ), l = List.Transform( List.Zip( List.Transform( t{2}, (x)=> { x{2}, x{2} * x{3} } ) ), List.Sum ), d = Table.AddColumn( c, "Qty", each l{0} - List.Sum( Table.FirstN( c, [i] )[Quantity] ) ), e = Table.AddColumn( d, "Value", each l{1} - List.Sum( Table.FirstN( c, [i] )[amount] ) ) in Table.RemoveColumns( e, { "i", "q" } ) ), Custom3 = Table.Combine( Custom2 ) in Custom3
kirete17
3 years agoFrequent Visitor
Power Query may not be the best solution, but I made it work
let
Purchase = Excel.CurrentWorkbook(){[Name="Purchase"]}[Content],
Sales = Excel.CurrentWorkbook(){[Name="Sales"]}[Content],
Custom1 =
Table.AddColumn(
Table.Group( Sales, "Product Name", { "S", each _ } ),
"P",
(x)=> List.Select( Table.ToRows( Purchase ), (y)=> y{1} = x[Product Name] )
),
Custom2 =
Table.ToList(
Custom1,
(t)=>
let
f =
(p)=>
List.Accumulate(
t{2},
{ 0, 0 },
(x,y)=>
if x{1} + y{2} < p
then { x{0} + y{2} * y{3}, x{1} + y{2} }
else { x{0} + List.Max( { 0, p - x{1} } ) * y{3}, x{1} + y{2} }
){0},
a =
Table.AddIndexColumn( t{1}, "i", 1, 1 ),
b =
Table.AddColumn( a, "q", each f( List.Sum( Table.FirstN( a, [i] )[Quantity] ) ) ),
c =
Table.AddColumn( b, "amount", each if [i] = 1 then [q] else [q] - b[q]{[i]-2} ),
l =
List.Transform( List.Zip( List.Transform( t{2}, (x)=> { x{2}, x{2} * x{3} } ) ), List.Sum ),
d =
Table.AddColumn( c, "Qty", each l{0} - List.Sum( Table.FirstN( c, [i] )[Quantity] ) ),
e =
Table.AddColumn( d, "Value", each l{1} - List.Sum( Table.FirstN( c, [i] )[amount] ) )
in
Table.RemoveColumns( e, { "i", "q" } )
),
Custom3 =
Table.Combine( Custom2 )
in
Custom3