Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
PravenBabu
Regular Visitor

Add Values in a Column by matching conditions in other Columns and Rows

In Power query, i  have a large database of transactions in the format as below

DateItem CodeIn QtyOut Qty
1/12/2024A100 
1/14/2024A 50
1/13/2024B200 
1/14/2024B 50
1/14/2024B 50
1/15/2024A50 
1/15/2024A 50

 I need to add the value in a new column also with the value from the past dates matching the item code as below

DateItem CodeIn QtyOut QtyNET
1/12/2024A100 100
1/14/2024A 5050
1/13/2024B200 200
1/14/2024B 50150
1/14/2024B 50100
1/15/2024A50 100
1/15/2024A 5050

Please advise

1 ACCEPTED SOLUTION
AlienSx
Super User
Super User

ff1.png

 

 

 

let
    fx = (x) => 
        [sorted_list = List.Buffer(Table.ToList(Table.Sort(x, "idx"), (x) => x)),
        rt = List.Generate(
            () => [i = 0, r = sorted_list{0}, net = List.Sum({r{2}, - r{3}})],
            (x) => x[i] < List.Count(sorted_list),
            (x) => [i = x[i] + 1, r = sorted_list{i}, net = List.Sum({x[net], r{2}, - r{3}})],
            (x) => x[r] & {x[net]}
        )
        ][rt],
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    idx = Table.AddIndexColumn(Source, "idx"),
    blank_to_nulls = Table.TransformColumns(idx, {}, (x) => if x = " " then null else x),
    group = Table.Group(blank_to_nulls, "Item Code", {"x", fx}),
    result = Table.RemoveColumns(
        Table.Sort(
            Table.FromList(List.Combine(group[x]), (x) => x, Table.ColumnNames(Source) & {"idx", "NET"}),
            "idx"
        ),
        "idx"
    )
in
    result

 

tt1.png

View solution in original post

5 REPLIES 5
PravenBabu
Regular Visitor

@Omid_Motamedise @lbendlin @AlienSx @Anonymous Thanks for all your support. I used @AlienSx Solution and got the desired result good and fast. 

Anonymous
Not applicable

Hi @PravenBabu 

 

Hope these help:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ3NNI3MjAyUdJRcgRiQwMDIKkUqwOWMkGWAiJTA5iMMUzGCYiNsGtyQteEW8YU2SJTZNNMsTghFgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, #"Item Code" = _t, #"In Qty" = _t, #"Out Qty" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Item Code", type text}, {"In Qty", Int64.Type}, {"Out Qty", Int64.Type}}),
    #"Sorted Rows" = Table.Sort(#"Changed Type",{{"Item Code", Order.Ascending}, {"Date", Order.Ascending}}),
    #"Replaced Value" = Table.ReplaceValue(#"Sorted Rows",null,0,Replacer.ReplaceValue,{"In Qty", "Out Qty"}),
    #"Grouped Rows" = Table.Group(#"Replaced Value", {"Item Code"}, {{"Data", each Table.AddIndexColumn(_, "Index", 1, 1, Int64.Type)}}),
    Custom1 = Table.TransformColumns(#"Grouped Rows",{"Data", (x)=> Table.AddColumn(x , "NET", each List.Sum(List.FirstN(x[In Qty],[Index]))-List.Sum(List.FirstN(x[Out Qty],[Index]) ))}),
    #"Expanded Data" = Table.ExpandTableColumn(Custom1, "Data", {"Date", "In Qty", "Out Qty", "NET"}, {"Date", "In Qty", "Out Qty", "NET"})
in
    #"Expanded Data"

vcgaomsft_0-1730858108540.png

Best Regards,
Gao

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum

Omid_Motamedise
Super User
Super User

Just copy the below code and past it into the advance editor

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ3NNI3MjAyUdJRcgRiQwMDIKmgFKsDljNBllMAYlMDmJQxTMoJiI1waHPC0IZHyhTZMlMUA02xuSMWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, #"Item Code" = _t, #"In Qty" = _t, #"Out Qty" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"In Qty", Int64.Type}, {"Out Qty", Int64.Type}}),
    Index = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(Index, "Custom", each [a=Table.SelectRows(Table.FirstN(Index,[Index]),(x)=> x[Item Code]=[Item Code]),b=List.Sum(a[In Qty])-(List.Sum(a[Out Qty])??0)][b])
in
    #"Added Custom"

 

If my answer helped solve your issue, please consider marking it as the accepted solution. It helps others in the community find answers faster—and keeps the community growing stronger!
You can also check out my YouTube channel for tutorials, tips, and real-world solutions in Power Query with the following link
https://youtube.com/@omidbi?si=96Bo-ZsSwOx0Z36h
AlienSx
Super User
Super User

ff1.png

 

 

 

let
    fx = (x) => 
        [sorted_list = List.Buffer(Table.ToList(Table.Sort(x, "idx"), (x) => x)),
        rt = List.Generate(
            () => [i = 0, r = sorted_list{0}, net = List.Sum({r{2}, - r{3}})],
            (x) => x[i] < List.Count(sorted_list),
            (x) => [i = x[i] + 1, r = sorted_list{i}, net = List.Sum({x[net], r{2}, - r{3}})],
            (x) => x[r] & {x[net]}
        )
        ][rt],
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    idx = Table.AddIndexColumn(Source, "idx"),
    blank_to_nulls = Table.TransformColumns(idx, {}, (x) => if x = " " then null else x),
    group = Table.Group(blank_to_nulls, "Item Code", {"x", fx}),
    result = Table.RemoveColumns(
        Table.Sort(
            Table.FromList(List.Combine(group[x]), (x) => x, Table.ColumnNames(Source) & {"idx", "NET"}),
            "idx"
        ),
        "idx"
    )
in
    result

 

tt1.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ3NNI3MjAyUdJRcgRiQwMDIKmgFKsDljNBllMAYlMDmJQxTMoJiI1waHPC0IZHyhTZMlMUA02xuSMWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, #"Item Code" = _t, #"In Qty" = _t, #"Out Qty" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"In Qty", Int64.Type}, {"Out Qty", Int64.Type}, {"Date", type date}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Net", (k)=> let t = Table.SelectRows(#"Added Index", each [Item Code]=k[Item Code] and [Index]<=k[Index] )   
in (List.Sum(t[In Qty])??0)-(List.Sum(t[Out Qty])??0) 
,Int64.Type)
in
    #"Added Custom"

 

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

Find out what's new and trending in the Fabric community.

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.

Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

Top Solution Authors
Top Kudoed Authors