cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
MirageZX
Frequent Visitor

Power Query Running_On_hand

Hi, I am new in Power Query and I have been struggling to find out how to create a Column name "Running_On_hand". The source data are two tables: 

 

MaterialRes_DateReservation_NumberReq_Qty
102928917/3/20215632311
1029289110/10/202223650561
100239877/3/20215625391
102722997/3/20215552733
1027229912/24/202227011128
102722992/13/2023294404510
102722992/16/202329590991
102722993/2/202330212085
102722993/9/2023305071530
102722993/23/202331179945
102722994/3/202331647705
101952497/8/20215671841
101952497/8/20215679121
110039807/8/2021558134750

Table1

 

MaterialOn_Hand_Qty
102928912
100239871
102722991
101952492
110039801500

Table2

 

The goal is to obtain a new Column that refer to the On_hand_Qty from Table2 as initial and go subtracting the Req_Qty for each material. 

 

MaterialRe_Creation_DReservation_NumberReq_QtyRunning_Stock
102928917/3/202156323111
1029289110/10/2022236505610
100239877/3/202156253910
102722997/3/20215552733-2
1027229912/24/202227011128-10
102722992/13/2023294404510-20
102722992/16/202329590991-21
102722993/2/202330212085-26
102722993/9/2023305071530-56
102722993/23/202331179945-61
102722994/3/202331647705-66
101952497/8/202156718411
101952497/8/202156791210
110039807/8/2021558134750750

New Column demonstration

 

Any help will be highly appreciated.

 

PD: I don't know how to make the table looks better in the post; the style or the width of each column

1 ACCEPTED SOLUTION
AlienSx
Memorable Member
Memorable Member

Hello, @MirageZX 

let
    t1 = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    t2 = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    lookup_t = Record.FromList(t2[On_Hand_Qty], List.Transform(t2[Material], Text.From)),
    f = (t as table) => 
        [opening_balance = Record.Field(lookup_t, Text.From(t[Material]{0})),
        pos = List.Buffer(List.Skip(List.Positions(t[Material]))),
        res = List.Buffer(t[Req_Qty]),
        balances = 
            List.Accumulate(
                pos,
                {opening_balance - res{0}},
                (s, c) => s & { s{c - 1} - res{c} }
            ),
        out = Table.FromColumns(Table.ToColumns(t) & {balances}, Table.ColumnNames(t) & {"Running_Stock"})][out],
    gr = Table.Group(t1, {"Material"}, {{"all", each f(Table.Sort(_, "Res_Date"))}}),
    z = Table.Combine(gr[all])
in
    z

View solution in original post

4 REPLIES 4
AlienSx
Memorable Member
Memorable Member

Hello, @MirageZX 

let
    t1 = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    t2 = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    lookup_t = Record.FromList(t2[On_Hand_Qty], List.Transform(t2[Material], Text.From)),
    f = (t as table) => 
        [opening_balance = Record.Field(lookup_t, Text.From(t[Material]{0})),
        pos = List.Buffer(List.Skip(List.Positions(t[Material]))),
        res = List.Buffer(t[Req_Qty]),
        balances = 
            List.Accumulate(
                pos,
                {opening_balance - res{0}},
                (s, c) => s & { s{c - 1} - res{c} }
            ),
        out = Table.FromColumns(Table.ToColumns(t) & {balances}, Table.ColumnNames(t) & {"Running_Stock"})][out],
    gr = Table.Group(t1, {"Material"}, {{"all", each f(Table.Sort(_, "Res_Date"))}}),
    z = Table.Combine(gr[all])
in
    z

Thanks mate! It works!!

ppm1
Solution Sage
Solution Sage

This is doable, but why not just do it with a DAX measure (loading both tables into your model).

 

Pat

Microsoft Employee
MirageZX
Frequent Visitor

Could you please provide the DAX code needed (Power Pivot) or the Power Query alternative? 

Helpful resources

Announcements
PBI Sept Update Carousel

Power BI September 2023 Update

Take a look at the September 2023 Power BI update to learn more.

Learn Live

Learn Live: Event Series

Join Microsoft Reactor and learn from developers.

Dashboard in a day with date

Exclusive opportunity for Women!

Join us for a free, hands-on Microsoft workshop led by women trainers for women where you will learn how to build a Dashboard in a Day!

MPPC 2023 PBI Carousel

Power Platform Conference-Power BI and Fabric Sessions

Join us Oct 1 - 6 in Las Vegas for the Microsoft Power Platform Conference.

Top Solution Authors
Top Kudoed Authors