Forum Discussion

BenHoward's avatar
BenHoward
Helper I
2 years ago
Solved

Replace null values in multipe columns with a different column

Hi, I am trying to replace the null values in only one of several columns with a single value from the row.  This is to help produce a heiarchy (and I am not able to do this in DAX due to data integr...
  • jennratten's avatar
    2 years ago

    Hello - this is one way you can do it.

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WckrNU9JRci3KTAZSUBSrE63kVpSagpCAqILJeWXm5laiS0I1gBUEJ+eXlGAaGwsA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Name = _t, L1 = _t, L2 = _t, L3 = _t, L4 = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"L1", type text}, {"L2", type text}, {"L3", type text}, {"L4", type text}}),
        #"Replaced Value" = Table.ReplaceValue(#"Changed Type","",null,Replacer.ReplaceValue,{"L2", "L3", "L4"}),
        Custom3 = Table.AddColumn ( #"Replaced Value", "FieldValues", each Record.FieldValues ( _ ) ),
        ReplFirstNull = Table.AddColumn ( 
            Custom3, 
            "New", each 
            let 
                varFirstNonNullPosition = List.NonNullCount ( [FieldValues] ),
                ReplaceFirstNull = Text.Combine ( List.ReplaceRange ( [FieldValues], varFirstNonNullPosition, 1, { [FieldValues]{0} } ), "|" )
            in
                ReplaceFirstNull
        ),
        #"Removed Other Columns" = Table.SelectColumns(ReplFirstNull,{"New"}),
        #"Split Column by Delimiter" = Table.SplitColumn ( #"Removed Other Columns", "New", Splitter.SplitTextByDelimiter("|", QuoteStyle.Csv), Table.ColumnNames ( Source ) )
    in
        #"Split Column by Delimiter"