Customized running total in Excel power query

sara1312

New Member
Joined
Sep 9, 2024
Messages
10
Office Version
  1. 365
Platform
  1. Windows
Please find the attached table

GroupSKUSHours of production runTarget hoursIndexRunning TotalExample Colum
Group 11233612323
Group 124362274
Group 133363303
Group 1410364406
Group 152336563Not Eligible
Group 161336676Not Eligible
Group 171636792Not Eligible
Group 1831368123Not Eligible
Group 1913369136Not Eligible
Group 110153610151Not Eligible
Group 111233611174Not Eligible
Group 112723612246Not Eligible
Group 113113613257Not Eligible
Group 11453614262Not Eligible
Group 115623615324Not Eligible
Group 116203616344Not Eligible
Group 117643617408Not Eligible
Group 2182236182222
Group 2191036193210
Group 2208336201154
Group 221243621139Not Eligible
Group 222653622204Not Eligible
Group 223103623214Not Eligible

Group 1 hours of production run to be running totaled but when its above 36 hours return the hours to add up to 36 , the remaining skus are not eligible for the calculation

Kindly support on how we can achieve this
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="YourTableName"]}[Content],
    Group = Table.Group(Source, {"Group"}, {{"All", each 
        let
            tbl = Table.AddIndexColumn(_, "idx"),
            tbl1 = Table.AddColumn(tbl, "Example", each 
                    if [Running Total] <= 36 then [Hours of production run] else
                    if [idx] = 0 then 36 else 
                    if tbl[Running Total]{[idx] -1} < 36 then 36 - tbl[Running Total]{[idx] -1} else 
                    "Not Eligble"
            )
        in  
            tbl1
    }}),
    Result = Table.RemoveColumns(Table.Combine(Group[All]), {"idx"})
in
    Result
 
Upvote 1
Solution
Not sure to fully understand your request.
Assuming that columns "SKUS" et "Index" as useless and "Target hours" may difference between group.

Power Query:
let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wci/KLy1QMFTSUTIyBhLGZkqxOsiiJtgEsao0NMAmit1UQ+yiZlgtMyTBBFPi3WBuhNUErLZhNdYMqwFGWIPBDCMcQZqNQISJKZooOCAxRC2MsYkamWATNTPFa24sAA==", BinaryEncoding.Base64), Compression.Deflate)),
             let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Group = _t, #"Hours of production run" = _t, #"Target hours" = _t]),
    cols = {Table.ColumnNames (Source)} & {{"Running Total", "Example Colum"}},
    #"Changed Type" = Table.TransformColumnTypes(Source,{{cols{0}{1}, Int64.Type}, {cols{0}{2}, Int64.Type}}),
    fx = (x, y)=> let x = List.Buffer(Table.ToColumns(x){1}),
                      y = List.Generate(
                          ()=> [ idx = 0,  a = x{0}, b = if x{0} < y then x{0} else "Not Eligible" ] ,
                          each [idx] <= List.Count(x)-1,
                          each [ idx = [idx] +1 , a = List.Sum({[a], x{[idx] + 1} }), b = if a < y then a else "Not Eligible"],
                          each [[a], [b]])
                  in List.Transform(y, each Record.ToList(_)),
    #"Grouped Rows" = Table.Group(#"Changed Type", cols{0}{0},
                      {{"x", each let target = List.Distinct(Table.Column(_, cols{0}{2})){0}
                      in Table.FromRows(
                      List.Transform(List.Zip({Table.ToRows(_) , fx(_,target)}),
                      List.Combine),  cols{0} & cols{1})
                      }})[x],
    Result = Table.Combine(#"Grouped Rows")
in
    Result

Regards
 
Upvote 1
I just understood the initial request with JGordon11's answer - Code updated.

Power Query:
let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45Wci/KLy1QMFTSUTIyBhLGZkqxOsiiJtgEsao0NMAmit1UQ+yiZlgtMyTBBFPi3WBuhNUErLZhNdYMqwFGWIPBDCMcQZqNMEwwwhqQIFELDE+ATcBqrhmGe1HMjQUA", 
             BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Group = _t, #"Hours of production run" = _t, #"Target hours" = _t]),
    cols = {Table.ColumnNames (Source)} & {{"Running Total", "Example Colum"}},
    #"Changed Type" = Table.TransformColumnTypes(Source,{{cols{0}{1}, Int64.Type}, {cols{0}{2}, Int64.Type}}),
    fx = (x, y)=> let x = List.Buffer(Table.ToColumns(x){1}), 
                      y = List.Generate(
                          ()=> [ idx = 0,  a =  x{0}, b = if a < y then x{idx} else "Not Eligible" ] ,
                          each [idx] <= List.Count(x)-1,
                          each [ idx = [idx] +1 , a = List.Sum({[a], x{[idx]+1} }), b = if a < y then x{idx} else if y - [a] < 0 then "Not Eligible" else  y - [a] ],
                          each [[a], [b]])
                  in List.Transform(y, each Record.ToList(_)),
    #"Grouped Rows" = Table.Group(#"Changed Type", cols{0}{0}, 
                      {{"x", each let target = List.Distinct(Table.Column(_, cols{0}{2})){0} 
                      in Table.FromRows(
                      List.Transform(List.Zip({Table.ToRows(_) , fx(_,target)}), 
                      List.Combine),  cols{0} & cols{1})
                      }})[x],
    Result = Table.Combine(#"Grouped Rows")
in
    Result
 
Upvote 0
@JGordon11 Thanks a lot for the support . Works perfectly .
But I am getting an expression error as the field name already exists. I tried to change the names but everytime I do , the column with new name and old one being retained .
 
Upvote 0
Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="YourTableName"]}[Content],
    Group = Table.Group(Source, {"Group"}, {{"All", each
        let
            tbl = Table.AddIndexColumn(_, "idx"),
            tbl1 = Table.AddColumn(tbl, "Example", each
                    if [Running Total] <= 36 then [Hours of production run] else
                    if [idx] = 0 then 36 else
                    if tbl[Running Total]{[idx] -1} < 36 then 36 - tbl[Running Total]{[idx] -1} else
                    "Not Eligble"
            )
        in 
            tbl1
    }}),
    Result = Table.RemoveColumns(Table.Combine(Group[All]), {"idx"})
in
    Result
 
Upvote 0
Hi, Instead of source to the excel work book , I would like to connect source to the previous step . Column naming is causing the issues
 
Upvote 0
Thank you so much , the refresh is not working to this column , before this step its working .Kindly help
 
Upvote 0

Forum statistics

Threads
1,223,880
Messages
6,175,154
Members
452,615
Latest member
bogeys2birdies

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top