Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Nie
Helper I
Helper 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 loop to do this and I have googled how to emulate loops in M. However, the articles I found were on how to calculate and accumulate some values instead of calling some M functions.

 

Is there a way to achieve an equivalent of a loop in M to perform a Table.AddColumn() based on the number of items in a list?

1 ACCEPTED SOLUTION
Vera_33
Resident Rockstar
Resident Rockstar

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

 

 

View solution in original post

4 REPLIES 4
Nie
Helper I
Helper I

Thanks @Vera_33. I don't know how it works but it does.

 

I understand with the double [][] above that you are creating a record and then referencing a specific field value. In this case, it is [result]. And the strategy to add columns based on a dynamic list is to use a temporary record.

 

I tried simplifying your code to understand how it works by manually adding a step and then using the following:

= [
   counter = List.Count({"AAA"}),
   columnName = {"AAA"}{0},
   tempTable = Table.AddColumn(myTable, columnName, each columnName),
   result = tempTable
  ][result]

But when doing so I see the following when I look at the entire record.

Screenshot 2021-07-08 102715.png

When I look at [result] specifically, I get the error "Expression.Error: We cannot convert a value of type Function to type Table".

How and why does your code work?

 

Vera_33
Resident Rockstar
Resident Rockstar

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

Thank you @Vera_33 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 [][].

Vera_33
Resident Rockstar
Resident Rockstar

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

 

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.