let
Source = Folder.Files("D:\Shortie Documents\Household\Bank"),
FilterPDFOnly = Table.SelectRows(Source, each ([Extension] = ".pdf")),
FilteredHiddenFiles = Table.SelectRows(FilterPDFOnly, each [Attributes]?[Hidden]? <> true),
InvokeCustomFunction = Table.AddColumn(FilteredHiddenFiles, "Transform File", each #"Transform File"([Content])),
#"Renamed ColumnsName" = Table.RenameColumns(InvokeCustomFunction, {"Name", "Source.Name"}),
RemovedColumns = Table.SelectColumns(#"Renamed ColumnsName", {"Source.Name", "Transform File"}),
ExpandedTableColumn = Table.ExpandTableColumn(RemovedColumns, "Transform File", Table.ColumnNames(#"Transform File"(#"Sample File"))),
FilteredChoosePDFTables = Table.SelectRows(ExpandedTableColumn, each ([Id] = "Table002" or [Id] = "Table004" or [Id] = "Table006" or [Id] = "Table008")),
ExpandedData = Table.ExpandTableColumn(FilteredChoosePDFTables, "Data", {"Column1", "Column2", "Column3", "Column4", "Column5", "Column6"}, {"Data.Column1", "Data.Column2", "Data.Column3", "Data.Column4", "Data.Column5", "Data.Column6"}),
RemovedColumns1 = Table.RemoveColumns(ExpandedData,{"Source.Name", "Id", "Name", "Kind"}),
PromotedHeaders = Table.PromoteHeaders(RemovedColumns1, [PromoteAllScalars=true]),
RemoveExtraneousRows = Table.SelectRows(PromotedHeaders, each ([Type] <> "BROUGHT FORWARD") and ([Date] <> "Date")),
ConvertBlanksToNull = Table.ReplaceValue(RemoveExtraneousRows,"",null,Replacer.ReplaceValue,{"Date"}),
FilledDownDate = Table.FillDown(ConvertBlanksToNull,{"Date"}),
RemoveNullDate = Table.SelectRows(FilledDownDate, each ([Date] <> null)),
CleanText = Table.TransformColumns(RemoveNullDate,{{"Type", Text.Clean, type text}}),
CleanType = Table.ReplaceValue(CleanText,"DEBIT CARDTRANSACTION","DEBIT CARD TRANSACTION",Replacer.ReplaceText,{"Type"}),
AddIndex = Table.AddIndexColumn(CleanType, "Index", 1, 1, Int64.Type),
SplitRowId = Table.AddColumn(AddIndex, "SplitRow", each if [Balance] = null then 2 else 1) //Two means it's the second line of the split row
,
MrgRequired = Table.ExpandListColumn(Table.AddColumn(SplitRowId, "MergeRowReq", each List.Range(SplitRowId[SplitRow],[Index],1)), "MergeRowReq") //Move the splitRowId up 1 line.
,
NextRowType = Table.ExpandListColumn(Table.AddColumn(MrgRequired, "NextRowType", each List.Range(MrgRequired[Type],[Index],1)), "NextRowType"),
TypeFinal = Table.AddColumn(NextRowType, "TypeFinal", each if [MergeRowReq] = 1 then [Type] else ([Type] & " " & (if [NextRowType] = null then "" else [NextRowType]))) //Finally concatenate the two cells.
,
NextRowDesc = Table.ExpandListColumn(Table.AddColumn(TypeFinal, "NextRowDesc", each List.Range(MrgRequired[Description],[Index],1)), "NextRowDesc"),
DescFinal = Table.AddColumn(NextRowDesc, "DescFinal", each if [MergeRowReq] = 1 then [Description] else [Description] & " " & [NextRowDesc]),
RemoveNullRows = Table.SelectRows(DescFinal, each ([Balance] <> null)),
#"Removed Columns" = Table.RemoveColumns(RemoveNullRows,{"Type", "Description", "Index", "SplitRow", "MergeRowReq", "NextRowType", "NextRowDesc"}),
ReorderedColumns = Table.ReorderColumns(#"Removed Columns",{"Date", "TypeFinal", "DescFinal", "Paid in", "Paid out", "Balance"}),
RenamedColumns = Table.RenameColumns(ReorderedColumns,{{"TypeFinal", "Type"}, {"DescFinal", "Description"}}),
ChangedType = Table.TransformColumnTypes(RenamedColumns,{{"Paid out", Currency.Type}, {"Balance", Currency.Type}})
in
ChangedType