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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
anshpalash
Helper II
Helper II

Data Formatting - Transpose Repeated Rows into Columns

Hi,

 

I have data in this format.  This is just one file.

 

anshpalash_1-1630077129846.png

 

I want to combine multiple files with similar format. The problem is that each file has different amount of ID numbers. I am having trouble figuring out a code that essentially tells power BI to transpose the repeated rows after the 'Parameters' row above.

 

For this file, I want format to be something like:

anshpalash_2-1630077257891.png

Below this file, new files should be merged into this which would have different IDs and Dates. The number of IDs in each file is different. Would really appreciate if someone could help me figure out how to format the data. 

 

 

 

1 ACCEPTED SOLUTION
v-yetao1-msft
Community Support
Community Support

Hi @anshpalash 

Based on the file you provided, I created a sample.

(1)I create three tables , below is a screenshot of the original data that you provided .

Ailsamsft_0-1630395839303.png

(2)Go to Power Query ,then click Merge Queries as New , merge table ‘Sale’ and ‘Parameters’ based on field “ID” .

Ailsamsft_1-1630395839305.png

Ailsamsft_2-1630395839307.png

(3)Expended Parameters  

Ailsamsft_3-1630395839308.png

(4)Remove the duplicate column [Parameters.ID] and pivot [Parameters.Column2] [Parameters.Column3]

Ailsamsft_4-1630395839309.png

Ailsamsft_5-1630395839310.png

(5)Append table ‘Merge1’ and table ‘Date’ to return a new table ‘Append 1’ .

Ailsamsft_6-1630395839310.png

Ailsamsft_7-1630395839311.png

(6)Replace the value of column [Date]

Ailsamsft_8-1630395839313.png

(7)Remove bottom row and Close & Apply to Desktop view .

Ailsamsft_9-1630395839315.png

(8)Add a table visual with table ‘Append 1’ , the final result is as shown :

Ailsamsft_10-1630395839315.png

I have attached my pbix file ,you can refer to it .

 

Best Regards

Community Support Team _ Ailsa Tao

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-yetao1-msft
Community Support
Community Support

Hi @anshpalash 

Based on the file you provided, I created a sample.

(1)I create three tables , below is a screenshot of the original data that you provided .

Ailsamsft_0-1630395839303.png

(2)Go to Power Query ,then click Merge Queries as New , merge table ‘Sale’ and ‘Parameters’ based on field “ID” .

Ailsamsft_1-1630395839305.png

Ailsamsft_2-1630395839307.png

(3)Expended Parameters  

Ailsamsft_3-1630395839308.png

(4)Remove the duplicate column [Parameters.ID] and pivot [Parameters.Column2] [Parameters.Column3]

Ailsamsft_4-1630395839309.png

Ailsamsft_5-1630395839310.png

(5)Append table ‘Merge1’ and table ‘Date’ to return a new table ‘Append 1’ .

Ailsamsft_6-1630395839310.png

Ailsamsft_7-1630395839311.png

(6)Replace the value of column [Date]

Ailsamsft_8-1630395839313.png

(7)Remove bottom row and Close & Apply to Desktop view .

Ailsamsft_9-1630395839315.png

(8)Add a table visual with table ‘Append 1’ , the final result is as shown :

Ailsamsft_10-1630395839315.png

I have attached my pbix file ,you can refer to it .

 

Best Regards

Community Support Team _ Ailsa Tao

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

parry2k
Super User
Super User

@anshpalash here it is, you can make this as a function and send file content as a parameter to the function and it will return a table output.

 

For testing, create a blank query, click advanced editor and paste this code:

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WckksSVXSUQKhWJ1oJSMDI0NdAwtdA0MkQSSmpwuQFZyYkwoTAKkzNIBrB/FMYTxjIMMIzjMB8YxhvIDEosTc1JLUomIk00GG+eQnJ5Zk5ucBmY6lxSWZeXCZkMoCkLVhqRmZyUAHwOxD0uCZl1IJF4cqd8pPLIG7BkltcGKegld+cSpcDovxJqha/CLhojDDM7OBKmMB", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}, {"Column3", type text}}),
    #"Filtered Rows" = Table.SelectRows(#"Changed Type", each ([Column1] <> "")),
    #"Added Custom" = Table.AddColumn(#"Filtered Rows", "Group", each if [Column1] = "Date" or [Column1] = "ID" or [Column1] = "Parameters" then [Column1] else null, type text),
    #"Filled Down" = Table.FillDown(#"Added Custom",{"Group"}),
    #"Filtered Rows1" = Table.SelectRows(#"Filled Down", each ([Column1] <> "Date" and [Column1] <> "ID" and [Column1] <> "Parameters")),
    #"Date Table" = Table.SelectRows(#"Filtered Rows1", each [Group] = "Date"),
    #"Date" = #"Date Table"{0}[Column1],
    #"Parameters Table" = Table.SelectRows(#"Filtered Rows1", each [Group] = "Parameters"),
    #"Removed Columns" = Table.RemoveColumns(#"Parameters Table",{"Group"}),
    #"Pivoted Column" = Table.Pivot(#"Removed Columns", List.Distinct(#"Removed Columns"[Column2]), "Column2", "Column3"),
    #"ID Table" = Table.SelectRows(#"Filtered Rows1", each [Group] = "ID"),
    #"Merged Queries" = Table.NestedJoin(#"ID Table", {"Column1"}, #"Pivoted Column", {"Column1"}, "Parameters Table", JoinKind.LeftOuter),
    #"Expanded Parameters Table" = Table.ExpandTableColumn(#"Merged Queries", "Parameters Table", {"Location", "Type"}, {"Location", "Type"}),
    #"Added Custom1" = Table.AddColumn(#"Expanded Parameters Table", "Date", each #"Date"),
    #"Removed Columns1" = Table.RemoveColumns(#"Added Custom1",{"Column3", "Group"}),
    #"Renamed Columns" = Table.RenameColumns(#"Removed Columns1",{{"Column1", "ID"}, {"Column2", "Sale"}}),
    #"Changed Type1" = Table.TransformColumnTypes(#"Renamed Columns",{{"Sale", Currency.Type}, {"Date", type date}})
in
    #"Changed Type1"

 

Follow us on LinkedIn

 

Check my latest blog post The Power of Using Calculation Groups with Inactive Relationships (Part 1) (perytus.com) I would  Kudos if my solution helped. 👉 If you can spend time posting the question, you can also make efforts to give Kudos to whoever helped to solve your problem. It is a token of appreciation!

 

Visit us at https://perytus.com, your one-stop-shop for Power BI-related projects/training/consultancy.



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.

Hi @parry2k , should I paste the code into the 'Transform Sample File' query or my main table query? I will be adding more files to the folder from which power bi is getting the file. Thanks!

Helpful resources

Announcements
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.