Forum Discussion
Nie
5 years agoHelper I
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...
- Anonymous5 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
Anonymous
5 years agoNot applicable
Hi Nie
The [][] replaces the let...in...structure, it is a custom function called fxAddColumn
fxAddColumn = (T as table, N as number) =>
let
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)
in
result
then you can call this function to do recursion, I am not sure how you add the code to your current table, as this is actually a table function: Table.AddColumn
Nie
5 years agoHelper I
Thank you Anonymous for explaining. Turns out I had a misconception. I previously thought your code involves creating records. I didn't know let..in can be substituted using [][].