Forum Discussion

KiranGupta15's avatar
KiranGupta15
Frequent Visitor
5 years ago
Solved

Key Value pair values

HI All. I  am new to BI. I have below text in a column of a table, I want to extract the Name,ID,City into different columns Duplicate values also there in my string.     tx:StudentID:24tgfg, t...
  • edhans's avatar
    5 years ago

    Hi KiranGupta15 ,

     

    I can get you this far, but I need more info from you. I turned this:

    into this:

    using this code:

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WKqmwCi4pTUnNK/F0sTIyKUlPS9dRAAr6JeamWjlnpJYk5oH5zpkllVaOpcUlmRB+cUliSapVSIQOyIDSJKus9PQsEATx/YvSrTKysjLSs4ACGRAxp1Cr1PKi8qKicjDUQbPX0tjEAGGvd2YRIWuVYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
        #"Split Column by Delimiter" = Table.SplitColumn(Source, "Column1", Splitter.SplitTextByDelimiter("tx:", QuoteStyle.Csv), {"Column1.1", "Column1.2", "Column1.3", "Column1.4", "Column1.5", "Column1.6", "Column1.7", "Column1.8", "Column1.9", "Column1.10", "Column1.11", "Column1.12"}),
        #"Transposed Table" = Table.Transpose(#"Split Column by Delimiter"),
        #"Split Column by Delimiter1" = Table.SplitColumn(#"Transposed Table", "Column1", Splitter.SplitTextByDelimiter(":", QuoteStyle.Csv), {"Column1.1", "Column1.2"}),
        #"Filtered Rows" = Table.SelectRows(#"Split Column by Delimiter1", each ([Column1.2] <> null)),
        #"Extracted Text Before Delimiter" = Table.TransformColumns(#"Filtered Rows", {{"Column1.2", each Text.BeforeDelimiter(_, ","), type text}}),
        #"Capitalized Each Word" = Table.TransformColumns(#"Extracted Text Before Delimiter",{{"Column1.1", Text.Proper, type text}}),
        #"Filtered Rows1" = Table.SelectRows(#"Capitalized Each Word", each ([Column1.1] = "City" or [Column1.1] = "Name" or [Column1.1] = "Studentid"))
    in
        #"Filtered Rows1"

     

    However, I cannot get the Name, ID, and City into columns because there is nothing to tell me what student, ID, and city are associated with each other, unless I just assume it is in sequential order, in which case I can get this:

    using this code:

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WKqmwCi4pTUnNK/F0sTIyKUlPS9dRAAr6JeamWjlnpJYk5oH5zpkllVaOpcUlmRB+cUliSapVSIQOyIDSJKus9PQsEATx/YvSrTKysjLSs4ACGRAxp1Cr1PKi8qKicjDUQbPX0tjEAGGvd2YRIWuVYmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t]),
        #"Split Column by Delimiter" = Table.SplitColumn(Source, "Column1", Splitter.SplitTextByDelimiter("tx:StudentID", QuoteStyle.Csv), {"Column1.1", "Column1.2", "Column1.3"}),
        #"Removed Other Columns" = Table.SelectColumns(#"Split Column by Delimiter",{"Column1.2", "Column1.3"}),
        #"Transposed Table" = Table.Transpose(#"Removed Other Columns"),
        #"Added Index" = Table.AddIndexColumn(#"Transposed Table", "Index", 0, 1, Int64.Type),
        #"Split Column by Delimiter1" = Table.SplitColumn(#"Added Index", "Column1", Splitter.SplitTextByDelimiter("tx:", QuoteStyle.Csv), {"Column1.1", "Column1.2", "Column1.3", "Column1.4", "Column1.5", "Column1.6", "Column1.7"}),
        #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Split Column by Delimiter1", {"Index"}, "Attribute", "Value"),
        #"Split Column by Delimiter2" = Table.SplitColumn(#"Unpivoted Other Columns", "Value", Splitter.SplitTextByDelimiter(":", QuoteStyle.Csv), {"Value.1", "Value.2"}),
        #"Replaced Value" = Table.ReplaceValue(#"Split Column by Delimiter2","","StudentID",Replacer.ReplaceValue,{"Value.1"}),
        #"Extracted Text Before Delimiter" = Table.TransformColumns(#"Replaced Value", {{"Value.2", each Text.BeforeDelimiter(_, ","), type text}}),
        #"Filtered Rows" = Table.SelectRows(#"Extracted Text Before Delimiter", each ([Value.1] = "City" or [Value.1] = "Name" or [Value.1] = "StudentID")),
        #"Removed Other Columns1" = Table.SelectColumns(#"Filtered Rows",{"Index", "Value.1", "Value.2"}),
        #"Pivoted Column" = Table.Pivot(#"Removed Other Columns1", List.Distinct(#"Removed Other Columns1"[Value.1]), "Value.1", "Value.2"),
        #"Removed Columns" = Table.RemoveColumns(#"Pivoted Column",{"Index"})
    in
        #"Removed Columns"

     

    If that is not what you need, please be a bit more specific on the expected results, and explain how I am to read that sample record you posted.

     

    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.

     

    If you need to post more data to make it clearer, please see the links below on putting tables in the posts.

    How to get good help fast. Help us help you.
    How to Get Your Question Answered Quickly
    How to provide sample data in the Power BI Forum