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

Get inspired! Check out the entries from the Power BI DataViz World Championships preliminary rounds and give kudos to your favorites. View the vizzies.

Reply
devans91
Frequent Visitor

Recursive Carried/Brought Forward

Hello,

I'm working on a dataset which needs to find the difference between a Resource Required and Resource Allocated for a particular day and carry it forward to the next relevant row. For example if on 1/1/25 Resource Required = 3, Resource Allocated = 2.5 then the Carried Forward = 0.5. I also somehow need to make sure that this value is at least 0, so any negative value is replaced with 0.

 

I want this 0.5 figure to be carried over to the next row and then added to the Resource Required, eg. 2/1/25 Resource Required = 2, Resource Allocated = 2.5, Brought Forward = 0.5 then I'd like to create a Rolling Resource Required which should equal the Resource Required + Brought Forward = 2.5.

 

I tried doing a self-join by creating 2 Index columns (one shifted) but that only worked out the difference between 2 days and didn't carry forward over multiple days. I think it requires some sort of iteration but I just can't work it out!

 

The table below is what I'd like as a result. Essentially each check needs to match the Line of Business and Document Type and bring down the Carried Forward figures into the Brought Forward columns. 

 

Appreciate any help and hopefully this isn't as complicated as I think it is...!

 

DateLine of BusinessDoc TypeInput RequiredQC RequiredInput Brought ForwardQC Brought ForwardRolling Input RequiredRolling QC RequiredInput AllocatedQC AllocatedInput Carried ForwardQC Carried Forward
1/1/2025PropertyQuote105001054362
2/1/2025PropertyQuote866214843105
3/1/2025PropertyQuote7310517843135
4/1/2025PropertyQuote12413525943216
5/1/2025PropertyQuote972163013432610
6/1/2025PropertyQuote11626103716433313
7/1/2025PropertyQuote10533134318433915
1/1/2025AutoPolicy1270012710820
2/1/2025AutoPolicy94201148530
3/1/2025AutoPolicy151030181012961
1 ACCEPTED SOLUTION
dufoq3
Super User
Super User

Hi @devans91, do you need all calculated columns or is it enough to calc just Carried Forward cols?

 

Output:

dufoq3_0-1739446188352.png

 

You have to specify columns (as a list) for which you want to calculate Carried Forward col:

dufoq3_1-1739446288989.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ31DcyMDJV0lEKKMovSC0qqQQyA0vzS1KBtKEBkADJmQCxsVKsTrSSEV4NFkBshqLeGK96c7A6ZPUm+B1kBFWM0GCKV4Ml1BKEejP8Fhhi+MCc1CBCClPH0pJ8kL78nMzkSpj7zWHaLNBDFF25JdRcC7AdaMGJYbYpzFywLZZKsbEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, #"Line of Business" = _t, #"Doc Type" = _t, #"Input Required" = _t, #"QC Required" = _t, #"Input Allocated" = _t, #"QC Allocated" = _t]),
    ChangedType = Table.TransformColumnTypes(Source,{{"Input Required", Int64.Type}, {"QC Required", Int64.Type}, {"Input Allocated", Int64.Type}, {"QC Allocated", Int64.Type}}),
    ColsToCalculate = {"Input", "QC"},
    F = (tbl as table, cols as list)=>
                List.Transform(cols, (r)=> 
                    [ a1 = List.Buffer(Table.ToRows(Table.SelectColumns(tbl, List.Transform({"Required", "Allocated"}, (x)=> Text.Combine({r, x}, " "))))),
                      a2 = List.Generate(
                                ()=> [ x = 0, y = a1{x}, z = List.Max({y{0} - y{1}, 0}) ],
                                each [x] < List.Count(a1),
                                each [ x = [x]+1, y = a1{x}, z = List.Max({y{0} - y{1} + [z], 0}) ] ,
                                each [z] )
                    ][a2] ),
    StepBack = ChangedType,
    Ad_CarriedForwardCols = Table.Combine(Table.Group(StepBack, {"Line of Business"}, {{"All", each _}, {"T", each
        [ cols = List.Transform(ColsToCalculate, (x)=> x & " Carried Forward"),
          a = Table.FromColumns(Table.ToColumns(_) & F(_, ColsToCalculate), Table.ColumnNames(_) & cols),
          b = Table.TransformColumnTypes(a, List.Transform(cols, (x)=> {x, type number}))
        ][b], type table}})[T])
in
    Ad_CarriedForwardCols

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

View solution in original post

5 REPLIES 5
dufoq3
Super User
Super User

Hi @devans91, do you need all calculated columns or is it enough to calc just Carried Forward cols?

 

Output:

dufoq3_0-1739446188352.png

 

You have to specify columns (as a list) for which you want to calculate Carried Forward col:

dufoq3_1-1739446288989.png

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMtQ31DcyMDJV0lEKKMovSC0qqQQyA0vzS1KBtKEBkADJmQCxsVKsTrSSEV4NFkBshqLeGK96c7A6ZPUm+B1kBFWM0GCKV4Ml1BKEejP8Fhhi+MCc1CBCClPH0pJ8kL78nMzkSpj7zWHaLNBDFF25JdRcC7AdaMGJYbYpzFywLZZKsbEA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t, #"Line of Business" = _t, #"Doc Type" = _t, #"Input Required" = _t, #"QC Required" = _t, #"Input Allocated" = _t, #"QC Allocated" = _t]),
    ChangedType = Table.TransformColumnTypes(Source,{{"Input Required", Int64.Type}, {"QC Required", Int64.Type}, {"Input Allocated", Int64.Type}, {"QC Allocated", Int64.Type}}),
    ColsToCalculate = {"Input", "QC"},
    F = (tbl as table, cols as list)=>
                List.Transform(cols, (r)=> 
                    [ a1 = List.Buffer(Table.ToRows(Table.SelectColumns(tbl, List.Transform({"Required", "Allocated"}, (x)=> Text.Combine({r, x}, " "))))),
                      a2 = List.Generate(
                                ()=> [ x = 0, y = a1{x}, z = List.Max({y{0} - y{1}, 0}) ],
                                each [x] < List.Count(a1),
                                each [ x = [x]+1, y = a1{x}, z = List.Max({y{0} - y{1} + [z], 0}) ] ,
                                each [z] )
                    ][a2] ),
    StepBack = ChangedType,
    Ad_CarriedForwardCols = Table.Combine(Table.Group(StepBack, {"Line of Business"}, {{"All", each _}, {"T", each
        [ cols = List.Transform(ColsToCalculate, (x)=> x & " Carried Forward"),
          a = Table.FromColumns(Table.ToColumns(_) & F(_, ColsToCalculate), Table.ColumnNames(_) & cols),
          b = Table.TransformColumnTypes(a, List.Transform(cols, (x)=> {x, type number}))
        ][b], type table}})[T])
in
    Ad_CarriedForwardCols

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Thank you, I was able to derive the rest of the required columns from the Carried Forward calculations. Honestly the solution felt like magic and can't thank you enough!

You're welcome. Enjoy 😉


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

devans91
Frequent Visitor

Hi BBF, thanks for looking into this!

The input data is the Date, Line of Business, Doc Type, Input Required, QC Required, Input Allocated and QC Allocated. The rest of the columns I'd like to calculate.

BeaBF
Super User
Super User

@devans91 Hi! which are the input data?

 

BBF

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

FebPBI_Carousel

Power BI Monthly Update - February 2025

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

Feb2025 NL Carousel

Fabric Community Update - February 2025

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