M Code - change field names

JasL

Board Regular
Joined
Dec 16, 2002
Messages
79
Hi

I need to change a number of field Names in a DataTable. Dozens, and it may chnage going fwd, hence the need for automation.

I have Table (FieldNameChgTable) with the fields to change. Can anybody PLEASE help with the M-Code to loop through the FieldNameChgTable to update the DataTable.
I can do it with VBA, but I'm trying toget more skilled in M-Code - but not sure how to do this. :(

Example

Pre Change
DataTable
Field1, Field2, Field3, Field4
x, x, x, x,
x, x, x, x,

Changes
FieldNameChgTable
From, To
Field1, NewName1
Field4, NewName4

Post Change
DataTable
NewName1, Field2, Field3, NewName4
x, x, x, x,
x, x, x, x,

Thx for your time :)
 

Some videos you may like

Excel Facts

What did Pito Salas invent?
Pito Salas, working for Lotus, popularized what would become to be pivot tables. It was released as Lotus Improv in 1989.

JasL

Board Regular
Joined
Dec 16, 2002
Messages
79
Thx for help, but I couldnt get this to work....
= Table.RenameColumns(#"Pre Change",Table.ToRows(Changes))
Went with:

let
Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
#"Renamed Columns" = Table.RenameColumns(Source, List.Zip({Table.ColumnNames(Source), Table.ColumnNames(Table1)}) )
in
#"Renamed Columns"

Where
Table 1
a, b
x, x

Table 2
A1, B1
x, x

Code imports Table 2, then ReNames fields based on Table 1
Table 2 (imported)
a, b
x, x
 
Last edited:

macfuller

Active Member
Joined
Apr 30, 2014
Messages
257
I think you'll need to work on the table schema rather than within the table itself. Sorry I haven't altered it to suit you but I hope this is quicker to get you started than to wait a couple of days for me to try a solution.

This code changes the column type based on search criteria. I hope if you have your list of before and after column names you can use much the same logic to swap names.

Code:
    SearchText = "Daily",
    SearchColumnType = "Any.Type",
    NewColumnType = "type text",


//    --------------------------------------------------------------
//    Change the column types based on column type
//    --------------------------------------------------------------

    Source = Excel.CurrentWorkbook(){[Name="tblCart"]}[Content],


    GetTableSchema = Table.Schema(Source),


//    Match the type for a transformation.  Set to <> to change all others
    FilterRowsToMatchType = Table.SelectRows(GetTableSchema, each ([TypeName] = SearchColumnType)),
    GetFirstListColumn = Table.SelectColumns(  FilterRowsToMatchType,{List.First(Table.ColumnNames(FilterRowsToMatchType))}),
    ConvertToList = Table.ToList(GetFirstListColumn),
    CountOfChanges = List.Count(ConvertToList),
    TransformColumnsType = Table.TransformColumnTypes(ChangeColTypes, Table.ToRows(Table.FromColumns({ConvertToList, List.Repeat({type text}, CountOfChanges  )}))),
 

macfuller

Active Member
Joined
Apr 30, 2014
Messages
257
Sorry, I made an incorrect assumption. What you posted should be fine. But you can certainly use the Table.Schema for other manipulations of column type etc if you need further changes.
 

Forum statistics

Threads
1,089,581
Messages
5,409,123
Members
403,253
Latest member
iscoupon01

This Week's Hot Topics

  • help please
    SORRY NOT ANY GOOD AT EXCEL SO HELP WOULD BE MUCH APPRECIATED this formula is in a sheet called ignore...
  • two formulas needed
    Hello, I'll try my best to explain this: First formula needed in Sheet1 cell A2: If Sheet1 cell B2 = Sheet2 cell B2 then return a 1. If not then...
  • Dynamic Counts
    Good afternoon, we are tidying up some data & the data seems to be growing quicker than we are tidying it up! What we confirm (by reviewing it...
  • Help Excel formula eliminate duplicate values and keep only 2 identical rows.
    as picture below column A has a duplicate value. but the values are not the same as the rule. sometimes 4 rows, sometimes 10 rows or 7 or 9...
  • Macro Compile Error Sub or Function not defined
    Hello, I am trying to run macros from a validation list, all macros have been created and run perfectly on there own but I'm getting a compile...
  • Last row combined with Current Region VBA
    I'm generally happy finding the last row of data through something like Lastrow = Cells(Rows.Count, "D").End(xlUp) but I don't always receive data...
Top