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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
dumpguy
Frequent Visitor

How to get derived string recognized as column name. List.Last(Table.ColumnNames(...

Dear all,

I´m new to this forum, but a lot of posts could already help me with my Power Query ambition. Thanks´s a lot for that.
Now, for my last problem I couldn´t find a solution (or rather don´t know the right expression to search for).

 

My goal is to keep the first row of nested tables, which I could solve by this M code:

 

= Table.AddColumn(#"Expand all", "First row only", each Table.FirstN([Grouped 5], 1))

 

This got me a column "First row only" holding nested tables each with exact one row, the top row fetched from the nested tables in column "Grouped 5".

 

So far so good, but now I created my source with a dynamic amount of columns, meaning the last column  - the one to be processed - can be named "Grouped 1", "Grouped 2" up to "Grouped 7".

 

By testing this M code:

= Table.AddColumn(#"Expand all - dynamically", "Custom", each List.Last(Table.ColumnNames(#"Expand all - dynamically")))

I could verify to strip the name of the last column, e.g. "Grouped 5".

 

But putting this into the code for receiving the first rows of each nested table in this last column didn´t work. Obviously there´s a mistake in it:

= Table.AddColumn(#"Expand all - dynamically", "First row only", each Table.FirstN(List.Last(Table.ColumnNames(#"Expand all - dynamically")), 1))

 

Expression.Error: We cannot convert the value ""Grouped 5"" to type Table.

 

I think the doubled "" are an indication what´s going wrong, but my experience on M code isn´t sufficient to figure this out.
I hope somebody can help me.

 

Regards,

Stefan

1 ACCEPTED SOLUTION
Vera_33
Resident Rockstar
Resident Rockstar

Hi @dumpguy 

 

If I understand correctly, you want to find your last column which is a table, then pass the column name as a variable to get the first row, try it

Table.AddColumn(#"Expand all - dynamically", "First row only", each Table.FirstN(Record.Field(_,List.Last(Table.ColumnNames(#"Expand all - dynamically"))),1))

 

View solution in original post

2 REPLIES 2
Vera_33
Resident Rockstar
Resident Rockstar

Hi @dumpguy 

 

If I understand correctly, you want to find your last column which is a table, then pass the column name as a variable to get the first row, try it

Table.AddColumn(#"Expand all - dynamically", "First row only", each Table.FirstN(Record.Field(_,List.Last(Table.ColumnNames(#"Expand all - dynamically"))),1))

 

Hello Vera_33,

this was the solution! Many thanks for your support.

Have a nice day,

Stefan

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

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

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors
Top Kudoed Authors