Forum Discussion

As9124141636's avatar
As9124141636
Icon for Helper I rankHelper I
4 years ago
Solved

unpivot automatically

I have an Excel file contains

 

 

 

  • Hey As9124141636 ,

     

    I think I found a solution.

    I split the table into different years. Then I'm looping over each table and do the transformation.

     

    Please check the attached file. When you open the Power BI file, you have to adapt the parameter "PathToXLSXFile" to the path and name of your XLSX file, for example to "C:\temp\Transpose.xlsx".

    Afterwards the rest should load automatically.

     

    If you need any help please let me know.
    If I answered your question I would be happy if you could mark my post as a solution โœ”๏ธ and give it a thumbs up ๐Ÿ‘

    Best regards
    Denis

    Blog: WhatTheFact.bi
    Follow me: twitter.com/DenSelimovic

4 Replies

  • As9124141636 , Having them in one sheet is not a good idea. Check if this code can help

     

    let
        Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("fZJLEgMhCETvMuss5NMCZ5nK/a8RGFNJqsK4QpQHbet5HnQ8DjIxWC7YY4yMgolKGdPrnIfQzIVD8Y3Px3lwlVEw39M+ERVZ3vkHlswMCNqMNh0eP/TMdnrRWscQ0blTTkLSDq8yUtiq7/HsbtHSNTNb80Xd0KKpvqXtkj5VNnQ+Crz3zS/faMRGucfsldeuiUI2o8EAtTSNdXHzjevp6fpO/3h1hdK62M1/CTLu6dp24uXqjXZVt+bFny8=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"2011" = _t, A = _t, B = _t, C = _t, D = _t, E = _t, F = _t, G = _t]),
        #"Changed Type" = Table.TransformColumnTypes(Source,{{"2011", Int64.Type}, {"A", Int64.Type}, {"B", Int64.Type}, {"C", Int64.Type}, {"D", Int64.Type}, {"E", Int64.Type}, {"F", Int64.Type}, {"G", Int64.Type}}),
        #"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Changed Type", {"A", "B", "C", "D", "E", "F", "G"}, "Attribute", "Value"),
        #"Renamed Columns" = Table.RenameColumns(#"Unpivoted Columns",{{"Attribute", "Year"}, {"Value", "Month"}}),
        #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Renamed Columns", {"Year", "Month"}, "Attribute", "Value")
    in
        #"Unpivoted Other Columns"
    • As9124141636's avatar
      As9124141636
      Icon for Helper I rankHelper I

      thanks

      i think you did it for one year. i want to do for all years and if new year added in excel, transform it automatically.

      I think the solution is create list of years and transform every year (in a loop) then finally append. but i dont know how to do in power query.

  • Hi,

    Why is your data in such a poor format?  Why not clean it up to atlest have all years listed down a single column and then upload the Data to PowerBI?

  • selimovd's avatar
    selimovd
    Icon for Most Valuable Professional rankMost Valuable Professional

    Hey As9124141636 ,

     

    I think I found a solution.

    I split the table into different years. Then I'm looping over each table and do the transformation.

     

    Please check the attached file. When you open the Power BI file, you have to adapt the parameter "PathToXLSXFile" to the path and name of your XLSX file, for example to "C:\temp\Transpose.xlsx".

    Afterwards the rest should load automatically.

     

    If you need any help please let me know.
    If I answered your question I would be happy if you could mark my post as a solution โœ”๏ธ and give it a thumbs up ๐Ÿ‘

    Best regards
    Denis

    Blog: WhatTheFact.bi
    Follow me: twitter.com/DenSelimovic