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
McJasem
Frequent Visitor

Help with using Table.ReplaceValue on several row values under several different Columns

Hi all,

 

Hope there's a couple of PowerGenuises that can help me.

 

I wish to use the Table.ReplaceValue funktion to find and change several different row values under several columns. as show underneath, all values "null", "blank", "0" & "n/a" should be changes to Not Set, across all columns.

 

SubSegmentSegmentSubCategoryCategory
 accessories0accessories
RYORYORYOn/a
null0 accessories
accessoriesaccessoriesaccessoriesnull

 

= Table.ReplaceValue(#"Source",null,"Not Set",Replacer.ReplaceValue,{"SubSegment", "Segment", "SubCategory", "Category"})

= Table.ReplaceValue(#"Source",0,"Not Set",Replacer.ReplaceValue,{"SubSegment", "Segment", "SubCategory", "Category"})

= Table.ReplaceValue(#"Source","","Not Set",Replacer.ReplaceValue,{"SubSegment", "Segment", "SubCategory", "Category"})

= Table.ReplaceValue(#"Source",n/a,"Not Set",Replacer.ReplaceValue,{"SubSegment", "Segment", "SubCategory", "Category"})

 

I was wondering how i could put all these replace values into one Table.ReplaceValue step - keeping in mind the cleaning shouldn't affect the processing of data, because i have millions of rows.

2 ACCEPTED SOLUTIONS
CNENFRNL
Community Champion
Community Champion

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WUtJRSkxOTi0uzi/KTC0G8gzQRGJ1opWCIv2Boshknn4iWAaqQQGLJlRj8fHySnNylGJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [SubSegment = _t, Segment = _t, SubCategory = _t, Category = _t]),

    #"Replaced Value" = List.Accumulate(
        Table.ColumnNames(Source),
        Source,
        (s,c) => Table.ReplaceValue(
            s, null, null, (x,y,z) => if List.Contains({"", "0", "N/A", null, "null"}, Text.Trim(x), Comparer.OrdinalIgnoreCase) then "Not Set" else x, {c}
        )
    )
in
    #"Replaced Value"

Screenshot 2021-08-11 202007.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

View solution in original post

Hi edhans

 

Thanks for the swift response and tips💪😎you are a powerbi champ!

 

Meanwhile I tried another workaround that worked, and I would like to hear your thoughts on it.

 

= Table.TransformColumns(#"Trimmed Text", {{"EAN Code", each if _ = "" then null else _}}, each if List.Contains({null, "", 0, "n/a"}, _) then "Not Set" else _)

 

Let me hear if this is a viable solution, although I can see it solved my problem with changing several different values under different columns to one value if found.

 

 

View solution in original post

5 REPLIES 5
CNENFRNL
Community Champion
Community Champion

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WUtJRSkxOTi0uzi/KTC0G8gzQRGJ1opWCIv2Boshknn4iWAaqQQGLJlRj8fHySnNylGJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [SubSegment = _t, Segment = _t, SubCategory = _t, Category = _t]),

    #"Replaced Value" = List.Accumulate(
        Table.ColumnNames(Source),
        Source,
        (s,c) => Table.ReplaceValue(
            s, null, null, (x,y,z) => if List.Contains({"", "0", "N/A", null, "null"}, Text.Trim(x), Comparer.OrdinalIgnoreCase) then "Not Set" else x, {c}
        )
    )
in
    #"Replaced Value"

Screenshot 2021-08-11 202007.png


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

Ahhh... I didn't even think about using list.accumulate here...



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
CNENFRNL
Community Champion
Community Champion

Yep, I remember having read a blog, saying that recursion/List.Accumulate/List.Generate are only ways to loop in PQ. Seems too few choices; but not too bad at all, it's easy to make a decision.


Thanks to the great efforts by MS engineers to simplify syntax of DAX! Most beginners are SUCCESSFULLY MISLED to think that they could easily master DAX; but it turns out that the intricacy of the most frequently used RANKX() is still way beyond their comprehension!

DAX is simple, but NOT EASY!

edhans
Super User
Super User

I'll be curious to see if someone can do this. You can use the below code to replace multiple different values with one value, so I did your logic on the SubSegment field:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WUtJRSkxOTi0uzi/KTC0G8gzQRGJ1opWCIv2Boshknn4iWAaqQQGLJlRj8fHySnNylGJjAQ==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [SubSegment = _t, Segment = _t, SubCategory = _t, Category = _t]),
    #"Replaced Value" = 
    Table.ReplaceValue(
        Source,
        each if [SubSegment] = ""  or [SubSegment] = null or [SubSegment] = 0 [SubSegment] = "n/a" then [SubSegment] else null,
        "Not Set",
        Replacer.ReplaceValue,
        {"SubSegment"}
    )
in
    #"Replaced Value"

 


However, I am not sure how you can do that logic on multiple fields to pass the right info to the "find" part of the Table.ReplaceValue function.

And even if you could, now you are throwing in a lot of if/then/else constructs and I am not sure that is going to make it very performant

How to use M code provided in a blank query:
1) In Power Query, select New Source, then Blank Query
2) On the Home ribbon, select "Advanced Editor" button
3) Remove everything you see, then paste the M code I've given you in that box.
4) Press Done
5) See this article if you need help using this M code in your model.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Hi edhans

 

Thanks for the swift response and tips💪😎you are a powerbi champ!

 

Meanwhile I tried another workaround that worked, and I would like to hear your thoughts on it.

 

= Table.TransformColumns(#"Trimmed Text", {{"EAN Code", each if _ = "" then null else _}}, each if List.Contains({null, "", 0, "n/a"}, _) then "Not Set" else _)

 

Let me hear if this is a viable solution, although I can see it solved my problem with changing several different values under different columns to one value if found.

 

 

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
Top Kudoed Authors