Hello @AllisterB,
There’s also an alternative where you’ll absolutely not be required to perform the intensive transformative steps like I’ve performed in my file.
You just need to copy and paste the M Code from my file to yours and automatically the transformed data will be loaded into your PBIX file. Below are the steps suggested for the reference -
Step 1: In the Power Query, under the “New Source” select the “Blank Query” option.
Step 2: Now, go to the “Advanced Editor” option and paste the below provided query into it.
let
Source = Excel.Workbook(File.Contents("C:\Users\har\Downloads\UnPivot Multiple Columns.xlsm"), null, true),
Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
#"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Column1", type text}, {"Column2", type any}, {"Column3", type text}, {"Column4", type any}, {"Column5", type text}, {"Column6", type any}, {"Column7", type any}, {"Column8", type any}, {"Column9", type any}, {"Column10", Int64.Type}, {"Column11", type any}, {"Column12", type any}, {"Column13", type any}, {"Column14", type any}, {"Column15", type any}, {"Column16", type any}}),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"Column2", "Column4"}),
#"Removed Top Rows" = Table.Skip(#"Removed Columns",1),
#"Filled Down" = Table.FillDown(#"Removed Top Rows",{"Column1", "Column3"}),
#"Replaced Value" = Table.ReplaceValue(#"Filled Down"," ","",Replacer.ReplaceText,{"Column5"}),
#"Replaced Value1" = Table.ReplaceValue(#"Replaced Value","Hours","A-SIL Hours",Replacer.ReplaceValue,{"Column6"}),
#"Replaced Value2" = Table.ReplaceValue(#"Replaced Value1","Contacts","A-SIL Contacts",Replacer.ReplaceValue,{"Column7"}),
#"Replaced Value3" = Table.ReplaceValue(#"Replaced Value2","Hours","B-SIL Hours",Replacer.ReplaceValue,{"Column8"}),
#"Replaced Value4" = Table.ReplaceValue(#"Replaced Value3","Contacts","B-SIL Contacts",Replacer.ReplaceValue,{"Column9"}),
#"Removed Columns1" = Table.RemoveColumns(#"Replaced Value4",{"Column10"}),
#"Replaced Value5" = Table.ReplaceValue(#"Removed Columns1","Hours","T-SIL Hours",Replacer.ReplaceValue,{"Column11"}),
#"Replaced Value6" = Table.ReplaceValue(#"Replaced Value5","Contacts","T-SIL Contacts",Replacer.ReplaceValue,{"Column12"}),
#"Replaced Value7" = Table.ReplaceValue(#"Replaced Value6","Hours","S-SIL Hours",Replacer.ReplaceValue,{"Column13"}),
#"Replaced Value8" = Table.ReplaceValue(#"Replaced Value7","Contacts","S-SIL Contacts",Replacer.ReplaceValue,{"Column14"}),
#"Replaced Value9" = Table.ReplaceValue(#"Replaced Value8","Hours","C-SIL Hours",Replacer.ReplaceValue,{"Column15"}),
#"Replaced Value10" = Table.ReplaceValue(#"Replaced Value9","Contacts","C-SIL Contacts",Replacer.ReplaceValue,{"Column16"}),
#"Removed Top Rows1" = Table.Skip(#"Replaced Value10",1),
#"Promoted Headers1" = Table.PromoteHeaders(#"Removed Top Rows1", [PromoteAllScalars=true]),
#"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers1",{{"Branch Name", type text}, {"Key Staff", type text}, {"StaffType", type text}, {"A-SIL Hours", Int64.Type}, {"A-SIL Contacts", Int64.Type}, {"B-SIL Hours", Int64.Type}, {"B-SIL Contacts", Int64.Type}, {"T-SIL Hours", Int64.Type}, {"T-SIL Contacts", Int64.Type}, {"S-SIL Hours", Int64.Type}, {"S-SIL Contacts", Int64.Type}, {"C-SIL Hours", Int64.Type}, {"C-SIL Contacts", Int64.Type}}),
#"Removed Columns2" = Table.RemoveColumns(#"Changed Type1",{"A-SIL Hours", "B-SIL Hours", "T-SIL Hours", "S-SIL Hours", "C-SIL Hours"}),
#"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Removed Columns2", {"Branch Name", "Key Staff", "StaffType"}, "Attribute", "Value"),
#"Renamed Columns" = Table.RenameColumns(#"Unpivoted Columns",{{"Attribute", "Attributes"}, {"Value", "Values"}})
in
#"Renamed Columns"
Now, once the query is loaded and the table is generated. Rename the table from “Query1” to “Contacts - As Per Sheet 1”. Since the above query provided is for the transformations performed onto the Worksheet No. 1
Step 3: Now again, go to the “Advanced Editor” option and paste the below provided query into it.
let
Source = Excel.Workbook(File.Contents("C:\Users\har\Downloads\UnPivot Multiple Columns.xlsm"), null, true),
Sheet1_Sheet = Source{[Item="Sheet1",Kind="Sheet"]}[Data],
#"Promoted Headers" = Table.PromoteHeaders(Sheet1_Sheet, [PromoteAllScalars=true]),
#"Changed Type" = Table.TransformColumnTypes(#"Promoted Headers",{{"Column1", type text}, {"Column2", type any}, {"Column3", type text}, {"Column4", type any}, {"Column5", type text}, {"Column6", type any}, {"Column7", type any}, {"Column8", type any}, {"Column9", type any}, {"Column10", Int64.Type}, {"Column11", type any}, {"Column12", type any}, {"Column13", type any}, {"Column14", type any}, {"Column15", type any}, {"Column16", type any}}),
#"Removed Columns" = Table.RemoveColumns(#"Changed Type",{"Column2", "Column4"}),
#"Removed Top Rows" = Table.Skip(#"Removed Columns",1),
#"Filled Down" = Table.FillDown(#"Removed Top Rows",{"Column1", "Column3"}),
#"Replaced Value" = Table.ReplaceValue(#"Filled Down"," ","",Replacer.ReplaceText,{"Column5"}),
#"Replaced Value1" = Table.ReplaceValue(#"Replaced Value","Hours","A-SIL Hours",Replacer.ReplaceValue,{"Column6"}),
#"Replaced Value2" = Table.ReplaceValue(#"Replaced Value1","Contacts","A-SIL Contacts",Replacer.ReplaceValue,{"Column7"}),
#"Replaced Value3" = Table.ReplaceValue(#"Replaced Value2","Hours","B-SIL Hours",Replacer.ReplaceValue,{"Column8"}),
#"Replaced Value4" = Table.ReplaceValue(#"Replaced Value3","Contacts","B-SIL Contacts",Replacer.ReplaceValue,{"Column9"}),
#"Removed Columns1" = Table.RemoveColumns(#"Replaced Value4",{"Column10"}),
#"Replaced Value5" = Table.ReplaceValue(#"Removed Columns1","Hours","T-SIL Hours",Replacer.ReplaceValue,{"Column11"}),
#"Replaced Value6" = Table.ReplaceValue(#"Replaced Value5","Contacts","T-SIL Contacts",Replacer.ReplaceValue,{"Column12"}),
#"Replaced Value7" = Table.ReplaceValue(#"Replaced Value6","Hours","S-SIL Hours",Replacer.ReplaceValue,{"Column13"}),
#"Replaced Value8" = Table.ReplaceValue(#"Replaced Value7","Contacts","S-SIL Contacts",Replacer.ReplaceValue,{"Column14"}),
#"Replaced Value9" = Table.ReplaceValue(#"Replaced Value8","Hours","C-SIL Hours",Replacer.ReplaceValue,{"Column15"}),
#"Replaced Value10" = Table.ReplaceValue(#"Replaced Value9","Contacts","C-SIL Contacts",Replacer.ReplaceValue,{"Column16"}),
#"Removed Top Rows1" = Table.Skip(#"Replaced Value10",1),
#"Promoted Headers1" = Table.PromoteHeaders(#"Removed Top Rows1", [PromoteAllScalars=true]),
#"Changed Type1" = Table.TransformColumnTypes(#"Promoted Headers1",{{"Branch Name", type text}, {"Key Staff", type text}, {"StaffType", type text}, {"A-SIL Hours", Int64.Type}, {"A-SIL Contacts", Int64.Type}, {"B-SIL Hours", Int64.Type}, {"B-SIL Contacts", Int64.Type}, {"T-SIL Hours", Int64.Type}, {"T-SIL Contacts", Int64.Type}, {"S-SIL Hours", Int64.Type}, {"S-SIL Contacts", Int64.Type}, {"C-SIL Hours", Int64.Type}, {"C-SIL Contacts", Int64.Type}}),
#"Removed Columns2" = Table.RemoveColumns(#"Changed Type1",{"A-SIL Contacts", "B-SIL Contacts", "T-SIL Contacts", "S-SIL Contacts", "C-SIL Contacts"}),
#"Unpivoted Columns" = Table.UnpivotOtherColumns(#"Removed Columns2", {"Branch Name", "Key Staff", "StaffType"}, "Attribute", "Value"),
#"Renamed Columns" = Table.RenameColumns(#"Unpivoted Columns",{{"Attribute", "Attributes"}, {"Value", "Values"}}),
#"Merged Queries" = Table.NestedJoin(#"Renamed Columns", {"Branch Name", "Key Staff", "StaffType"}, #"Contacts - As Per Sheet 1", {"Branch Name", "Key Staff", "StaffType"}, "Contacts - As Per Sheet 1", JoinKind.LeftOuter),
#"Expanded Contacts - As Per Sheet 1" = Table.ExpandTableColumn(#"Merged Queries", "Contacts - As Per Sheet 1", {"Values"}, {"Values.1"}),
#"Renamed Columns1" = Table.RenameColumns(#"Expanded Contacts - As Per Sheet 1",{{"Values.1", "Contacts"}, {"Values", "Hours"}}),
#"Split Column by Delimiter" = Table.SplitColumn(#"Renamed Columns1", "Attributes", Splitter.SplitTextByDelimiter(" ", QuoteStyle.Csv), {"Attributes.1", "Attributes.2"}),
#"Changed Type2" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Attributes.1", type text}, {"Attributes.2", type text}}),
#"Removed Columns3" = Table.RemoveColumns(#"Changed Type2",{"Attributes.2"}),
#"Renamed Columns2" = Table.RenameColumns(#"Removed Columns3",{{"Attributes.1", "Attributes"}})
in
#"Renamed Columns2"
Now, once the query is loaded and the table is generated. Rename the table from “Query1” to “Raw Data - As Per Sheet 1”. Again, the above query provided is for the transformations performed onto the Worksheet No. 1
Step 4: Finally, un-check the option of “Enable Load” for the table “Contacts - As Per Sheet 1”.
Note:
1. Just under the first line “Source” you’ll have to change the file location i.e. mention the file path where your Excel file is located. Apart from this, everything remains the same.
2. Once you complete the aforementioned steps for Worksheet No. 1. Follow the same steps for Worksheet No. 2.
3. So firstly, you’ll be required to copy the M Code of “Contacts” part and paste it into the Advanced Editor and than you’ll have to copy and paste the M Code for “Raw Data” part.
Hoping this is way easier for you than to go through and perform tedious transformative steps like I’ve performed in my file. And also hoping you’ll find this useful.
Thanks and Warm Regards,
Harsh