Forum Discussion

Oadr21's avatar
Oadr21
Regular Visitor
2 years ago
Solved

Referencing Cells in Power Query

I am having this trouble to reference cells in Power Query like in Excel. So I have the following table in Excel: Where the formula for the Finish Qty column is the following: =[@[Initial Qty...
  • ronrsnfld's avatar
    2 years ago

    You can use the List.Generate function to accomplish the same result.

    I started off with just your In Process and Scrap Qty columns.

     

    And I stored the first "Initial Qty" value of 86 as a Parameter named Initial since you said you got that from someplace else.

     

    Then, with this code:

     

    let
        Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"In Process", Int64.Type}, {"Scrap Qty", Int64.Type}}),
        #"Initial/Finished" = List.Generate(
            ()=>[i=Initial, f=Initial - #"Changed Type"[In Process]{0} - #"Changed Type"[Scrap Qty]{0}, idx=0],
            each [idx] < Table.RowCount(#"Changed Type"),
            each [i=[f], f = [f]-#"Changed Type"[In Process]{[idx]+1} - #"Changed Type"[Scrap Qty]{[idx]+1}, idx=[idx]+1 ],
            each {[i],[f]}),
        
        #"Add Initial/Finished" = Table.FromColumns(
            Table.ToColumns(#"Changed Type") &
            {List.Transform(#"Initial/Finished", each _{0})} &
            {List.Transform(#"Initial/Finished", each _{1})},
            type table[In Process=Int64.Type, Scrap Qty=Int64.Type,Initial Qty=Int64.Type, Finish Qty=Int64.Type]),
        #"Reordered Columns" = Table.ReorderColumns(#"Add Initial/Finished",{"Initial Qty", "In Process", "Finish Qty", "Scrap Qty"})
    
    in  
        #"Reordered Columns"

     

    you obtain this result:

     

     

  • Anonymous's avatar
    Anonymous
    2 years ago

    Hi Oadr21 ,

    ronrsnfld lbendlin Thanks for your concern about this case!
    And Oadr21 another solution:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjJQ0lEyUIrViVZCsIyALEMwyxAhaIGi0BBNCy4x/CxjLGKmaOYZmxBjYCwA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"In Process" = _t, #"Scrap Qty" = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"In Process", Int64.Type}, {"Scrap Qty", Int64.Type}}),
        #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1, Int64.Type),
        #"Added Custom" = Table.AddColumn(#"Added Index", "Initial Qty", each let 
    currentIndex = [Index]
    in
    if [Index] = 1 then 86 else 86 - List.Sum(
        Table.SelectRows(#"Added Index", each [Index] <= currentIndex)[In Process]) - List.Sum(
            Table.SelectRows(#"Added Index", each [Index] <= currentIndex)[Scrap Qty]
        )),
        #"Added Custom1" = Table.AddColumn(#"Added Custom", "Finish Qty", each let 
       currentIndex = [Index],
       nextRow = Table.SelectRows(#"Added Custom", each [Index] = currentIndex +1)
    in
       if Table.IsEmpty(nextRow) then 0 else Record.Field(nextRow{0}, "Initial Qty"))
    in
        #"Added Custom1"

    Output:


    Best Regards,
    Dino Tao
    If this post helps, then please consider Accept all of the answers as the solution to help the other members find it more quickly.