Forum Discussion

rpiboy_1's avatar
rpiboy_1
Helper V
3 years ago
Solved

struggling with List.Accumulate and a Table Function

Hi all,   I'm attempting to use List.Accumulate with a Table Function to end up with a single combined table from the results.   So data looks like this:   Project No Data Type RawTable ...
  • rpiboy_1's avatar
    3 years ago

    In case anyone happens upon this thread, or cares. Ultimatly I discovered that my understanding of List.Accumulate was generally correct, my error was in the Function being called, where it was effectively attempting to drill down an 'extra' time, where as the code above effectively was providing the correct level of drill down. I didn't happen upon this solution until thoroughly convincing myself (thanks Youtube) that I correctly understood 'each' and '_'. After that, and with some further testing, experimentation I realized what was going on.

    As they say, if you're troubleshooting yourself, you're just workin'. 🙂

    Also for the record the final code looks something like this, including proper handling for the 'seed' context of the Accumulate function:

    let
        FilterValue = "value",
        #"Filtered Rows" = Table.SelectRows(ProjectData, each ([Data Type Parameter] = FilterValue)),
    //invoke the custom function to transform the tables as a NewColumn (maybe I could just transform the column in place?). Might need some error handling for the possibility of null tables or such, not sure.
        #"Invoked Custom Function" = 
               Table.AddColumn(
                    #"Filtered Rows",
                    "FnIterator",
                    each FnIterator([DetailedData], FilterValue
               )
        ),
    //isolate the table from the first row to use a seed
        #"IsolateFirstTable"= #"Invoked Custom Function"{0}[FnIterator],
    //now combine all of the tables, starting with the seed table
        #"ListAccumulate" =
               List.Accumulate(
                  //need to remove the first item from our list, since we're using it as our seed value
                  List.RemoveFirstN(
                      #"Invoked Custom Function"[FnIterator],
                      1
                  ),
                  #"IsolateFirstTable",
                  (current, state)=>
                     //this becomes super simple, because we're only combining the list of things we already transformed
                     Table.Combine({current, state})
               ),
        #"Removed Columns" = Table.RemoveColumns(ListAccumulate,{"id"})
        
    in
        #"Removed Columns"