Hi,
I am very new to power query and I am trying to get the records that are all in column and unpivot them. I was able to do it but the query takes a long time to run. Could you take a look at the attached file and help me out?
Thanks,
I am very new to power query and I am trying to get the records that are all in column and unpivot them. I was able to do it but the query takes a long time to run. Could you take a look at the attached file and help me out?
Power Query:
let
Source = Excel.CurrentWorkbook(){[Name="Sweep"]}[Content],
#"Added Custom" = Table.AddColumn(Source, "Custom", each if Text.StartsWith([COL1], "Record") then 1 else null),
#"Replaced Errors" = Table.ReplaceErrorValues(#"Added Custom", {{"Custom", null}}),
#"Added Index" = Table.AddIndexColumn(#"Replaced Errors", "Index", 1, 1, Int64.Type),
#"Added Custom1" = Table.AddColumn(#"Added Index", "Custom.1", each List.Sum(List.FirstN(#"Added Index"[Custom],[Index]))),
#"Grouped Rows" = Table.Group(#"Added Custom1", {"Custom.1"}, {{"All", each _, type table [COL1=any, Custom=nullable number, Index=number, Custom.1=number]}}),
#"Added Custom2" = Table.AddColumn(#"Grouped Rows", "Custom", each Table.AddIndexColumn([All],"ColIden",1)),
#"Expanded Custom" = Table.ExpandTableColumn(#"Added Custom2", "Custom", {"COL1", "ColIden"}, {"Custom.COL1", "Custom.ColIden"}),
#"Removed Columns" = Table.RemoveColumns(#"Expanded Custom",{"All"}),
#"Pivoted Column" = Table.Pivot(Table.TransformColumnTypes(#"Removed Columns", {{"Custom.ColIden", type text}}, "en-US"), List.Distinct(Table.TransformColumnTypes(#"Removed Columns", {{"Custom.ColIden", type text}}, "en-US")[Custom.ColIden]), "Custom.ColIden", "Custom.COL1"),
#"Removed Columns1" = Table.RemoveColumns(#"Pivoted Column",{"4", "Custom.1"}),
#"Renamed Columns" = Table.RenameColumns(#"Removed Columns1",{{"1", "Record Number"}}),
#"Removed Other Columns" = Table.SelectColumns(#"Renamed Columns",{"Record Number", "2", "3", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31"}),
#"Replaced Value" = Table.ReplaceValue(#"Removed Other Columns",":","",Replacer.ReplaceText,{"Record Number"}),
#"Changed Type" = Table.TransformColumnTypes(#"Replaced Value",{{"2", type date}, {"3", type time}}),
#"Added Custom3" = Table.AddColumn(#"Changed Type", "Date", each [2]&[3]),
#"Reordered Columns" = Table.ReorderColumns(#"Added Custom3",{"Record Number", "Date", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "2", "3"}),
#"Renamed Columns1" = Table.RenameColumns(#"Reordered Columns",{{"5", "Station"}, {"6", "Registration #"}, {"7", "Ship #"}, {"9", "Measure Applied to Flight"}}),
#"Reordered Columns1" = Table.ReorderColumns(#"Renamed Columns1",{"Record Number", "Date", "Station", "Registration #", "Ship #", "8", "Measure Applied to Flight", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "2", "3", "10"}),
#"Renamed Columns2" = Table.RenameColumns(#"Reordered Columns1",{{"11", "Outbound Flight"}, {"12", "Origin"}, {"13", "Destination"}, {"16", "Completed Date"}}),
#"Split Column by Character Transition" = Table.SplitColumn(#"Renamed Columns2", "14", Splitter.SplitTextByCharacterTransition((c) => not List.Contains({"0".."9"}, c), {"0".."9"}), {"14.1", "14.2", "14.3"}),
#"Renamed Columns3" = Table.RenameColumns(#"Split Column by Character Transition",{{"14.1", "Search Location"}}),
#"Added Custom4" = Table.AddColumn(#"Renamed Columns3", "Time", each [14.2]&[14.3]),
#"Reordered Columns2" = Table.ReorderColumns(#"Added Custom4",{"Record Number", "Date", "Station", "Registration #", "Ship #", "8", "Measure Applied to Flight", "Outbound Flight", "Origin", "Destination", "Search Location", "14.2", "14.3", "15", "Completed Date", "Time", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "2", "3", "10"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Reordered Columns2",{{"Completed Date", type date}, {"Time", type time}}),
#"Added Custom5" = Table.AddColumn(#"Changed Type1", "Interior Completion Date", each [Completed Date]&[Time]),
#"Reordered Columns3" = Table.ReorderColumns(#"Added Custom5",{"Record Number", "Date", "Station", "Registration #", "Ship #", "8", "Measure Applied to Flight", "Outbound Flight", "Origin", "Destination", "Search Location", "Interior Completion Date", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "14.2", "14.3", "15", "Completed Date", "Time", "17", "30", "31", "2", "3", "10", "18"}),
#"Renamed Columns4" = Table.RenameColumns(#"Reordered Columns3",{{"19", "Search Agents"}}),
#"Changed Type2" = Table.TransformColumnTypes(#"Renamed Columns4",{{"22", type date}}),
#"Added Custom6" = Table.AddColumn(#"Changed Type2", "Ext Time", each if [20] <> null then Text.End([20], 5) else null),
#"Reordered Columns4" = Table.ReorderColumns(#"Added Custom6",{"Record Number", "Date", "Station", "Registration #", "Ship #", "8", "Measure Applied to Flight", "Outbound Flight", "Origin", "Destination", "Search Location", "Interior Completion Date", "Search Agents", "20", "21", "22", "Ext Time", "23", "24", "25", "26", "27", "28", "29", "14.2", "14.3", "15", "Completed Date", "Time", "17", "30", "31", "2", "3", "10", "18"}),
#"Renamed Columns5" = Table.RenameColumns(#"Reordered Columns4",{{"20", "Exterior Search"}}),
#"Split Column by Character Transition1" = Table.SplitColumn(#"Renamed Columns5", "Exterior Search", Splitter.SplitTextByCharacterTransition((c) => not List.Contains({"0".."9"}, c), {"0".."9"}), {"Exterior Search.1", "Exterior Search.2", "Exterior Search.3"}),
#"Renamed Columns6" = Table.RenameColumns(#"Split Column by Character Transition1",{{"Search Agents", "Interior Search Agents"}}),
#"Changed Type3" = Table.TransformColumnTypes(#"Renamed Columns6",{{"Ext Time", type time}}),
#"Added Custom7" = Table.AddColumn(#"Changed Type3", "Exterior Completion Date", each if [22] <> null and [Ext Time] <> null then [22] & [Ext Time] else null),
#"Reordered Columns5" = Table.ReorderColumns(#"Added Custom7",{"Record Number", "Date", "Station", "Registration #", "Ship #", "8", "Measure Applied to Flight", "Outbound Flight", "Origin", "Destination", "Search Location", "Interior Completion Date", "Interior Search Agents", "Exterior Search.1", "Exterior Search.2", "Exterior Search.3", "21", "22", "Ext Time", "Exterior Completion Date", "23", "24", "25", "26", "27", "28", "29", "14.2", "14.3", "15", "Completed Date", "Time", "17", "30", "31", "2", "3", "10", "18"}),
#"Renamed Columns7" = Table.RenameColumns(#"Reordered Columns5",{{"Exterior Search.1", "Exterior Search"}}),
#"Reordered Columns6" = Table.ReorderColumns(#"Renamed Columns7",{"Record Number", "Date", "Station", "Registration #", "Ship #", "8", "Measure Applied to Flight", "Outbound Flight", "Origin", "Destination", "Search Location", "Interior Completion Date", "Interior Search Agents", "Exterior Search", "Exterior Completion Date", "23", "24", "25", "26", "27", "28", "29", "14.2", "14.3", "15", "Completed Date", "Time", "17", "30", "31", "2", "3", "10", "18", "Exterior Search.2", "Exterior Search.3", "21", "22", "Ext Time"}),
#"Renamed Columns8" = Table.RenameColumns(#"Reordered Columns6",{{"25", "Ext Search Agents"}}),
#"Reordered Columns7" = Table.ReorderColumns(#"Renamed Columns8",{"Record Number", "Date", "Station", "Registration #", "Ship #", "8", "Measure Applied to Flight", "Outbound Flight", "Origin", "Destination", "Search Location", "Interior Completion Date", "Interior Search Agents", "Exterior Search", "Exterior Completion Date", "Ext Search Agents", "26", "27", "28", "29", "14.2", "14.3", "15", "Completed Date", "Time", "17", "30", "31", "2", "3", "10", "18", "Exterior Search.2", "Exterior Search.3", "21", "22", "Ext Time", "23", "24"}),
#"Renamed Columns9" = Table.RenameColumns(#"Reordered Columns7",{{"26", "Aircraft Baggage / Cargo Bins"}, {"28", "Bag Date"}}),
#"Split Column by Position" = Table.SplitColumn(#"Renamed Columns9", "Aircraft Baggage / Cargo Bins", Splitter.SplitTextByPositions({0, 5}, true), {"Aircraft Baggage / Cargo Bins.1", "Aircraft Baggage / Cargo Bins.2"}),
#"Changed Type4" = Table.TransformColumnTypes(#"Split Column by Position",{{"Search Location", type text}, {"Exterior Search", type text}, {"Aircraft Baggage / Cargo Bins.1", type text}, {"Aircraft Baggage / Cargo Bins.2", type time}, {"14.2", type text}, {"14.3", Int64.Type}, {"Exterior Search.2", type text}, {"Exterior Search.3", Int64.Type}}),
#"Renamed Columns10" = Table.RenameColumns(#"Changed Type4",{{"Aircraft Baggage / Cargo Bins.1", "Aircraft Baggage / Cargo Bins"}, {"Aircraft Baggage / Cargo Bins.2", "Aircraft Baggage Time"}}),
#"Removed Columns2" = Table.RemoveColumns(#"Renamed Columns10",{"27"}),
#"Changed Type5" = Table.TransformColumnTypes(#"Removed Columns2",{{"Bag Date", type date}}),
#"Added Custom8" = Table.AddColumn(#"Changed Type5", "Baggage Bins Completion Date", each if [Bag Date] <> null and [Aircraft Baggage Time] <> null then [Bag Date] & [Aircraft Baggage Time] else null),
#"Reordered Columns8" = Table.ReorderColumns(#"Added Custom8",{"Record Number", "Date", "Station", "Registration #", "Ship #", "8", "Measure Applied to Flight", "Outbound Flight", "Origin", "Destination", "Search Location", "Interior Completion Date", "Interior Search Agents", "Exterior Search", "Exterior Completion Date", "Ext Search Agents", "Aircraft Baggage / Cargo Bins", "Baggage Bins Completion Date", "15", "Completed Date", "Time", "17", "30", "31", "2", "3", "10", "18", "Exterior Search.2", "Exterior Search.3", "21", "22", "Ext Time", "23", "24", "Aircraft Baggage Time", "Bag Date", "29", "14.2", "14.3"}),
#"Renamed Columns11" = Table.RenameColumns(#"Reordered Columns8",{{"31", "Cargo Search Agents"}}),
#"Removed Columns3" = Table.RemoveColumns(#"Renamed Columns11",{"15", "Completed Date", "Time", "17", "30", "2", "3", "10", "18", "Exterior Search.2", "Exterior Search.3", "21", "22", "Ext Time", "23", "24", "Aircraft Baggage Time", "Bag Date", "29", "14.2", "14.3"}),
#"Split Column by Position1" = Table.SplitColumn(#"Removed Columns3", "Station", Splitter.SplitTextByPositions({0, 3}, true), {"Station.1", "Station.2"}),
#"Changed Type6" = Table.TransformColumnTypes(#"Split Column by Position1",{{"Station.1", type text}, {"Station.2", type text}}),
#"Removed Columns4" = Table.RemoveColumns(#"Changed Type6",{"Station.1"}),
#"Renamed Columns12" = Table.RenameColumns(#"Removed Columns4",{{"Station.2", "Station"}}),
#"Changed Type7" = Table.TransformColumnTypes(#"Renamed Columns12",{{"Date", type datetime}}),
#"Extracted Text After Delimiter" = Table.TransformColumns(#"Changed Type7", {{"Registration #", each Text.AfterDelimiter(_, "#:"), type text}}),
#"Extracted Text After Delimiter1" = Table.TransformColumns(#"Extracted Text After Delimiter", {{"Ship #", each Text.AfterDelimiter(_, "#:"), type text}}),
#"Changed Type8" = Table.TransformColumnTypes(#"Extracted Text After Delimiter1",{{"Ship #", Int64.Type}}),
#"Removed Columns5" = Table.RemoveColumns(#"Changed Type8",{"8"}),
#"Extracted Text After Delimiter2" = Table.TransformColumns(#"Removed Columns5", {{"Measure Applied to Flight", each Text.AfterDelimiter(_, ":"), type text}, {"Outbound Flight", each Text.AfterDelimiter(_, ":"), type text}, {"Origin", each Text.AfterDelimiter(_, ":"), type text}, {"Destination", each Text.AfterDelimiter(_, ":"), type text}}),
#"Changed Type9" = Table.TransformColumnTypes(#"Extracted Text After Delimiter2",{{"Interior Completion Date", type datetime}, {"Exterior Completion Date", type datetime}, {"Baggage Bins Completion Date", type datetime}, {"Ship #", type text}, {"Outbound Flight", Int64.Type}})
in
#"Changed Type9"
Thanks,