Forum Discussion
Urgent Help - Power Query - Data Transformation
- 1 year ago
Hi jaineshp
You will need to shape your data so the two headers are parallel in two separate columns. You can do that by accessing the first and second row before promoting the headers and use the combination of the two as the new column names. Unpivot your data after then split the attribute into two separate columns.
let Source = Excel.Workbook(File.Contents("D:\testdata.xlsx"), null, true), Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data], #"Automatically Renamed Columns" = let tbl = Sheet1_Sheet, header1 = Record.ToList(Sheet1_Sheet{0}), header2 = Record.ToList(Sheet1_Sheet{1}), zipped = List.Zip({header1, header2}), combined = List.Transform( zipped, //each (if _{0} = null then _{1} else _{0}) & "__" & _{1} //each (if _{0} = null then _{1} & "" else _{0}) & "__" & _{1} each (if _{0} = null then "" else _{0}) & "__" & _{1} ), OriginalColumns = Table.ColumnNames(tbl), renamevalues = List.Zip({OriginalColumns,combined }), renamed = Table.RenameColumns(tbl, renamevalues) in renamed, #"Removed Top Rows" = Table.Skip(#"Automatically Renamed Columns",2), #"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Removed Top Rows", {"__Type", "__Qty", "__USD"}, "Attribute", "Value"), #"Renamed Columns" = Table.RenameColumns(#"Unpivoted Other Columns",{{"__Type", "Type"}, {"__Qty", "Qty"}, {"__USD", "USD"}}), #"Split Column by Delimiter" = Table.SplitColumn(#"Renamed Columns", "Attribute", Splitter.SplitTextByDelimiter("__", QuoteStyle.Csv), {"Attribute.1", "Attribute.2"}), #"Renamed Columns1" = Table.RenameColumns(#"Split Column by Delimiter",{{"Attribute.1", "Month"}, {"Attribute.2", "Category"}}), #"Added Custom" = Table.AddColumn(#"Renamed Columns1", "Month Sort", each Date.Month(Date.From([Month] & "1, 2025"))), #"Changed Type" = Table.TransformColumnTypes(#"Added Custom",{{"Value", Int64.Type}, {"Month Sort", Int64.Type}, {"Type", type text}, {"Qty", type text}, {"USD", type text}}) in #"Changed Type"Please see the attached sample pbix.
Hi SundarRaj
Thank you for your response.
Yes, you are right, this is the data I will have to deal with. Also, please note, values of double header S, T, X, Y these would change every time, basically this the customer name. It changes every month.
I'm unable to open the link, would you mind providing the sample PBI file directly.
Hi Lio123 ,
Yes, that makes sense. The customer names would change dynamically here since no change has been done to them. Just that the double headers issue has been solved.
Thanks,
Sundar
- Lio1231 year agoAdvocate I
Hi SundarRaj ,
The data in the excel is perfect, but how to handle this in Power Query/ Power BI ?- Sundar26001 year agoRegular Visitor
Hey Lio123,
Open the Excel file and just launch the Power Query Editor through the search bar. You'll see all the steps associated with the query and the output. Let me know in case of any issues. If you'd like detailed steps explanation, do let me know as well. Thanks
- v-dineshya1 year agoCommunity Support
Hi Sundar2600 , Thank you for your prompt response.
Hi Lio123 , Could you please try the proposed solution shared by Sundar2600 , Please do let us know if you have any further queries.
Regards,
Dinesh