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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

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
Super User
Super User

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
Super User
Super User

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
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors