let
folderPath = "C:\LeDossier",
folderFiles = Folder.Files(folderPath),
filterXlsx = Table.SelectRows(folderFiles, each ([Extension] = ".xlsx")),
selectFilesColumns = Table.SelectColumns(filterXlsx,{"Name", "Content"}),
binariesToExcel = Table.TransformColumns(selectFilesColumns,{{"Content", Excel.Workbook, type table}}),
expandExcelData = Table.ExpandTableColumn(binariesToExcel, "Content", {"Name", "Data", "Item", "Kind", "Hidden"}, {"SheetName", "Data", "Item", "Kind", "Hidden"}),
filterSheets = Table.SelectRows(expandExcelData, each Text.StartsWith([SheetName], "U") and [Kind] = "Sheet"),
fnTransformDataSheet = (rawDataSheet as table) as table =>
let
rawColsName = Table.ColumnNames(rawDataSheet),
newColsName =
let
rawHeaderRows = Table.SelectRows(rawDataSheet, each ([Column1] = null)),
mergeRows = List.Transform(rawColsName, each let newHeader = Text.Combine(List.RemoveNulls(List.Transform(Table.Column(rawHeaderRows, _), Text.Trim)), " ") in if newHeader = "" then _ else newHeader),
changeFirstAndLast = {"Utilisateur"} & List.RemoveFirstN(List.RemoveLastN(mergeRows, 1), 1) & {"EstVerifie"}
in
changeFirstAndLast,
dataRows = Table.SelectRows(rawDataSheet, each ([Column1] <> null)),
renameColumns = Table.RenameColumns(dataRows,List.Zip({rawColsName, newColsName})),
colsWithData = List.RemoveNulls(List.Transform(newColsName, each if List.Count(List.RemoveNulls(Table.Column(renameColumns, _))) = 0 then null else _)),
removeEmptyColumns = Table.SelectColumns(renameColumns, colsWithData),
unpivotColumns = Table.UnpivotOtherColumns(removeEmptyColumns, {"Utilisateur", "EstVerifie"}, "Attribut", "Valeur")
in
unpivotColumns,
transformSheets = Table.TransformColumns(filterSheets, {"Data", fnTransformDataSheet, type table}),
selectSheetsColumns = Table.SelectColumns(transformSheets,{"Name", "SheetName", "Data"}),
expandSheetsData = Table.ExpandTableColumn(selectSheetsColumns, "Data", {"Utilisateur", "EstVerifie", "Attribut", "Valeur"}, {"Utilisateur", "EstVerifie", "Attribut", "Valeur"})
in
expandSheetsData