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

Score big with last-minute savings on the final tickets to FabCon Vienna. Secure your discount

Reply
LAH
New Member

Handling Poor Data in Power BI

Hi Everyone,

 

I am very new to Power BI and attempting to teach myself how to use it. I have some very poorly layed out data that I wish to change the format of. Can anyone walk me through how to do this? I could use a python script to ammend to output a cleaner version of the data but I was hoping to instead learn more Power BI functionality. 

 

Here is the format of the origonal data.

 

Algeria
Country risk assessments :
C
Business climate assessments :
C
Angola
Country risk assessments :
C
Business climate assessments :
D
Argentina
Country risk assessments :
D
Business climate assessments :
B


And I want to data to move into columns and look like the following: 

Country     |  Country risk assessments | Business climate assessments |
Algeria       |  C                                      |  C
Angola       |  C                                      |  D
Argentina  |  D                                      |  B


So there are a number of problems to fix here.
1) The multiple "titles" of "Country risk assessments"and "Business climate assessments"
and 
2) Realiging the rows to columns which can't just be a transpose as they are sequential like headings

Any help would be much appreaciated 

1 ACCEPTED SOLUTION
parry2k
Super User
Super User

@LAH start a new blank query and copy this M code, follow these steps in your table, tweak the solution as you see fit.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcsxJTy3KTFSK1YlWcs4vzSspqlQoyizOVkgsLk4tLs5NzSspVrCCSINJp9LizDygjEJyTmZuYkkqToWOeen5OdQ12AVicFE6UCwzjyizXYg120kpNhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Data = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Data", type text}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    #"Added Group" = Table.AddColumn(#"Added Index", "Group", each [Index]-Number.Mod([Index],5), Int64.Type),
    #"Added Column Order" = Table.AddColumn(#"Added Group", "Column Order", each Number.Mod([Index],5), Int64.Type),
    #"Removed Index" = Table.RemoveColumns(#"Added Column Order",{"Index"}),
    #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Removed Index", {{"Column Order", type text}}, "en-CA"), List.Distinct(Table.TransformColumnTypes(#"Removed Index", {{"Column Order", type text}}, "en-CA")[#"Column Order"]), "Column Order", "Data"),
    #"Renamed Columns" = Table.RenameColumns(#"Pivoted Column",{{"0", "Country"}, {"2", "Country risk assessments"}, {"4", "Business climate assessments"}}),
    #"Removed Other Columns" = Table.SelectColumns(#"Renamed Columns",{"Country", "Country risk assessments", "Business climate assessments"})
in
    #"Removed Other Columns"

 

Output:

 

parry2k_0-1739295408472.png

 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

View solution in original post

1 REPLY 1
parry2k
Super User
Super User

@LAH start a new blank query and copy this M code, follow these steps in your table, tweak the solution as you see fit.

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WcsxJTy3KTFSK1YlWcs4vzSspqlQoyizOVkgsLk4tLs5NzSspVrCCSINJp9LizDygjEJyTmZuYkkqToWOeen5OdQ12AVicFE6UCwzjyizXYg120kpNhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Data = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Data", type text}}),
    #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1, Int64.Type),
    #"Added Group" = Table.AddColumn(#"Added Index", "Group", each [Index]-Number.Mod([Index],5), Int64.Type),
    #"Added Column Order" = Table.AddColumn(#"Added Group", "Column Order", each Number.Mod([Index],5), Int64.Type),
    #"Removed Index" = Table.RemoveColumns(#"Added Column Order",{"Index"}),
    #"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Removed Index", {{"Column Order", type text}}, "en-CA"), List.Distinct(Table.TransformColumnTypes(#"Removed Index", {{"Column Order", type text}}, "en-CA")[#"Column Order"]), "Column Order", "Data"),
    #"Renamed Columns" = Table.RenameColumns(#"Pivoted Column",{{"0", "Country"}, {"2", "Country risk assessments"}, {"4", "Business climate assessments"}}),
    #"Removed Other Columns" = Table.SelectColumns(#"Renamed Columns",{"Country", "Country risk assessments", "Business climate assessments"})
in
    #"Removed Other Columns"

 

Output:

 

parry2k_0-1739295408472.png

 



Subscribe to the @PowerBIHowTo YT channel for an upcoming video on List and Record functions in Power Query!!

Learn Power BI and Fabric - subscribe to our YT channel - Click here: @PowerBIHowTo

If my solution proved useful, I'd be delighted to receive Kudos. When you put effort into asking a question, it's equally thoughtful to acknowledge and give Kudos to the individual who helped you solve the problem. It's a small gesture that shows appreciation and encouragement! ❤


Did I answer your question? Mark my post as a solution. Proud to be a Super User! Appreciate your Kudos 🙂
Feel free to email me with any of your BI needs.

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

Check out the August 2025 Power BI update to learn about new features.

August 2025 community update carousel

Fabric Community Update - August 2025

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