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
Meindert
New Member

Looping through columns in table

Hi everyone,

 

I have the following problem.

My datasource, an Excel file, exists out of 101columns. As from column 20 I want to merge 2 columns at the time. 

So the data in column 20 and column 21 needs to be merged in a new column 20, with || as seperator.

Column 22 and column 23 needs to be merged in a new column 22, again with || as the seperator....till column 100 and column 101...

I started it by manually merging 2 columns, but my Query becomes so long and I was wondering if there isn't a faster, more elegant way to do this by creating a loop in M.

The amount of columns is fixed and the starting column also.

 

Thanks for anyone who can guide me to a possible solution.

 

 

1 ACCEPTED SOLUTION
Meindert
New Member

Hi,

 

I've been testing around, after a friend told me a way to incoorporate loops in M and this is what I found myself.

 

MergedColumns = List.Accumulate(

        {7..40}, #"Changed Type",(state,current) 

        => Table.CombineColumns(state,

                                {"Column" & Number.ToText(current*2-1), "Column" & Number.ToText(current*2) },

                                Combiner.CombineTextByDelimiter("||",QuoteStyle.None),

                                "Column"& Number.ToText(current*2-1))

        )

 

And this works. It merges all columns between number 13 and 80 and gives them correct names.

 

Cheers

Meindert

 

 

 

View solution in original post

1 REPLY 1
Meindert
New Member

Hi,

 

I've been testing around, after a friend told me a way to incoorporate loops in M and this is what I found myself.

 

MergedColumns = List.Accumulate(

        {7..40}, #"Changed Type",(state,current) 

        => Table.CombineColumns(state,

                                {"Column" & Number.ToText(current*2-1), "Column" & Number.ToText(current*2) },

                                Combiner.CombineTextByDelimiter("||",QuoteStyle.None),

                                "Column"& Number.ToText(current*2-1))

        )

 

And this works. It merges all columns between number 13 and 80 and gives them correct names.

 

Cheers

Meindert

 

 

 

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.