Forum Discussion
Victoria-9
Mar 19, 2019Copper Contributor
Data model conversion-Power Query
Hello,all.
I have encountered a problem, as shown in the photo.
I got some data (Figure 1) and want to convert it to the form of Figure 2.I have entered Power Query through the From Table(Figure 3), but don't know what to do next.
(Figure 1)
(Figure 2)
(Figure 3)
Maybe you can give me some suggestions or a way to learn.
Any help would be greatly appreciated.
Thanks.
7 Replies
Sort By
Hi Victoria-9 ,
Initial idea of how to work with multi-row headers is from Gil Raviv, at least I picked-up it here. Now don't remember there are the details, in his book or blog. In brief, transformation is like
generated script is
let Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content], MergeFirstThreeColumns = Table.CombineColumns(Source, {"Column1", "Column2", "Column3"}, Combiner.CombineTextByDelimiter("=", QuoteStyle.None),"Merged"), TransposeTable = Table.Transpose(MergeFirstThreeColumns), FirstRowAsHeader = Table.PromoteHeaders(TransposeTable, [PromoteAllScalars=true]), UnpivoteOtherThanFirstTwoColumns = Table.UnpivotOtherColumns(FirstRowAsHeader, {"==", "Number=Name=Group"}, "Attribute", "Value"), PivotMonths = Table.Pivot(UnpivoteOtherThanFirstTwoColumns, List.Distinct(UnpivoteOtherThanFirstTwoColumns[#"Number=Name=Group"]), "Number=Name=Group", "Value"), SplitFirstColumnsBack = Table.SplitColumn(PivotMonths, "Attribute", Splitter.SplitTextByDelimiter("=", QuoteStyle.Csv), {"Attribute.1", "Attribute.2", "Attribute.3"}), RenameAsBefore = Table.RenameColumns(SplitFirstColumnsBack, {{"==", " "}, {"Attribute.1", "Number"}, {"Attribute.2", "Name"}, {"Attribute.3", "Group"}}), ProperColumnsOrder = Table.ReorderColumns(RenameAsBefore,{"Number", "Name", "Group", "J", "K", " "}) in ProperColumnsOrder
and file is attached.
Hope above are self-explained if repeat step by step. Didn't play with final sorting, recognised too late it's better to do one.
- Victoria-9Copper Contributor
Hi SergeiBaklan
Thank you very much!
I followed your steps to change the information and tried it. I have succeeded! But I made a mistake in my question.
The value contained in the 'Number' column should be ‘123456’ instead of ‘a123’. After replacing the ‘number’ with ‘letter + number’ in my table , I succeeded.
Do you know how to modify the step if the number column is all numeric?
Victoria-9 , in addition I added months sorting by adding month number in front of each its name before pivoting; removed that index on latest steps.
- PeterBartholomew1Silver Contributor
I do not think tables or PQ are really intended to deal with two levels of heading. If you treat the J and K columns as two separate tables (this can be achieved within PQ by deleting columns). Then unpivot each using 'other columns' with Number, Name and Group selected to get two tables of the form you require and save each as a connection only.
Then, starting with the first table, merge the second using Number, Name, Group and Month from each as key fields for the join. Finally expand the second table extracting the value field only.
- Victoria-9Copper Contributor
Thanks for the way you provide, I will try it. Before, I saw a way to process this data using PQ in the same table, perhaps changing the title form, but I forgot,I will continue to look for.
Thanks very much for your help.
- PeterBartholomew1Silver Contributor
I think I have found A way of doing what you want without splitting the table into two. I won't claim it is THE way because I am just working by trial and error!
If you create single level headers by manually concatenating the J/K with the month (a space will do as a separator) and then unpivot, you get a table twice as long as you want. If you then split the compound column into its letter and date parts, you can (re)pivot on the new letter column to give the J and K output in separate columns.
I am not sure whether this helps you but I am learning!