Forum Discussion

Nie's avatar
Nie
Helper I
5 years ago
Solved

Loop Through List and Add Columns

I have a list that changes dynamically. It can have 10 items now, but 100 items later. I want to be able to add a corresponding column to my table for each item in this list.   I think I need a loo...
  • Anonymous's avatar
    Anonymous
    5 years ago

    Hi Nie 

     

    So you have a table, and a list. You need to add a column for each item in the list, and the column name / values are all that item from the list? If yes, one way to do it

     

     

    let
        yourStep = some steps...,
        fxAddColumn = (T as table, N as number) =>
             [
                 counter = List.Count(yourList),
                 columnName = yourList{N},
                 tempTable = Table.AddColumn(T, columnName, each columnName),
                 result = if N>= counter-1 then tempTable else @fxAddColumn(tempTable,N+1) 
             ][result], 
    
        Custom = fxAddColumn(yourStep,0)
    in
        Custom