Forum Discussion
power query - transform sample file issue
The below is the code for the transform sample file.
Note that the structure of the 'scenario' column is that i want the first item in that column. So i add an index column and then say that i want to only keep the item in the scenario column where the index colunm is 1 (Probably a better way to do that).
let
Source = Csv.Document(#"Sample File Parameter1",[Delimiter=",", Columns=50, Encoding=1252, QuoteStyle=QuoteStyle.None]),
#"Removed Columns" = Table.RemoveColumns(Source,{"Column3", "Column5", "Column6", "Column7", "Column8", "Column9"}),
#"Added Index" = Table.AddIndexColumn(#"Removed Columns", "Index", 1, 1),
#"Replaced Value1" = Table.ReplaceValue(#"Added Index",each if [Index] <> 1 then [Column4] else null, null, Replacer.ReplaceValue,{"Column4"}),
#"Removed Columns1" = Table.RemoveColumns(#"Replaced Value1",{"Index"}),
#"Filled Down" = Table.FillDown(#"Removed Columns1",{"Column4"}),
#"Replaced Value3" = Table.ReplaceValue(#"Filled Down","",null,Replacer.ReplaceValue,{"Column1"}),
#"Filled Down1" = Table.FillDown(#"Replaced Value3",{"Column1"}),
#"Replaced Value4" = Table.ReplaceValue(#"Filled Down1","",null,Replacer.ReplaceValue,{"Column2"}),
#"Filtered Rows" = Table.SelectRows(#"Replaced Value4", each ([Column2] <> null and [Column2] <> "Period counter (cumulative)" and [Column2] <> "Period counter (in year)" and [Column2] <> "Period type" and [Column2] <> "Summary period counter")),
#"Promoted Headers" = Table.PromoteHeaders(#"Filtered Rows", [PromoteAllScalars=true]),
#"Promoted Headers1" = Table.PromoteHeaders(#"Promoted Headers", [PromoteAllScalars=true]),
#"Renamed Columns1" = Table.RenameColumns(#"Promoted Headers1",{Table.ColumnNames(#"Promoted Headers1"){2}, "Scenario"}),
#"Renamed Columns2" = Table.RenameColumns(#"Renamed Columns1",{{Table.ColumnNames(#"Renamed Columns1"){0}, "Line Item Type"}}),
#"Unpivoted Other Columns" = Table.UnpivotOtherColumns(#"Renamed Columns2", {"Line Item Type", "Period end date", "Scenario"}, "Attribute", "Value"),
#"Renamed Columns" = Table.RenameColumns(#"Unpivoted Other Columns",{{Table.ColumnNames(#"Unpivoted Other Columns"){0}, "Line Item Type"},{Table.ColumnNames(#"Unpivoted Other Columns"){1}, "Line Item"},{Table.ColumnNames(#"Unpivoted Other Columns"){2}, "Scenario"},{Table.ColumnNames(#"Unpivoted Other Columns"){3}, "Date"}}),
#"Changed Type" = Table.TransformColumnTypes(#"Renamed Columns",{{"Date", Int64.Type}}),
#"Removed Errors" = Table.RemoveRowsWithErrors(#"Changed Type", {"Date"}),
#"Changed Type1" = Table.TransformColumnTypes(#"Removed Errors",{{"Value", type number}}),
#"Removed Errors1" = Table.RemoveRowsWithErrors(#"Changed Type1", {"Value"})
in
#"Removed Errors1"
The below is the code to aggregate and perform the transform sample file:
let
Source = Folder.Files(CSVLocation),
#"Filtered Out Sub folders" = Table.SelectRows(Source, each ([Folder Path] = CSVLocationB)),
#"Filtered for CSVs only" = Table.SelectRows(#"Filtered Out Sub folders", each ([Extension] = ".csv")),
#"Removed Other Columns" = Table.SelectColumns(#"Filtered for CSVs only",{"Content", "Name", "Date accessed", "Date modified", "Date created", "Attributes"}),
#"Filtered Hidden Files Out" = Table.SelectRows(#"Removed Other Columns", each [Attributes]?[Hidden]? <> true),
#"Invoke Custom Function" = Table.AddColumn(#"Filtered Hidden Files Out", "Transform File", each #"Transform File"([Content])),