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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Learn more

Reply
Mic1979
Post Partisan
Post Partisan

Change a function to reduce the lines of code

Dear all,

 

I have the following custom function:

 

(
tbl, 
Column1, 
Column2 , 
Column1_cond as list , 
Column2_cond as list, 
ColumnToChange1, 
ColumnToChange2, 
ColumnToChange3, 
ColumnToChange4, 
ColumnToChange5, 
ColumnToChange6, 
ColumnToChange7, 
ColumnToChange8, 
ColumnToChange9,
ColumnToChange10,
ColumnToChange11,
ColumnToChange12,
ColumnToChange14,
ColumnToChange15 ) =>
 
let
    New_Table = Table.FromRecords(Table.TransformRows(tbl, each
                if  List.Contains(Column1_cond, Record.Field(_, Column1)) and List.Contains(Column2_cond, Record.Field(_, Column2))
                then Record.TransformFields(_, {
                     {ColumnToChange1, (x)=> "A"} ,          
                     {ColumnToChange2, (x)=> "B"},
                     {ColumnToChange3, (x)=> "C"},
                     {ColumnToChange4, (x)=> "E"},
                     {ColumnToChange5, (x)=> "F"},
                     {ColumnToChange6, (x)=> "G"},
                     {ColumnToChange7, (x)=> "H"},
                     {ColumnToChange8, (x)=> "I"},
                     .......,
                     .......,
                     {ColumnToChange15, (x)=> "Z"}
                     })
                else _ ), Value.Type(Table.FirstN(tbl, 0)) )
in
    New_Table
 
Columns from ColumnToChange1 to ColumnToChange15 are all the columns of the table, excludingColumn1 and Column2.
Could you suggest an alternative way to write this code, instead of listing all the ColumnToChange?
 
Thanks for your support.
 
 
1 ACCEPTED SOLUTION
wdx223_Daniel
Super User
Super User

(
tbl, 
Column1, 
Column2 , 
Column1_cond as list , 
Column2_cond as list, 
ColumnsToChange as Lists) =>
 
let
    New_Table = Table.FromRecords(Table.TransformRows(tbl, each
                if  List.Contains(Column1_cond, Record.Field(_, Column1)) and List.Contains(Column2_cond, Record.Field(_, Column2))
                then Record.TransformFields(_, List.Transform(ColumnsToChange,each {_{0},(x)=>_{1}}))
                else _ ), Value.Type(Table.FirstN(tbl, 0)) )
in
    New_Table
 
the format of ColumnsToChange should be:
{
                     {ColumnToChange1,  "A"} ,          
                     {ColumnToChange2, "B"},
                     {ColumnToChange3,"C"},
                     {ColumnToChange4, "E"},
                     {ColumnToChange5, "F"},
                     {ColumnToChange6,  "G"},
                     {ColumnToChange7, "H"},
                     {ColumnToChange8,  "I"},
                     .......,
                     .......,
                     {ColumnToChange15,  "Z"}
                     }

View solution in original post

4 REPLIES 4
wdx223_Daniel
Super User
Super User

(
tbl, 
Column1, 
Column2 , 
Column1_cond as list , 
Column2_cond as list, 
ColumnsToChange as Lists) =>
 
let
    New_Table = Table.FromRecords(Table.TransformRows(tbl, each
                if  List.Contains(Column1_cond, Record.Field(_, Column1)) and List.Contains(Column2_cond, Record.Field(_, Column2))
                then Record.TransformFields(_, List.Transform(ColumnsToChange,each {_{0},(x)=>_{1}}))
                else _ ), Value.Type(Table.FirstN(tbl, 0)) )
in
    New_Table
 
the format of ColumnsToChange should be:
{
                     {ColumnToChange1,  "A"} ,          
                     {ColumnToChange2, "B"},
                     {ColumnToChange3,"C"},
                     {ColumnToChange4, "E"},
                     {ColumnToChange5, "F"},
                     {ColumnToChange6,  "G"},
                     {ColumnToChange7, "H"},
                     {ColumnToChange8,  "I"},
                     .......,
                     .......,
                     {ColumnToChange15,  "Z"}
                     }

Hello,

 

many thanks for your help. It works. I will accept this as solution.

Hello @wdx223_Daniel 

 

Many thanks 😊

I will try it.

lbendlin
Super User
Super User

use Table.RenameColumns or Table.TransformColumnNames

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Kudoed Authors