Forum Discussion

hdhillon's avatar
hdhillon
Icon for Advocate III rankAdvocate III
5 years ago
Solved

Replace non blanks with 1

All,   In Power Query, How do I replace all non blanks with 1   I have tried the Excel Find and Replace method of   *  to   1  However this does not seem to work in Power BI    
  • edhans's avatar
    5 years ago

    Hi hdhillon , try this:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSi/KLy1QMFTSUQJhUwOlWB10QUMDrKLmpmiiRljVGuMUNUIRNYKai2KsEZoTgEwQAjETi1PSYNxYAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Group = _t, Month = _t, Value = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Value", Int64.Type}, {"Month", Int64.Type}}),
        #"Replaced Value" = 
            Table.ReplaceValue(
                #"Changed Type",
                each [Group],
                each
                    let
                        varReplacementValue = if Text.Length([Group]) > 0 then "1" else [Group]
                    in
                varReplacementValue,
                Replacer.ReplaceText,
                {"Group"}
            )
    in
        #"Replaced Value"

    It turns this:

    Into this:

    It looks for any field that has a length greater than 0 and replaces that field with "1" if it does.

     

    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.