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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
sreejitsnair
New Member

How to convert data from multiple merged columns and sub columns to rows using power query

Hi all,

 

Have recently started exploring powerBI and have been fascinated with the power query functions.

I managed to use unpivot finction to transform my data from columns to rows. While working with multiple merged columns and sub columns, I am unable to get the desired results. I have been trying to transfrom this data:

 

 

                                       PTM Data(Cell B to E)                      SMC Data (Cell F to I)

School NameNos. of PTMMaleFemaleTotalNos. of SMCMaleFemaleTotal
School 113581235
School 212351235
School 351633493101929

 

(Can't copy the merged columns above hence headers mentioned in text)

 

To this:

 

School NameEventEvent NumberMaleFemaleTotal
School 1PTM1358
School 2PTM1235
School 3PTM5163349
School 1SMC1235
School 2SMC1235
School 3SMC3101929

 

Can this be done using unpivot or any other function in PowerBI ?

 

Regards,

 

Sreejit

2 ACCEPTED SOLUTIONS
Anonymous
Not applicable

Result: 

 

hunterfeldman_1-1598040416452.png


What I used for the source code:

PTM and SCM are merged + centered cells 

hunterfeldman_2-1598040468559.png

 

M Query Code and steps taken: 

 

hunterfeldman_3-1598040570858.png 

hunterfeldman_4-1598040590116.png


Explanation: 
After loading the data, make sure no headers were promoted.

hunterfeldman_5-1598040738859.png

 


Your first step is to transpose the table under the 'Transform' tab. This will bring down our PTM and SCM Event types.

hunterfeldman_6-1598040758422.png

 

From there we select Fill --> Fill Down from the 'Transform' tab. This will fill in the null values left by the merged excel cells. 

hunterfeldman_7-1598040863979.png

 

After that we ctrl + select both [Column1] aka Event Type and [Column2] aka Gender, then go to 'Add Column' tab and select Merge Columns. I choose not to add a separator but you can use whatever, this will come into play later. 

hunterfeldman_8-1598041023060.png

 

After merging, transpose the table again.

hunterfeldman_9-1598041060031.png

 

Congratulations, Headers, you're being promoted.

hunterfeldman_10-1598041107970.png

 

From there ctrl + select all of the columns except for [School Name] and [Event #]. Go back to the 'Transform' tab and select Unpivot Columns --> Unpivot Only Selected Columns. 

hunterfeldman_11-1598041285806.png

Now we're getting somewhere. Select the [Attribute] aka merged column and choose Split Column --> Split by Position under the 'Transform' tab. You'll need to split by delimiter if you used one during the merge column step. Just split by whatever delimiter you choose. I just like splitting by position. 

hunterfeldman_12-1598041486732.png

 

Finally, select [Attribute.2] column and select Pivot Column under the 'Transform' tab. Add finishing touches like header names and type changes. 

hunterfeldman_13-1598041554565.png

 

While this is method is possible it's a little cumbersome and will run much slower on larger datasets. Personally, I would either try to restructure the data you pull or organize it differently. For example I would ditch the total column all together and let DAX take care of it on the back end. I like to have my data organized like the following as I think you'll find it easier to manipulte with DAX:

hunterfeldman_14-1598041836918.png

 

Special thanks to the following website for guidance, this was fun problem to solve!
https://www.newtechdojo.com/unpivot-columns-in-power-bi/#dualrow



 

 

 

View solution in original post

Anonymous
Not applicable

all steps excuted from GUI:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WUtJRCgjxxUoGO2MjY3WilYKTM/LzcxT8EnNTgaJ++cV6CvlpChBdvok5IEG31FwIIyS/JDEHSVWwrzNOVUhmGwIFQdgYiE2B2ALKN4KLISk2wpDEoxiuwAwkC+KZWELVGRqACBDPyFIpNhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t, Column5 = _t, Column6 = _t, Column7 = _t, Column8 = _t, Column9 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}, {"Column4", type text}, {"Column5", type text}, {"Column6", type text}, {"Column7", type text}, {"Column8", type text}, {"Column9", type text}}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
    #"Merged Columns" = Table.CombineColumns(#"Promoted Headers",{"PTM", "PTM_1", "PTM_2", "PTM_3"},Combiner.CombineTextByDelimiter(":", QuoteStyle.None),"PTM"),
    #"Merged Columns1" = Table.CombineColumns(#"Merged Columns",{"SCM", "SCM_4", "SCM_5", "SCM_6"},Combiner.CombineTextByDelimiter(":", QuoteStyle.None),"SCM"),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Merged Columns1", {""}, "Attribute", "Value"),
    #"Promoted Headers1" = Table.PromoteHeaders(#"Unpivoted Other Columns", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers1",{{"School Name", type text}, {"PTM", type text}, {"Nos. of PTM:Male:Female:Total", type text}}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Changed Type1", "Nos. of PTM:Male:Female:Total", Splitter.SplitTextByDelimiter(":", QuoteStyle.Csv), {"Nos. of PTM:Male:Female:Total.1", "Nos. of PTM:Male:Female:Total.2", "Nos. of PTM:Male:Female:Total.3", "Nos. of PTM:Male:Female:Total.4"}),
    #"Changed Type2" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Nos. of PTM:Male:Female:Total.1", type text}, {"Nos. of PTM:Male:Female:Total.2", type text}, {"Nos. of PTM:Male:Female:Total.3", type text}, {"Nos. of PTM:Male:Female:Total.4", type text}}),
    #"Promoted Headers2" = Table.PromoteHeaders(#"Changed Type2", [PromoteAllScalars=true]),
    #"Changed Type3" = Table.TransformColumnTypes(#"Promoted Headers2",{{"School Name", type text}, {"SCM", type text}, {"Nos. of SMC", Int64.Type}, {"Male", Int64.Type}, {"Female", Int64.Type}, {"Total", Int64.Type}})
in
    #"Changed Type3"

image.png

 

View solution in original post

2 REPLIES 2
Anonymous
Not applicable

all steps excuted from GUI:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WUtJRCgjxxUoGO2MjY3WilYKTM/LzcxT8EnNTgaJ++cV6CvlpChBdvok5IEG31FwIIyS/JDEHSVWwrzNOVUhmGwIFQdgYiE2B2ALKN4KLISk2wpDEoxiuwAwkC+KZWELVGRqACBDPyFIpNhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t, Column5 = _t, Column6 = _t, Column7 = _t, Column8 = _t, Column9 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}, {"Column4", type text}, {"Column5", type text}, {"Column6", type text}, {"Column7", type text}, {"Column8", type text}, {"Column9", type text}}),
    #"Promoted Headers" = Table.PromoteHeaders(#"Changed Type", [PromoteAllScalars=true]),
    #"Merged Columns" = Table.CombineColumns(#"Promoted Headers",{"PTM", "PTM_1", "PTM_2", "PTM_3"},Combiner.CombineTextByDelimiter(":", QuoteStyle.None),"PTM"),
    #"Merged Columns1" = Table.CombineColumns(#"Merged Columns",{"SCM", "SCM_4", "SCM_5", "SCM_6"},Combiner.CombineTextByDelimiter(":", QuoteStyle.None),"SCM"),
    #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Merged Columns1", {""}, "Attribute", "Value"),
    #"Promoted Headers1" = Table.PromoteHeaders(#"Unpivoted Other Columns", [PromoteAllScalars=true]),
    #"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers1",{{"School Name", type text}, {"PTM", type text}, {"Nos. of PTM:Male:Female:Total", type text}}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Changed Type1", "Nos. of PTM:Male:Female:Total", Splitter.SplitTextByDelimiter(":", QuoteStyle.Csv), {"Nos. of PTM:Male:Female:Total.1", "Nos. of PTM:Male:Female:Total.2", "Nos. of PTM:Male:Female:Total.3", "Nos. of PTM:Male:Female:Total.4"}),
    #"Changed Type2" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Nos. of PTM:Male:Female:Total.1", type text}, {"Nos. of PTM:Male:Female:Total.2", type text}, {"Nos. of PTM:Male:Female:Total.3", type text}, {"Nos. of PTM:Male:Female:Total.4", type text}}),
    #"Promoted Headers2" = Table.PromoteHeaders(#"Changed Type2", [PromoteAllScalars=true]),
    #"Changed Type3" = Table.TransformColumnTypes(#"Promoted Headers2",{{"School Name", type text}, {"SCM", type text}, {"Nos. of SMC", Int64.Type}, {"Male", Int64.Type}, {"Female", Int64.Type}, {"Total", Int64.Type}})
in
    #"Changed Type3"

image.png

 

Anonymous
Not applicable

Result: 

 

hunterfeldman_1-1598040416452.png


What I used for the source code:

PTM and SCM are merged + centered cells 

hunterfeldman_2-1598040468559.png

 

M Query Code and steps taken: 

 

hunterfeldman_3-1598040570858.png 

hunterfeldman_4-1598040590116.png


Explanation: 
After loading the data, make sure no headers were promoted.

hunterfeldman_5-1598040738859.png

 


Your first step is to transpose the table under the 'Transform' tab. This will bring down our PTM and SCM Event types.

hunterfeldman_6-1598040758422.png

 

From there we select Fill --> Fill Down from the 'Transform' tab. This will fill in the null values left by the merged excel cells. 

hunterfeldman_7-1598040863979.png

 

After that we ctrl + select both [Column1] aka Event Type and [Column2] aka Gender, then go to 'Add Column' tab and select Merge Columns. I choose not to add a separator but you can use whatever, this will come into play later. 

hunterfeldman_8-1598041023060.png

 

After merging, transpose the table again.

hunterfeldman_9-1598041060031.png

 

Congratulations, Headers, you're being promoted.

hunterfeldman_10-1598041107970.png

 

From there ctrl + select all of the columns except for [School Name] and [Event #]. Go back to the 'Transform' tab and select Unpivot Columns --> Unpivot Only Selected Columns. 

hunterfeldman_11-1598041285806.png

Now we're getting somewhere. Select the [Attribute] aka merged column and choose Split Column --> Split by Position under the 'Transform' tab. You'll need to split by delimiter if you used one during the merge column step. Just split by whatever delimiter you choose. I just like splitting by position. 

hunterfeldman_12-1598041486732.png

 

Finally, select [Attribute.2] column and select Pivot Column under the 'Transform' tab. Add finishing touches like header names and type changes. 

hunterfeldman_13-1598041554565.png

 

While this is method is possible it's a little cumbersome and will run much slower on larger datasets. Personally, I would either try to restructure the data you pull or organize it differently. For example I would ditch the total column all together and let DAX take care of it on the back end. I like to have my data organized like the following as I think you'll find it easier to manipulte with DAX:

hunterfeldman_14-1598041836918.png

 

Special thanks to the following website for guidance, this was fun problem to solve!
https://www.newtechdojo.com/unpivot-columns-in-power-bi/#dualrow



 

 

 

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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

Top Solution Authors
Top Kudoed Authors