Forum Discussion

PhilippeMuniesa's avatar
PhilippeMuniesa
Icon for Resolver I rankResolver I
5 years ago
Solved

Table.ReplaceText or Value, in multiple columns selected by their order number

Hello,

 

I try to write a function to make a dynamic replacement in different columns of a table. The table is not always the same, and the columns are not of the same number and do not have the same name.

 

I started with an example, and I identify in a list the 4 columns in which the replacement must be apply.

 

But after, I always get stuck, I would use the list as the parameter of the function Table.ReplaceValue or Text,

 

I made this draft.

------------------------

let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIGYhMgNgViMyA2B2ILILZUitWJBstiQbGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t, Column5 = _t, Column6 = _t, Column7 = _t, Column8 = _t]),

W=1,
X=2,
Y=3,
Z=7,
Liste_des_Colonne_A_Supprimer = {Table.ColumnNames(Source){W},Table.ColumnNames(Source){X},Table.ColumnNames(Source){Y},Table.ColumnNames(Source){Z}}

//Wrong
//#"Replaced Value" = Table.ReplaceValue(Source,"","0",Replacer.ReplaceValue,{Table.ColumnNames(Source){List.Count(Table.ColumnNames(Source)) - 3}})

//Wrong
// #"Replaced Value" = Table.ReplaceValue(Source,"","0",Replacer.ReplaceValue,{Table.ColumnNames(Source),List.FromText(Liste_des_Colonne_A_Supprimer)})


//Wrong
// #"Replaced Value" = Table.ReplaceValue(Source,"","0",Replacer.ReplaceValue{ Table.SelectColumns( Source,List.FromText( Liste_des_Colonne_A_Supprimer ) , null) } )

in
//#"Replaced Value"
Liste_des_Colonne_A_Supprimer

-----------------------

but I block miserably and rely on the help of a super User

 

Thanks a lot

 

Philippe Muniesa

  • Hi PhilippeMuniesa 

    I'm not sure I understand but try this:

    let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIGYhMgNgViMyA2B2ILILZUitWJBstiQbGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t, Column5 = _t, Column6 = _t, Column7 = _t, Column8 = _t]),
    
    W=1,
    X=2,
    Y=3,
    Z=7,
    Liste_des_Colonne_A_Supprimer = {Table.ColumnNames(Source){W},Table.ColumnNames(Source){X},Table.ColumnNames(Source){Y},Table.ColumnNames(Source){Z}},
    #"Replaced Value" = Table.ReplaceValue(Source,"","0",Replacer.ReplaceValue,Liste_des_Colonne_A_Supprimer)
    in
    #"Replaced Value"
    

     

    Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

    Contact me privately for support with any larger-scale BI needs, tutoring, etc.

    Cheers 

  • Hello PhilippeMuniesa 

     

    here a dynamic approach. The list NumberList contains the number of columns, where the replaces have to be applied.

    let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIGYhMgNgViMyA2B2ILILZUitWJBstiQbGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t, Column5 = _t, Column6 = _t, Column7 = _t, Column8 = _t]),
    
    NumberList ={1,2,3,7},
    NameOfColumnsToApply = List.Accumulate(NumberList,{}, (p,c)=> p & {Table.ColumnNames(Source){c}} ),
    #"Replaced Value" = Table.ReplaceValue(Source,"","0",Replacer.ReplaceValue,NameOfColumnsToApply )
    in
    #"Replaced Value"

     

    Copy paste this code to the advanced editor in a new blank query to see how the solution works.

    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

2 Replies

  • AlB's avatar
    AlB
    Icon for Community Champion rankCommunity Champion

    Hi PhilippeMuniesa 

    I'm not sure I understand but try this:

    let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIGYhMgNgViMyA2B2ILILZUitWJBstiQbGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t, Column5 = _t, Column6 = _t, Column7 = _t, Column8 = _t]),
    
    W=1,
    X=2,
    Y=3,
    Z=7,
    Liste_des_Colonne_A_Supprimer = {Table.ColumnNames(Source){W},Table.ColumnNames(Source){X},Table.ColumnNames(Source){Y},Table.ColumnNames(Source){Z}},
    #"Replaced Value" = Table.ReplaceValue(Source,"","0",Replacer.ReplaceValue,Liste_des_Colonne_A_Supprimer)
    in
    #"Replaced Value"
    

     

    Please mark the question solved when done and consider giving a thumbs up if posts are helpful.

    Contact me privately for support with any larger-scale BI needs, tutoring, etc.

    Cheers 

  • Jimmy801's avatar
    Jimmy801
    Icon for Community Champion rankCommunity Champion

    Hello PhilippeMuniesa 

     

    here a dynamic approach. The list NumberList contains the number of columns, where the replaces have to be applied.

    let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUTIGYhMgNgViMyA2B2ILILZUitWJBstiQbGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t, Column5 = _t, Column6 = _t, Column7 = _t, Column8 = _t]),
    
    NumberList ={1,2,3,7},
    NameOfColumnsToApply = List.Accumulate(NumberList,{}, (p,c)=> p & {Table.ColumnNames(Source){c}} ),
    #"Replaced Value" = Table.ReplaceValue(Source,"","0",Replacer.ReplaceValue,NameOfColumnsToApply )
    in
    #"Replaced Value"

     

    Copy paste this code to the advanced editor in a new blank query to see how the solution works.

    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