To dynamically unpivot multiple columns in Power Query while keeping key columns unchanged, follow these steps:
1. Identify Key and Unpivot Columns
Select Key Columns – These are the columns that should remain unchanged (e.g., Date, Category).
Dynamically Select Columns to Unpivot – Instead of hardcoding, use Table.ColumnNames() to determine which columns need unpivoting dynamically.
2. Use Power Query M Code for Dynamic Unpivoting
let
Source = YourTable,
KeyColumns = {"Date", "Category"}, // Adjust with your key columns
AllColumns = Table.ColumnNames(Source),
UnpivotColumns = List.Difference(AllColumns, KeyColumns),
UnpivotedTable = Table.UnpivotOtherColumns(Source, KeyColumns, "Attribute", "Value")
in
UnpivotedTable
List.Difference(AllColumns, KeyColumns) ensures that only the required columns are unpivoted, even if new columns are added later.
Table.UnpivotOtherColumns() efficiently transforms the dataset without manually selecting columns.
3. Optimize for Performance
Remove Unnecessary Columns before unpivoting to reduce data processing overhead.
Apply Filtering Early in Power Query to minimize the number of rows processed.