Forum Discussion

julhelp's avatar
julhelp
Helper I
4 years ago
Solved

Automatically generate column caption in power query

Hi,

 

i have a question aubout power query. Is there any posibillity to generate automatically the column caption from an table which is retrieved via api from a database? 

 

So far i have to hardcode it into my last step: 

The problem is, that i want to switch beetwenn other tables and therefore i have to change this step of code by my self. 

this is my code jet:

 

Have anyone an idea?

 

Thanks in advance

 

  • Since it's a record column, I think you'd use Record.FieldNames instead.

     

    Maybe like this:

    [...]
        #"Custom items" = Table.ExpandListColumn(#"Removed Columns", "data"),
        ColumnList = Record.FieldNames(#"Custom items"{0}[data]),
        #"Expanded data" = Table.ExpandRecordColumn(#"Custom items", "data",
            ColumnList, List.Transform(ColumnList, each "data." & _))
    in
        #"Expanded data"

6 Replies

  • mahoneypat's avatar
    mahoneypat
    Microsoft Employee

    Not totally clear on your scenario, but you can use Table.ColumnNames() on your [data] column to dynamically generated a list of the column names and use that in place of the hard-coded list.

     

    Pat

    • AlexisOlson's avatar
      AlexisOlson
      Super User

      Since it's a record column, I think you'd use Record.FieldNames instead.

       

      Maybe like this:

      [...]
          #"Custom items" = Table.ExpandListColumn(#"Removed Columns", "data"),
          ColumnList = Record.FieldNames(#"Custom items"{0}[data]),
          #"Expanded data" = Table.ExpandRecordColumn(#"Custom items", "data",
              ColumnList, List.Transform(ColumnList, each "data." & _))
      in
          #"Expanded data"
      • julhelp's avatar
        julhelp
        Helper I

        Yes that was exactly what i was looking for 😉