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
Anonymous
Not applicable

Replace multiple exact values in a single step

Hi all

 

It's my first day starting out with Power BI and M Code. I've looked through a dozen replace threads, but couldn't find what I was looking for.

 

I'm currently using the below code to replace 3 exact values with another value. I'm struggling to do this in a more efficent/elegant way (in a single step).

 

#"Replaced Value" = Table.ReplaceValue(#"Changed Type","0","Unknown",Replacer.ReplaceValue,{"EU Member State"}),

#"Replaced Value1" = Table.ReplaceValue(#"Changed Type","1","Yes",Replacer.ReplaceValue,{"EU Member State"}),
#"Replaced Value2" = Table.ReplaceValue(#"Replaced Value","2","No",Replacer.ReplaceValue,{"EU Member State"})

 

Any help would be greatly appreciated!

 

Note: I don't want to use a lookup table.


Thanks,

Jay.

1 ACCEPTED SOLUTION
hnguy71
Memorable Member
Memorable Member

@Fowmy has a great solution and I think that's what you're looking for. If you want a more advanced technique, you can use an array to find and replace bulk items. Here is a sample:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlCK1YlWMgSTRmDSsyQ1VyE8syRDIbggMTlVKTYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"EU Member State" = _t]),
    ReplaceArr = 
        [
            0 = "Unknown",
            1 = "Yes",
            2 = "No",
            #"Item With Space" = "Replace Item With Space"
        ],
    FindAndReplace = Table.TransformColumns(Source, {{"EU Member State", each Record.FieldOrDefault(ReplaceArr, _, _)}})
in
    FindAndReplace

 

 

 

 



Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!

View solution in original post

4 REPLIES 4
hnguy71
Memorable Member
Memorable Member

@Fowmy has a great solution and I think that's what you're looking for. If you want a more advanced technique, you can use an array to find and replace bulk items. Here is a sample:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlCK1YlWMgSTRmDSsyQ1VyE8syRDIbggMTlVKTYWAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"EU Member State" = _t]),
    ReplaceArr = 
        [
            0 = "Unknown",
            1 = "Yes",
            2 = "No",
            #"Item With Space" = "Replace Item With Space"
        ],
    FindAndReplace = Table.TransformColumns(Source, {{"EU Member State", each Record.FieldOrDefault(ReplaceArr, _, _)}})
in
    FindAndReplace

 

 

 

 



Did I answer your question?
Please help by clicking the thumbs up button and mark my post as a solution!
Fowmy
Super User
Super User

@Anonymous 

You can use Table. Transform with IF instead.

Table.TransformColumns(#"Changed Type",{{"EU Member State", each  if _="0" then "Unknown" else if _=1 then "Yes" else if _=2 then "No" else _, type text}})

  

Did I answer your question? Mark my post as a solution! and hit thumbs up


Subscribe and learn Power BI from these videos

Website LinkedIn PBI User Group

amitchandak
Super User
Super User

@Anonymous , this video talks about list accumulate, see if that can help

https://www.youtube.com/watch?v=YWAMaas_1AU

Anonymous
Not applicable

This uses a lookup table. But I'm wanting to avoid that and instead do it all within M.

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.