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
Borja204
Helper II
Helper II

Flatten excel matrix when importing into powerBi

Hi,

 

First of all I'm very new to PowerBi.

 

My problem is about importing the data into powerBi. I have to import an excel file as datasource. I can't touch that excel and the data comes in the following format:

 

 202001202001202001202002202002202002202003202003202003
 Value1Value2Value3Value1Value2Value3Value1Value2Value3
Car 11456755278
Car 25667879887789983

 

When importing the file and to be able to use it for my dashboards and metrics I need to transform (qith the query editor) to the following format:

 

CarNameDateValue1Value2Value3
Car 1202001145
Car 12020026755
Car 1202003278
Car 2202001566787
Car 2202002988778
Car 22020039983

 

I'm totally lost in this ( at least I know the format I need to use the data).

 

Any help is apreciated,

 

Big thanks in advance and regards,

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hello @Borja204,

I did some testing with the sample you provided and I came up with the following solution:

  1. Load data and make sure "Use first row as header is not being used" (This will cause issues with duplicate column names)
  2. Transpose the table
  3. Promote Header (This will make it so the car column gets created in the next step
  4. Unpivot Car 1 and Car 2
  5. Now re-pivot column "_1" with the Value as the value column => go into advanced and set aggregation to "Dont Aggregate"

Step 5 is optionial to recieve the data as per format, personally i'd rather skip this last step.

Here is the full code based on an input table:

let
  Source = Table.FromRows(
    Json.Document(
      Binary.Decompress(
        Binary.FromText(
          "i45WUlDSUTIyMDIwMMTNMMLNMMbCiNWBGBuWmFOaaghjGMEYxmRKgYx1TixSAMmBsAkQmwKxmTmUAcIg9SC+BVw5SMTUDKbOAkRYWsBY5iCWJUwIaEcsAA==", 
          BinaryEncoding.Base64
        ), 
        Compression.Deflate
      )
    ), 
    let
      _t = ((type nullable 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, 
        Column10 = _t
      ]
  ),
  #"Transposed Table" = Table.Transpose(Source),
  #"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table", [PromoteAllScalars = true]),
  #"Unpivoted Columns" = Table.UnpivotOtherColumns(
    #"Promoted Headers", 
    {" ", " _1"}, 
    "Attribute", 
    "Value"
  ),
  #"Pivoted Column" = Table.Pivot(
    #"Unpivoted Columns", 
    List.Distinct(#"Unpivoted Columns"[#" _1"]), 
    " _1", 
    "Value"
  ),
  #"Changed Type1" = Table.TransformColumnTypes(
    #"Pivoted Column", 
    {{" ", Int64.Type}, {"Value1", Int64.Type}, {"Value2", Int64.Type}, {"Value3", Int64.Type}}
  )
in
  #"Changed Type1"

 

Hope it helps

 

Kind regards
Joren Venema

Data & Analytics Consultant
If this reply solved your question be sure to mark this post as the solution to help others find the answer more easily. 

 

View solution in original post

5 REPLIES 5
FarhanAhmed
Community Champion
Community Champion

I think you need to do following

 

- In transformation you first need to "Transpose" rows into columns

Transpose001.png

 

 

 

 

 

 

 

 

 

-- Promote First row as headers

Transpose002.png

 

---UnPivot Other Columns by select Column1 & 2

Transpose003.png

 

 

 

I hope this helps you what you are trying to achieve

 







Did I answer your question? Mark my post as a solution! Appreciate your Kudos!!

Proud to be a Super User!




Thank you mate, it helped me, just the last repivot step missing but the screenshots helped me a lot!

amitchandak
Super User
Super User

Anonymous
Not applicable

Hello @Borja204,

I did some testing with the sample you provided and I came up with the following solution:

  1. Load data and make sure "Use first row as header is not being used" (This will cause issues with duplicate column names)
  2. Transpose the table
  3. Promote Header (This will make it so the car column gets created in the next step
  4. Unpivot Car 1 and Car 2
  5. Now re-pivot column "_1" with the Value as the value column => go into advanced and set aggregation to "Dont Aggregate"

Step 5 is optionial to recieve the data as per format, personally i'd rather skip this last step.

Here is the full code based on an input table:

let
  Source = Table.FromRows(
    Json.Document(
      Binary.Decompress(
        Binary.FromText(
          "i45WUlDSUTIyMDIwMMTNMMLNMMbCiNWBGBuWmFOaaghjGMEYxmRKgYx1TixSAMmBsAkQmwKxmTmUAcIg9SC+BVw5SMTUDKbOAkRYWsBY5iCWJUwIaEcsAA==", 
          BinaryEncoding.Base64
        ), 
        Compression.Deflate
      )
    ), 
    let
      _t = ((type nullable 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, 
        Column10 = _t
      ]
  ),
  #"Transposed Table" = Table.Transpose(Source),
  #"Promoted Headers" = Table.PromoteHeaders(#"Transposed Table", [PromoteAllScalars = true]),
  #"Unpivoted Columns" = Table.UnpivotOtherColumns(
    #"Promoted Headers", 
    {" ", " _1"}, 
    "Attribute", 
    "Value"
  ),
  #"Pivoted Column" = Table.Pivot(
    #"Unpivoted Columns", 
    List.Distinct(#"Unpivoted Columns"[#" _1"]), 
    " _1", 
    "Value"
  ),
  #"Changed Type1" = Table.TransformColumnTypes(
    #"Pivoted Column", 
    {{" ", Int64.Type}, {"Value1", Int64.Type}, {"Value2", Int64.Type}, {"Value3", Int64.Type}}
  )
in
  #"Changed Type1"

 

Hope it helps

 

Kind regards
Joren Venema

Data & Analytics Consultant
If this reply solved your question be sure to mark this post as the solution to help others find the answer more easily. 

 

 You are a beast!!!

 

Thank you mate your instructions guided me into the correct approach with the real data (which was a bit diferent)

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.