let // 1. Load all tables from the current workbook Tables = Excel.CurrentWorkbook(), // 2. Load the dimensions metadata table DimensionsRaw = Tables{[Name="dimensions"]}[Content], // Trim column names to remove any hidden whitespace (defensive) CleanDimNames = Table.TransformColumnNames(DimensionsRaw, each Text.Trim(_)), // Convert to records — now safe because headers are correct DimRecords = Table.ToRecords(CleanDimNames), // Extract: [id] = column names in data, [label] = final label column names DimNames = List.Transform(DimRecords, each [id]), // e.g., "YEAR", "CAT", "AGE" DimLabels = List.Transform(DimRecords, each [label]), // e.g., "any", "Catalunya", "edat quinquennal" // 3. Load the main data table DataRaw = Tables{[Name="data"]}[Content], // Trim column names in data (in case of spaces) CleanDataNames = Table.TransformColumnNames(DataRaw, each Text.Trim(_)), // Type all dimension key columns as text, VALUE and STATUS as appropriate DimTypes = List.Transform(DimNames, each {_, type text}), FixedTypes = { {"VALUE", Number.Type}, {"STATUS", type text} }, AllTypes = DimTypes & FixedTypes, TypedData = Table.TransformColumnTypes(CleanDataNames, AllTypes, "en-US"), // 3.5 Rename key columns: "YEAR" → "YEAR ID", "CAT" → "CAT ID", etc. RenamePairs = List.Transform(DimNames, each {_, _ & " ID"}), RenamedKeyColumns = Table.RenameColumns(TypedData, RenamePairs), // Update DimNames to point to the new column names for joining KeyColumnNames = List.Transform(DimNames, each _ & " ID"), // 4. Function to load a lookup table (e.g., "YEAR", "CAT", etc.) GetLookupTable = (dimRecord as record) => let TableName = dimRecord[id], // e.g., "YEAR" SourceStep = try Tables{[Name=TableName]}[Content] otherwise error "Lookup table not found: " & TableName, // Trim column names in lookup table WithCleanNames = Table.TransformColumnNames(SourceStep, each Text.Trim(_)), // Ensure types Cleaned = Table.TransformColumnTypes(WithCleanNames, { {"id", type text}, {"label", type text} }), Output = Cleaned in Output, // 5. Generate list of lookup tables with metadata LookupList = List.Transform(DimRecords, each [DimRecord = _, LookupTable = GetLookupTable(_)]), // 6. Join each lookup table dynamically Result = List.Accumulate( LookupList, RenamedKeyColumns, // Start with renamed keys (current, item) => let // Use the *original* [id] to find the lookup table OriginalId = item[DimRecord][id], // But join on the *renamed* column: "YEAR ID" KeyColumn = OriginalId & " ID", // Label column remains the [label] value LabelColumn = item[DimRecord][label], Lookup = item[LookupTable], // Join: current[KeyColumn] → lookup[id] Joined = Table.NestedJoin( current, {KeyColumn}, Lookup, {"id"}, "JoinTemp", JoinKind.LeftOuter ), // Expand and rename label → LabelColumn (e.g., "any") Expanded = Table.ExpandTableColumn( Joined, "JoinTemp", {"label"}, {LabelColumn} ) in Expanded ), // 7. Final column order: renamed keys → labels → measures FinalOrder = List.Transform(DimNames, each _ & " ID") & DimLabels & {"VALUE", "STATUS"}, ExistingCols = Table.ColumnNames(Result), SafeOrder = List.Intersect({FinalOrder, ExistingCols}), FinalTable = Table.SelectColumns(Result, SafeOrder) in FinalTable