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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
mmp
Helper I
Helper I

Error wile bulk changing column names in Power Query

I'm trying to bulk change column names with power query and i'm stuck with an error i can't understand : 

 

Figure1.png

 

For that i've followed a very interesting post of Soheil Bakhshi : https://www.biinsight.com/a-power-query-custom-function-to-rename-all-columns-at-once-in-a-table/#mo...

 

for the function :

 

 

 

//fnRenameColumnsFromRefQuery
(ColumnName as text) as text =>
let
    Source = 
        if (
            List.Contains(
                Record.FieldNames(#sections[Section1]), 
                "Column Names Mapping"
                ) 
             ) = true 
        then #"Column Names Mapping" 
        else null,
    ColumnNewName = 
        try 
            if List.Contains(Source[Column Name], ColumnName) = true 
            then 
                if Text.Trim(Table.SelectRows(Source, each ([Column Name] = ColumnName)){0}[Description]) = "" 
                then ColumnName 
                else Table.SelectRows(Source, each ([Column Name] = ColumnName)){0}[Description] 
            else Source 
        otherwise ColumnName
in
    ColumnNewName

 

 

 

Then the code used for changing in my data source table :

 

 

 

let
    Source = Sql.Database("XXX.XXX.XXX.XXX", "XXXXXXX"),
    dbo_HCR120P = Source{[Schema="dbo",Item="HCR120P"]}[Data],
    #"Type modifié" = Table.TransformColumnTypes(dbo_HCR120P,{{"PACSOC", type text}, {"PACPAY", type text}, {"PALPAY", type text}, {"PAFCEE", type text}, {"PACINS", type text}, {"PACLGU", type text}, {"PACSEC", type text}, {"PANCTT", type text}, {"PAADAC", type text}, {"PAMDAC", type text}, {"PAJDAC", type text}}),
    #"Rename Column" = Table.TransformColumnNames(#"Type modifié", fnRenameColumnsFromRefQuery)
in
    #"Rename Column"

 

 

 

Could you guys help me and tell me what's wrong ?

 

Kind regards,

 

Mohammad

1 ACCEPTED SOLUTION
Jimmy801
Community Champion
Community Champion

Hello @mmp 

 

the problem is in your else clause. Use this code instead

//fnRenameColumnsFromRefQuery
(ColumnName as text) as text =>
let
    Source = 
        if (
            List.Contains(
                Record.FieldNames(#sections[Section1]), 
                "Column Names Mapping"
                ) 
             ) = true 
        then #"Column Names Mapping" 
        else null,
    ColumnNewName = 
        try 
            if List.Contains(Source[Column Name], ColumnName) = true 
            then 
                if Text.Trim(Table.SelectRows(Source, each ([Column Name] = ColumnName)){0}[Description]) = "" 
                then ColumnName 
                else Table.SelectRows(Source, each ([Column Name] = ColumnName)){0}[Description] 
            else ColumnName
        otherwise ColumnName
in
    ColumnNewName


If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

View solution in original post

1 REPLY 1
Jimmy801
Community Champion
Community Champion

Hello @mmp 

 

the problem is in your else clause. Use this code instead

//fnRenameColumnsFromRefQuery
(ColumnName as text) as text =>
let
    Source = 
        if (
            List.Contains(
                Record.FieldNames(#sections[Section1]), 
                "Column Names Mapping"
                ) 
             ) = true 
        then #"Column Names Mapping" 
        else null,
    ColumnNewName = 
        try 
            if List.Contains(Source[Column Name], ColumnName) = true 
            then 
                if Text.Trim(Table.SelectRows(Source, each ([Column Name] = ColumnName)){0}[Description]) = "" 
                then ColumnName 
                else Table.SelectRows(Source, each ([Column Name] = ColumnName)){0}[Description] 
            else ColumnName
        otherwise ColumnName
in
    ColumnNewName


If this post helps or solves your problem, please mark it as solution (to help other users find useful content and to acknowledge the work of users that helped you)
Kudoes are nice too

Have fun

Jimmy

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors