Forum Discussion

Richard_Halsall's avatar
1 year ago
Solved

Name a Table from a Column Value

Hi

 

Is there a way to name a table based on a column value in that table

 

I have a grouped table of data as below named AssetActivity

In a new query I have this code

 

let
Source = if Table.RowCount(AssetActivity) >= 1 then AssetActivity{0}[AllData] else null,

 

which creates a table named Asset1 with all data from the inner table named 'AllData'

 

I manually create a new query for each row and end up with 26 tables named Asset1.....Asset26

 

Is there anyway I can name the table 'T-33_441' rather than it being names Asset1 i.e. the value in the column named Asset present in the AssetActivity table

 

If it helps the Asset column is also present in the inner table

 

Any help would be appreciated. Thanks

  • I can not reproduce your behaviour. The code snippet you gave is not complete, so I guess there is more happening.

    BUT,

    You will always have to manuall create a new query for each row. 
    Given that...

    Starting with:

    Rightclick on the Table in any line and "Add as New Query

    Will produce this new query:



    Did I answer your question? Then please (also) mark my post as a solution and make it easier to find for others having a similar problem.
    Remember: You can mark multiple answers as a solution...
    If I helped you, please click on the Thumbs Up to give Kudos.

    Kees Stolker

    A big fan of Power Query and Excel

     

     

4 Replies

  • Hi Richard_Halsall Could you try this please 

    • Group Data by Asset:

      • Go to Home → Group By.
      • In the Group By window:
        • Group by the Asset column.
        • Operation: All Rows.
    • Create a New Column for Table Names:

      • Add a custom column to extract the Asset value as the table name:
        • Go to Add Column → Custom Column.
        • Use this formula:
          Text.From([Asset])
    • Iterate and Save Tables: For each row (grouped by Asset), you can extract the nested table and save it with the dynamic name. However, Power Query does not directly support exporting multiple tables automatically. You’ll need to:
      • Go to each grouped table.
      • Rename the query manually based on the new column.
        Heres the M code 

     

    let
        Source = YourDataTable,
        GroupedData = Table.Group(Source, {"Asset"}, {{"AllData", each _, type table [Column1=type, Column2=type]}}),
        NamedTables = Table.AddColumn(GroupedData, "TableName", each "Table_" & Text.From([Asset])),
        Result = Table.TransformColumns(NamedTables, {"AllData", each Table.RenameColumns(_, {"Asset", "RenamedTable"})})
    in
        Result
    ​

     

    If this post helped please do give a kudos and accept this as a solution
    Thanks In Advance

  • I can not reproduce your behaviour. The code snippet you gave is not complete, so I guess there is more happening.

    BUT,

    You will always have to manuall create a new query for each row. 
    Given that...

    Starting with:

    Rightclick on the Table in any line and "Add as New Query

    Will produce this new query:



    Did I answer your question? Then please (also) mark my post as a solution and make it easier to find for others having a similar problem.
    Remember: You can mark multiple answers as a solution...
    If I helped you, please click on the Thumbs Up to give Kudos.

    Kees Stolker

    A big fan of Power Query and Excel

     

     

    • Richard_Halsall's avatar
      Richard_Halsall
      Helper IV

      Hi

      So the code I provided allows me to create a new query for each row, what I am trying to achieve, as this file is a template file to be distributed to colleagues, if I create 8 new queries named Query 1, Query 2, Query 3 etc can I rename the query using code to the value in the column named asset

       

      e.g. Query 1 renamed to T-16 441 using code

      Thanks

  • Still not entirely sure what you want to achieve, but NO, you can not rename a query by code on PowerQuery.

    If you are working in Excel, you CAN use VBA to rename queries, but you will not hav access to the query results in VBA...

    Kees Stolker

    A big fan of Power Query and Excel