Power Query Loop Calculation

PQDM 21 Reputation points
2021-02-06T18:52:39.807+00:00

Hi, I am new to Power Query and have an issue with a task which is relatively easy to complete in excel.

I need to get a loop calculation where the next output is dependant on the previous output in two places. Please see below for a brief snapshot.

The Starting point is 100 and no calculation is required. Column B is fixed throughout. Column C values vary between 0 - 15.

The operations is (Previous Answer * Column B) + Previous Answer + Column C

64824-calc.png

I came across the following but I am unable to manipulate the code to carry out the multiplication in the first step.

let _b = #"Added Index"{0} in
List.Accumulate(
List.Combine(
{{_b},
List.Range(#"Added Index"[A], 0, [Index] + 1)}),
0, (s, c) => s + c)

Any help would be greatly appreciated!

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
36,246 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lz._ 8,991 Reputation points
    2021-02-07T15:27:55.987+00:00

    @PQDM . Another option:

    let  
        Source = Table.FromRows(  
            {  
                {"Dec", null, null}, {"Jan", -0.0023, 0},  
                {"Feb", -0.0023, 0}, {"Mar", -0.0023, 7},  
                {"Apr", -0.0023, 0}  
            },  
            type table [A=text, B=number, C=number]  
        ),  
        Index = Table.AddIndexColumn(Source, "Index", 0, 1),  
        Output = Table.AddColumn(Index, "Output", each  
            if [Index] = 0 then 100  
            else  
                Number.Round(  
                    List.Accumulate(List.Skip(List.Range(Index[C], 0, [Index]+1)), 100,  
                        (state,current)=> (state * -0.0023) + state + current  
                    ),  
                    2  
                ),  
            type number  
        ),  
        RemovedIndex = Table.RemoveColumns(Output,{"Index"})  
    in  
        RemovedIndex  
    
    1 person found this answer helpful.
    0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Lz._ 8,991 Reputation points
    2021-02-07T04:01:02.523+00:00

    Hi @PQDM

    let  
    // Table for demo  
        Source = Table.FromRows(  
            {  
                {"Dec", null, null}, {"Jan", -0.0023, 0},  
                {"Feb", -0.0023, 0}, {"Mar", -0.0023, 7},  
                {"Apr", -0.0023, 0}  
            },  
            type table [A=text, B=number, C=number]  
        ),  
    //  
        OutList = List.Generate(  
            ()=> [i = 0, out = 100],  
            each [i] < Table.RowCount(Source),  
            each [i = [i] +1, out = Number.Round(([out]*-0.0023) + [out] + Source[C]{i},2)],  
            each [out]  
        ),  
        ToColumns = Table.ToColumns(Source) & {OutList},  
        ToTable = Table.FromColumns(ToColumns,  
            Table.ColumnNames(Source) & {"Output"}  
        ),  
        ChangedType = Table.TransformColumnTypes(ToTable,  
            {{"Output", type number}}  
        )  
    in  
        ChangedType  
    
    0 comments No comments

  2. t123 1 Reputation point
    2023-01-07T00:31:24.49+00:00

    Hello!

    Thank you for the solution above. I am trying to extend it to include calculations for group by ID. Will you be able to help please

    277073-image.png

    0 comments No comments