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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Datatouille
Solution Sage
Solution Sage

Power Query Challenge

Hello everyone,

 

I'd be interested in your thoughts for this Power Query question !

 

Here is how my database looks like

 

Img IDAgeGender
130;40;50M;F;M
235;38;45F;F;M
323;28;50M;M;F

 

and here is how it should look like after cleansing:

Img IDAgeGender
130M
140F
150M
235F
238F
245M
323M
328M
350F

 

"Double Unpivot" is impossible here. This is what I am doing for the moment to solve the issue:

I am creating two separate tables referencing my original table.

 

I am using the first separate table to delete Age Column, split& unpivot Gender

I am using the second separate table to delete Gender Column, split & unpivot Age

 

Then, I am merging these 2 tables based on Imgid and the index generated.

 

I am wondering if you guys have a better approach (probably using M tricks?) ?

1 ACCEPTED SOLUTION
MarcelBeug
Community Champion
Community Champion

You can split the text in Age and Gender to nested lists,

then add a column with the nested lists combined in nested tables,

expand the nested tables,

remove the old columns and

rename and type the new columns.

 

    SplittedAge = Table.TransformColumns(PreviousStep,{{"Age", each Text.Split(_,";")}}),
    SplittedGender = Table.TransformColumns(SplittedAge,{{"Gender", each Text.Split(_,";")}}),
    #"Added Custom" = Table.AddColumn(SplittedGender, "AgeAndGender", each Table.FromColumns({[Age],[Gender]})),
    #"Expanded AgeAndGender" = Table.ExpandTableColumn(#"Added Custom", "AgeAndGender", {"Column1", "Column2"}, {"Column1", "Column2"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded AgeAndGender",{"Age", "Gender"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Column1", "Age"}, {"Column2", "Gender"}}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Renamed Columns",{{"Gender", type text}, {"Age", type text}})

 

Specializing in Power Query Formula Language (M)

View solution in original post

1 REPLY 1
MarcelBeug
Community Champion
Community Champion

You can split the text in Age and Gender to nested lists,

then add a column with the nested lists combined in nested tables,

expand the nested tables,

remove the old columns and

rename and type the new columns.

 

    SplittedAge = Table.TransformColumns(PreviousStep,{{"Age", each Text.Split(_,";")}}),
    SplittedGender = Table.TransformColumns(SplittedAge,{{"Gender", each Text.Split(_,";")}}),
    #"Added Custom" = Table.AddColumn(SplittedGender, "AgeAndGender", each Table.FromColumns({[Age],[Gender]})),
    #"Expanded AgeAndGender" = Table.ExpandTableColumn(#"Added Custom", "AgeAndGender", {"Column1", "Column2"}, {"Column1", "Column2"}),
    #"Removed Columns" = Table.RemoveColumns(#"Expanded AgeAndGender",{"Age", "Gender"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns",{{"Column1", "Age"}, {"Column2", "Gender"}}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Renamed Columns",{{"Gender", type text}, {"Age", type text}})

 

Specializing in Power Query Formula Language (M)

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.