Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
5 years ago
Solved

Replace null values with unique values

Hi,

 

I need to replace null values in a column with unique values that are some combination of text and a incrementing number (for example "Some code 1", "Some code 2"). Main problem that I have is that all values in this column have to be unique so null values need to replaced with unique values.

 

Thank you.

  • Anonymous ,

     

    Try this mcode:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCs7PTVVIzk9JVTBUitWJVgITCEEjbILGSrGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
        #"Replaced Value" = Table.ReplaceValue(#"Changed Type","",null,Replacer.ReplaceValue,{"Column1"}),
        #"Grouped Rows" = Table.Group(#"Replaced Value", {"Column1"}, {{"Rows", each if _[Column1]{0} = null then Table.AddIndexColumn(_, "Index", 1,1) else _, type table}}),
        #"Removed Other Columns" = Table.SelectColumns(#"Grouped Rows",{"Rows"}),
        #"Expanded Rows" = Table.ExpandTableColumn(#"Removed Other Columns", "Rows", {"Column1", "Index"}, {"Column1", "Index"}),
        #"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Expanded Rows", {{"Index", type text}}, "pt-BR"),{"Column1", "Index"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"Merged")
    in
        #"Merged Columns"

     

    Also, check the attached file.

2 Replies

  • Anonymous , you can create an index column starting with a very big number, when code 1, 2 will not reach and then replace null with code & [index]

  • camargos88's avatar
    camargos88
    Community Champion

    Anonymous ,

     

    Try this mcode:

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCs7PTVVIzk9JVTBUitWJVgITCEEjbILGSrGxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}}),
        #"Replaced Value" = Table.ReplaceValue(#"Changed Type","",null,Replacer.ReplaceValue,{"Column1"}),
        #"Grouped Rows" = Table.Group(#"Replaced Value", {"Column1"}, {{"Rows", each if _[Column1]{0} = null then Table.AddIndexColumn(_, "Index", 1,1) else _, type table}}),
        #"Removed Other Columns" = Table.SelectColumns(#"Grouped Rows",{"Rows"}),
        #"Expanded Rows" = Table.ExpandTableColumn(#"Removed Other Columns", "Rows", {"Column1", "Index"}, {"Column1", "Index"}),
        #"Merged Columns" = Table.CombineColumns(Table.TransformColumnTypes(#"Expanded Rows", {{"Index", type text}}, "pt-BR"),{"Column1", "Index"},Combiner.CombineTextByDelimiter("", QuoteStyle.None),"Merged")
    in
        #"Merged Columns"

     

    Also, check the attached file.