Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
Sarath5c8
Frequent Visitor

Transpose Power query based condition

I have a single column data which need to be transformed in list once expanded the required output should be in given format.

Input.

Value
John
K
USA
NJ
Add Ken
Ken
S
UK
London
67078
Add Sara
Sara
Jenny
JPN
Tokyo
4567-354

Expected Output: 

FirstNameLastNameCountryStatePostalCode
JohnKUSANJnull
KenSUKLondon67078
SaraJennyJPNTokyo4567-354

the split should happen based the condition where "Add" word is present and the next one is the new record. If corresponding attribute is not present then null should be displayed example: the first record Postal code is not present hence null.

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Sarath5c8 

You can put the following code to Advanced editor in power query

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8srPyFOK1YlW8gaTocGOYNrPC0w5pqQoeKdCFUDpYIhCiHqf/LyUfIi4mbmBuQVcV3BiUSJEOYzhlZqXVwlhBfiB6ZD87Mp8MMvE1Mxc19jURCk2FgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Value", type text}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each if [Index]=1 or Text.Contains([Value],"Add") then Text.Trim(Text.Replace([Value],"Add","")) else null),
    #"Filled Down" = Table.FillDown(#"Added Custom",{"Custom"}),
    #"Filtered Rows" = Table.SelectRows(#"Filled Down", each not Text.Contains([Value], "Add")),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Index"}),
    #"Grouped Rows" = Table.Group(#"Removed Columns", {"Custom"}, {{"Count", each Table.AddIndexColumn(_,"Index",1,1),type table}}),
    #"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows", "Count", {"Value", "Index"}, {"Value", "Index"}),
    #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Expanded Count", {{"Index", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Expanded Count", {{"Index", type text}}, "en-US")[Index]), "Index", "Value"),
    #"Removed Columns1" = Table.RemoveColumns(#"Pivoted Column",{"Custom"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns1",{{"1", "FirstName"}, {"2", "LastName"}, {"3", "Country"}, {"4", "State"}, {"5", "PostalCode"}})
in
    #"Renamed Columns"

Output

vxinruzhumsft_0-1708500401186.png

Best Regards!

Yolo Zhu

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

Hi @Sarath5c8 

You can put the following code to Advanced editor in power query

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8srPyFOK1YlW8gaTocGOYNrPC0w5pqQoeKdCFUDpYIhCiHqf/LyUfIi4mbmBuQVcV3BiUSJEOYzhlZqXVwlhBfiB6ZD87Mp8MMvE1Mxc19jURCk2FgA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Value = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Value", type text}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1, Int64.Type),
    #"Added Custom" = Table.AddColumn(#"Added Index", "Custom", each if [Index]=1 or Text.Contains([Value],"Add") then Text.Trim(Text.Replace([Value],"Add","")) else null),
    #"Filled Down" = Table.FillDown(#"Added Custom",{"Custom"}),
    #"Filtered Rows" = Table.SelectRows(#"Filled Down", each not Text.Contains([Value], "Add")),
    #"Removed Columns" = Table.RemoveColumns(#"Filtered Rows",{"Index"}),
    #"Grouped Rows" = Table.Group(#"Removed Columns", {"Custom"}, {{"Count", each Table.AddIndexColumn(_,"Index",1,1),type table}}),
    #"Expanded Count" = Table.ExpandTableColumn(#"Grouped Rows", "Count", {"Value", "Index"}, {"Value", "Index"}),
    #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Expanded Count", {{"Index", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Expanded Count", {{"Index", type text}}, "en-US")[Index]), "Index", "Value"),
    #"Removed Columns1" = Table.RemoveColumns(#"Pivoted Column",{"Custom"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns1",{{"1", "FirstName"}, {"2", "LastName"}, {"3", "Country"}, {"4", "State"}, {"5", "PostalCode"}})
in
    #"Renamed Columns"

Output

vxinruzhumsft_0-1708500401186.png

Best Regards!

Yolo Zhu

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

AlienSx
Super User
Super User

 

(upd)

let
    Source = single_column_data_table,
    type_text = Table.TransformColumnTypes(Source,{{"Value", type text}}),
    cols = {"FirstName", "LastName", "Country", "State", "PostalCode"},
    make_record = (tbl as table) =>
        [skip = Byte.From(Text.StartsWith(Table.FirstValue(tbl), "Add ")),
        rec = Record.FromList(
            List.Skip(tbl[Value], skip), 
            List.FirstN(cols, Table.RowCount(tbl) - skip))][rec],
    gr = Table.Group(
        type_text, "Value", {{"r", make_record}},
        GroupKind.Local, (s, c) => Byte.From(Text.StartsWith(c, "Add "))),
    z = Table.FromRecords(gr[r], cols, MissingField.UseNull)
in
    z

 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.