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
phaneendra
Frequent Visitor

Help with Transpose/UnPivot table in power BI

Hi Everyone,

 

I need your support in resolving an issue I am currently facing in Power BI

I have a sample data table as mentioned below

Emp IDDesignationLocationJan_HCJan_ACFeb_HCFeb_ACMar_HCMar_ACApr_HCApr_ACMay_HCMay_ACJun_HCJun_ACJul_HCJul_ACAug_HCAug_ACSep_HCSep_ACOct_HCOct_ACNov_HCNov_ACDec_HCDec_AC
123456789ManagerIndia10101010100.70100.2010101010
234567890Assistant ManagerIndia101010000000000000000000
234567890ManagerUSA0000001010100.500.500.500.500.5000
234567890ManagerEurope0000000000000.500.500.500.500.5010

 

HC represents Heacount & AC represents Attrition count

For every user we have 12 months of Headcount & Attrition data

 

The desired output is something like this in Power BI. preferably using Power Query transformations.

Emp IDDesignationLocationMonthHCAC
123456789ManagerIndiaJan10
123456789ManagerIndiaFeb10
123456789ManagerIndiaMar10
123456789ManagerIndiaApr10
123456789ManagerIndiaMay10
123456789ManagerIndiaJun0.70
123456789ManagerIndiaJul10
123456789ManagerIndiaAug0.20
123456789ManagerIndiaSep10
123456789ManagerIndiaOct10
123456789ManagerIndiaNov10
123456789ManagerIndiaDec10
234567890Assistant ManagerIndiaJan10
234567890Assistant ManagerIndiaFeb10
234567890Assistant ManagerIndiaMar10
234567890Assistant ManagerIndiaApr00
234567890Assistant ManagerIndiaMay00
234567890Assistant ManagerIndiaJun00
234567890Assistant ManagerIndiaJul00
234567890Assistant ManagerIndiaAug00
234567890Assistant ManagerIndiaSep00
234567890Assistant ManagerIndiaOct00
234567890Assistant ManagerIndiaNov00
234567890Assistant ManagerIndiaDec00
234567890ManagerUSAJan00
234567890ManagerUSAFeb00
234567890ManagerUSAMar00
234567890ManagerUSAApr10
234567890ManagerUSAMay10
234567890ManagerUSAJun10
234567890ManagerUSAJul0.50
234567890ManagerUSAAug0.50
234567890ManagerUSASep0.50
234567890ManagerUSAOct0.50
234567890ManagerUSANov0.50
234567890ManagerUSADec00
234567890ManagerEuropeJan00
234567890ManagerEuropeFeb00
234567890ManagerEuropeMar00
234567890ManagerEuropeApr00
234567890ManagerEuropeMay00
234567890ManagerEuropeJun00
234567890ManagerEuropeJul0.50
234567890ManagerEuropeAug0.50
234567890ManagerEuropeSep0.50
234567890ManagerEuropeOct0.50
234567890ManagerEuropeNov0.50
234567890ManagerEuropeDec10

 

It would be highly appreciated if any one could provide a feasible solution for this issue

 

Thanks in advance.

 

Regards,

Phani

2 ACCEPTED SOLUTIONS
Daryl-Lynch-Bzy
Resident Rockstar
Resident Rockstar

Hi @phaneendra  - please try video like the following to learn how to pivot and unpivot data.  How and why to Unpivot data with Power Query  

View solution in original post

v-yiruan-msft
Community Support
Community Support

Hi @phaneendra ,

You can follow the below steps to get it in Power Query Editor, please find the details in the attachment.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyNjE1M7ewVNJR8k3MS0xPLQKyPPNSMhOBtCEQGxBBG+iZY4gYEaU3VidaCeYEkKhjcXFmcUliXokC8a6hDkZ3CcL+0GBHPDpxh4kpqSwC7nAtLcovSCXHcyQ7BRo5sQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Emp ID" = _t, Designation = _t, Location = _t, Jan_HC = _t, Jan_AC = _t, Feb_HC = _t, Feb_AC = _t, Mar_HC = _t, Mar_AC = _t, Apr_HC = _t, Apr_AC = _t, May_HC = _t, May_AC = _t, Jun_HC = _t, Jun_AC = _t, Jul_HC = _t, Jul_AC = _t, Aug_HC = _t, Aug_AC = _t, Sep_HC = _t, Sep_AC = _t, Oct_HC = _t, Oct_AC = _t, Nov_HC = _t, Nov_AC = _t, Dec_HC = _t, Dec_AC = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Emp ID", Int64.Type}, {"Designation", type text}, {"Location", type text}, {"Jan_HC", Int64.Type}, {"Jan_AC", Int64.Type}, {"Feb_HC", Int64.Type}, {"Feb_AC", Int64.Type}, {"Mar_HC", Int64.Type}, {"Mar_AC", Int64.Type}, {"Apr_HC", Int64.Type}, {"Apr_AC", Int64.Type}, {"May_HC", Int64.Type}, {"May_AC", Int64.Type}, {"Jun_HC", type number}, {"Jun_AC", Int64.Type}, {"Jul_HC", type number}, {"Jul_AC", Int64.Type}, {"Aug_HC", type number}, {"Aug_AC", Int64.Type}, {"Sep_HC", type number}, {"Sep_AC", Int64.Type}, {"Oct_HC", type number}, {"Oct_AC", Int64.Type}, {"Nov_HC", type number}, {"Nov_AC", Int64.Type}, {"Dec_HC", Int64.Type}, {"Dec_AC", Int64.Type}}),
    #"Reversed Rows" = Table.ReverseRows(#"Changed Type"),
    #"Unpivoted Only Selected Columns" = Table.Unpivot(#"Reversed Rows", {"Dec_AC", "Dec_HC", "Nov_AC", "Nov_HC", "Oct_AC", "Oct_HC", "Sep_AC", "Sep_HC", "Aug_AC", "Aug_HC", "Jul_AC", "Jul_HC", "Jun_AC", "Jun_HC", "May_AC", "May_HC", "Apr_AC", "Apr_HC", "Mar_AC", "Mar_HC", "Feb_AC", "Feb_HC", "Jan_AC", "Jan_HC"}, "Attribute", "Value"),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Only Selected Columns", "Attribute", Splitter.SplitTextByDelimiter("_", QuoteStyle.Csv), {"Month", "Type"}),
    #"Pivoted Column" = Table.Pivot(#"Split Column by Delimiter", List.Distinct(#"Split Column by Delimiter"[Type]), "Type", "Value", List.Sum)
in
    #"Pivoted Column"

yingyinr_0-1670226360744.png

Best Regards

Community Support Team _ Rena
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

3 REPLIES 3
v-yiruan-msft
Community Support
Community Support

Hi @phaneendra ,

You can follow the below steps to get it in Power Query Editor, please find the details in the attachment.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjQyNjE1M7ewVNJR8k3MS0xPLQKyPPNSMhOBtCEQGxBBG+iZY4gYEaU3VidaCeYEkKhjcXFmcUliXokC8a6hDkZ3CcL+0GBHPDpxh4kpqSwC7nAtLcovSCXHcyQ7BRo5sQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Emp ID" = _t, Designation = _t, Location = _t, Jan_HC = _t, Jan_AC = _t, Feb_HC = _t, Feb_AC = _t, Mar_HC = _t, Mar_AC = _t, Apr_HC = _t, Apr_AC = _t, May_HC = _t, May_AC = _t, Jun_HC = _t, Jun_AC = _t, Jul_HC = _t, Jul_AC = _t, Aug_HC = _t, Aug_AC = _t, Sep_HC = _t, Sep_AC = _t, Oct_HC = _t, Oct_AC = _t, Nov_HC = _t, Nov_AC = _t, Dec_HC = _t, Dec_AC = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Emp ID", Int64.Type}, {"Designation", type text}, {"Location", type text}, {"Jan_HC", Int64.Type}, {"Jan_AC", Int64.Type}, {"Feb_HC", Int64.Type}, {"Feb_AC", Int64.Type}, {"Mar_HC", Int64.Type}, {"Mar_AC", Int64.Type}, {"Apr_HC", Int64.Type}, {"Apr_AC", Int64.Type}, {"May_HC", Int64.Type}, {"May_AC", Int64.Type}, {"Jun_HC", type number}, {"Jun_AC", Int64.Type}, {"Jul_HC", type number}, {"Jul_AC", Int64.Type}, {"Aug_HC", type number}, {"Aug_AC", Int64.Type}, {"Sep_HC", type number}, {"Sep_AC", Int64.Type}, {"Oct_HC", type number}, {"Oct_AC", Int64.Type}, {"Nov_HC", type number}, {"Nov_AC", Int64.Type}, {"Dec_HC", Int64.Type}, {"Dec_AC", Int64.Type}}),
    #"Reversed Rows" = Table.ReverseRows(#"Changed Type"),
    #"Unpivoted Only Selected Columns" = Table.Unpivot(#"Reversed Rows", {"Dec_AC", "Dec_HC", "Nov_AC", "Nov_HC", "Oct_AC", "Oct_HC", "Sep_AC", "Sep_HC", "Aug_AC", "Aug_HC", "Jul_AC", "Jul_HC", "Jun_AC", "Jun_HC", "May_AC", "May_HC", "Apr_AC", "Apr_HC", "Mar_AC", "Mar_HC", "Feb_AC", "Feb_HC", "Jan_AC", "Jan_HC"}, "Attribute", "Value"),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Unpivoted Only Selected Columns", "Attribute", Splitter.SplitTextByDelimiter("_", QuoteStyle.Csv), {"Month", "Type"}),
    #"Pivoted Column" = Table.Pivot(#"Split Column by Delimiter", List.Distinct(#"Split Column by Delimiter"[Type]), "Type", "Value", List.Sum)
in
    #"Pivoted Column"

yingyinr_0-1670226360744.png

Best Regards

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

Thank you so much for the support!!!

 

Highly Appreciated!!!!👍

Daryl-Lynch-Bzy
Resident Rockstar
Resident Rockstar

Hi @phaneendra  - please try video like the following to learn how to pivot and unpivot data.  How and why to Unpivot data with Power Query  

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.