Power Query - Cumulated row, if a certain value is matched

M, Johannes 86 Reputation points
2020-11-30T14:21:05.707+00:00

Hey guys,

I got this table in the Power Query Editor. The first column is the date in ascending order. The second one is a part code. The third und fourth columns are the quantity of produced and shipped parts. The last column is the difference between produced und shipped (eg. [Produced]-[Shipped]).
43781-1.png

What i need is a new column "Cumulated" like in the example, where the differences are being cumulated between the present "Difference" and the "Cumulated" where the code is the same. So every time in the table when the code is the same, the column "Cumulated" sums the last cumulated value of the code und the present "Difference" value.
43736-2.png

43600-3.png

This is really tricky in power query, so you would save my day, if you can help me.

Cheers,

Johannes

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
35,969 questions
Excel Management
Excel Management
Excel: A family of Microsoft spreadsheet software with tools for analyzing, charting, and communicating data.Management: The act or process of organizing, handling, directing or controlling something.
1,640 questions
0 comments No comments
{count} votes

Accepted answer
  1. Lz._ 8,991 Reputation points
    2020-12-02T08:41:31.927+00:00

    @M, Johannes

    let  
        Source = ...,  
        ChangedType = ...,  
        GroupedRows = Table.Group(ChangedType, {"Code"},  
            {"CODES", each  
                let  
                    idx = Table.AddIndexColumn(_, "Idx", 0,1),  
                    cumul = Table.AddColumn(idx, "Cumulated",  
                    (state)=>  
                        List.Sum(  
                            Table.SelectRows(idx, each state[Idx] >= [Idx])[Difference]  
                        ), Int64.Type  
                    ),  
                    diffascumul = Table.AddColumn(_, "Cumulated", each [Difference], Int64.Type)  
                in  
                    if Table.RowCount(_) = 1 then diffascumul else cumul,  
                type table  
            }  
        ),  
        CombinedCodes = Table.Combine(GroupedRows[CODES]),  
        RemovedIdx = Table.RemoveColumns(CombinedCodes,"Idx")  
    in  
        RemovedIdx  
    

    44367-demo.png

    1 person found this answer helpful.

4 additional answers

Sort by: Most helpful
  1. Lz._ 8,991 Reputation points
    2020-12-01T03:54:12.607+00:00

    Hi @M, Johannes

    let  
        Source = ...,  
        ChangedTypes = ...,  
      
        // Func. by Imke Feldmann - www.TheBIccountant.com  
        fxShiftByOneRow = (MyTable as table, MyColumnName as text) as table =>  
        let  
            Source = MyTable,  
            ShiftedList = {null} &  List.RemoveLastN(Table.Column(Source, MyColumnName),1),  
            Custom1 = Table.ToColumns(Source) & {ShiftedList}  
        in  
            Table.FromColumns(Custom1, Table.ColumnNames(Source) & {"PreviousDiff"}),  
      
        GroupedRows = Table.Group(ChangedTypes, {"Code"},  
            {"CODE", each  
                let  
                    shiftedTable = fxShiftByOneRow(_,"Difference"),  
                    shiftedCumul = Table.AddColumn(shiftedTable, "Cumulated", each  
                        if [PreviousDiff] is null then [Difference]  
                        else [PreviousDiff] + [Difference],  
                        Int64.Type  
                    ),  
                    removedPreviousDiff = Table.RemoveColumns(shiftedCumul, "PreviousDiff"),  
                    diffAsCumul = Table.AddColumn(_, "Cumulated", each [Difference], Int64.Type)  
                in  
                    if Table.RowCount(_) > 1 then removedPreviousDiff else diffAsCumul,  
                type table  
            }  
        ),  
        CombinedNestedTables = Table.Combine(GroupedRows[CODE])  
    in  
        CombinedNestedTables  
    

    EDIT: For this case you can replace above lines
    1 with:

            ShiftedList = {0} &  List.RemoveLastN(Table.Column(Source, MyColumnName),1),  
    

    and 18-20 with:

                    shiftedCumul = Table.AddColumn(shiftedTable, "Cumulated", each  
                        [PreviousDiff] + [Difference],  
    
    
      
    
    1 person found this answer helpful.

  2. Emi Zhang-MSFT 21,706 Reputation points Microsoft Vendor
    2020-12-01T09:05:56.577+00:00

    Hi,
    Did you want to add the column in Power Query?
    I can get the result with Excel formula after transforming data in power query editor to Excel Worksheet:

    =E2+XLOOKUP(B2,$B$1:B1,$E$1:E1,0,0,-1)

    43929-image.png


    If the response is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

  3. Herbert Seidenberg 1,191 Reputation points
    2020-12-01T05:43:27.61+00:00

    Excel 365 Pro Plus with Power Pivot and Power Query.
    Cumulative by code.
    Added PivotTable and PivotChart.
    http://www.mediafire.com/file/aaafdtexgv8twor/11_30_20.xlsx/file
    http://www.mediafire.com/file/f2submfqmwgx0ry/11_30_20.pdf/file

    0 comments No comments

  4. Lz._ 8,991 Reputation points
    2020-12-03T09:15:21.027+00:00

    Hi @M, Johannes

    Below is another option I tested against a table with 3 different Codes each having 1000 records and perf. are better than the previous option I shared. Each scenario being quite unique you'll have to test it in real conditions. If you do I'll be interested to know how it went on your side

        GroupedRows = Table.Group(ChangedType, {"Code"},  
            {"CODES", each  
                let  
                    idx = Table.AddIndexColumn(_, "Idx", 1,1),  
                    cumul = Table.AddColumn(idx, "Cumulated", each  
                        List.Sum(  
                            List.Range(idx[Difference],0,[Idx])  
                        ), Int64.Type  
                    ),  
                    diffascumul = Table.AddColumn(_, "Cumulated", each [Difference], Int64.Type)  
                in  
                    if Table.RowCount(_) = 1 then diffascumul else cumul,  
                type table  
            }  
        ),  
    

    And if perf. are also better on your side I will appreciate you mark this option as answer as well => can help others... Thanks

    0 comments No comments