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

Join the FabCon + SQLCon recap series. Up next: Power BI, Real-Time Intelligence, IQ and AI, and Data Factory take center stage. All sessions are available on-demand after the live show. Register now

Reply
MAVIE
Helper I
Helper I

Dynamically Rename to ID column if ID column is missing

Hello Everyone, 

 

From sharepoint, I am getting a series of lookup columns which have the following format:

ApplicationId  Application  ServerId  Server  EnvironmentId  Environment  
1Record1Record1Record

 

However, these lookup columns can be changed to a custom column type within sharepoint, such that I would recieve the data in this format: 

Application  Server  Environment  
111

 

I would like the rename the second table to have the naming conversion "_Id", however I do not know ahead of time which column types have been changed within sharepoint, and thus I could potentially recieve a table which looks like the following: 

Application  ServerId  Server  Environment  
11Record1

 

Therefore, I need to dynamically check if the ID column exists, and if it does not exist then rename the corresponding column to be the ID column. In the table above for example, it would be renaming {{"Application", "ApplicationId"}, {"Environment", "Environment"}}. 

There are over 100 columns in the entire table, and this is only relevant for some of these columns. I have a list of the exact columns that might be affected, but do not know wheter or not some have been changed.

 

Any help would be greatly appreciated.

1 ACCEPTED SOLUTION
Vijay_A_Verma
Most Valuable Professional
Most Valuable Professional

Use below code. ListOfColumns is that list which needs to be populated. You can also keep this list in a separate list query and replace ListOfColumns by your list.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSAeOg1OT8ohQoRyk2FgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Application = _t, ServerId = _t, Server = _t, Environment = _t, abc = _t]),
    ListOfColumns = {"Application", "abc", "Server"},
    Custom1 = Table.TransformColumnNames(Source,(x)=>if List.Contains(ListOfColumns,x) then if List.Contains(Table.ColumnNames(Source),x&"Id") then x else x&"Id" else x)
in
    Custom1

View solution in original post

4 REPLIES 4
Vijay_A_Verma
Most Valuable Professional
Most Valuable Professional

Inset this step in your code

= Table.TransformColumnNames(Source,(x)=>if List.Contains(Table.ColumnNames(Source),if Text.EndsWith(x,"Id") then x else x&"Id") then x else x&"Id")

See the working here - Open a blank query - Home - Advanced Editor - Remove everything from there and paste the below code to test

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSAeOg1OT8ohQwJzYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Application = _t, ServerId = _t, Server = _t, Environment = _t]),
    Custom1 = Table.TransformColumnNames(Source,(x)=>if List.Contains(Table.ColumnNames(Source),if Text.EndsWith(x,"Id") then x else x&"Id") then x else x&"Id")
in
    Custom1

Perhaps I should have clarified. There are over 100 columns in the table and this has to be applied to only some of these. I have a list of all of the columns where this is relevant for.

Vijay_A_Verma
Most Valuable Professional
Most Valuable Professional

Use below code. ListOfColumns is that list which needs to be populated. You can also keep this list in a separate list query and replace ListOfColumns by your list.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSAeOg1OT8ohQoRyk2FgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Application = _t, ServerId = _t, Server = _t, Environment = _t, abc = _t]),
    ListOfColumns = {"Application", "abc", "Server"},
    Custom1 = Table.TransformColumnNames(Source,(x)=>if List.Contains(ListOfColumns,x) then if List.Contains(Table.ColumnNames(Source),x&"Id") then x else x&"Id" else x)
in
    Custom1

That seems to do the trick. Thank you very much for your help!

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

Check out the April 2026 Power BI update to learn about new features.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

FabCon and SQLCon Highlights Carousel

FabCon &SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.