Mastering Excel Power Query Pivot & Unpivot: M Language & Copilot 2026
Ever stared at a spreadsheet where crucial data points are scattered across a dozen columns, or, conversely, buried in a single, unmanageable column? For data analysts and reporting professionals, this 'wrong shape' is a common, frustrating reality. Traditional Excel methods often buckle under the weight of such challenges, but that's where excel power query shines.
Today, we're diving deep into two of Power Query's most transformative features: Pivot and Unpivot. These aren't just obscure functions; they are essential tools for reshaping messy, multi-source datasets into clean, analytical-ready formats. We'll explore practical power query m language examples and even see how Microsoft Copilot can streamline your entire data transformation workflow, making your Excel ETL processes more efficient and intuitive than ever before.
The Data Reshaping Challenge: Why Pivot and Unpivot are Essential
Before you can truly analyze data, it needs to be in the right structure. Often, raw data arrives in formats optimized for input, not for insight. This leads to two common scenarios: 'wide' tables, where distinct attributes are spread across numerous columns, and 'tall' tables, where multiple values for a single attribute are stacked vertically.
Imagine a sales report where each month has its own column (January Sales, February Sales, etc.). This is 'wide' data. If you want to analyze sales trends over time, you'd prefer a 'tall' format: a single 'Month' column and a single 'Sales Value' column. Conversely, survey responses might list each question as a column, with answers for different attributes (e.g., 'Q1_Rating', 'Q1_Comment'). Transforming these raw layouts is a critical step in any robust data transformation pipeline.
The Core Problem: Data Not Optimized for Analysis
Messy data isn't just an aesthetic issue; it's an analytical roadblock. Incorrect data structures make it difficult to:
Apply aggregate functions (SUM, AVERAGE) across related categories.
Create dynamic charts and pivot tables.
Join or merge data from different sources effectively.
Build scalable reports that adapt to new data periods or categories.
Without proper reshaping, your analytical efforts become manual, error-prone, and time-consuming. This is precisely why Power Query's Pivot and Unpivot functions are indispensable for effective ETL.
Mastering Power Query Pivot and Unpivot Examples
Let's walk through concrete scenarios for both pivoting and unpivoting data within the Power Query Editor.
1. Unpivoting Data: From Wide to Tall for Analysis
Unpivoting is the process of transforming columns into rows. This is incredibly useful when you have multiple columns representing the same type of data (e.g., monthly sales figures, product categories, or survey responses) that you want to consolidate into a single attribute column and a single value column. This is often how to clean messy data in excel power query when columns represent categories.
Worked Example: Unpivoting Sales Data
Suppose you have sales data structured like this, where each month is a separate column:
RegionProductJan-2026Feb-2026Mar-2026NorthA120150130SouthB90110100
To analyze monthly trends, you need a 'Month' column and a 'Sales Value' column.
Load Data: Import your data into Power Query (Data > From Table/Range).
Select Columns to Unpivot: In the Query Editor, select the 'Jan-2026', 'Feb-2026', and 'Mar-2026' columns.
Perform Unpivot: Go to the 'Transform' tab, click 'Unpivot Columns'. You can choose 'Unpivot Columns' (if you selected only the columns to unpivot) or 'Unpivot Other Columns' (if you selected the identifier columns, like 'Region' and 'Product').
Rename Columns: The new columns will be named 'Attribute' and 'Value'. Rename them to 'Month' and 'Sales Value' respectively.
Adjust Data Types: Ensure the 'Sales Value' column is set to 'Whole Number' or 'Decimal Number' and 'Month' to 'Date' (after splitting/transforming the text).
Your data will now look like this:
RegionProductMonthSales ValueNorthAJan-2026120NorthAFeb-2026150............
This 'tall' format is perfect for charting trends, grouping, and advanced analysis.
2. Pivoting Data: From Tall to Wide for Summaries
Pivoting is the inverse of unpivoting. It transforms unique values from a single column into new columns, often with an aggregation applied to another column. This is useful for creating summary tables or cross-tabulations.
Worked Example: Pivoting Regional Sales Data
Using the unpivoted data from above, let's say you want to see total sales by region, with each product as a separate column.
Identify Pivot Column: In the Query Editor, select the 'Product' column. This column contains the values that will become your new column headers.
Perform Pivot: Go to the 'Transform' tab, click 'Pivot Column'.
Choose Value Column and Aggregation: In the 'Pivot Column' dialog, select 'Sales Value' as the 'Values Column'. For 'Aggregate Value Function', choose 'Sum' (or another aggregation like Average, Max, Min).
Your data might now look like this:
RegionMonthABNorthJan-2026120NorthFeb-2026150SouthJan-202690SouthFeb-2026110
Notice the blank cells for products not sold in a region/month combination. You might further modify this in the Power Query Editor to replace nulls with zeros, or refine your pivot logic.
Elevating Transformations with Power Query M Language Examples
While the visual Query Editor is powerful, understanding the underlying Power Query M language unlocks unparalleled flexibility and control. Every action you perform in the UI generates M code. You can view this code by going to the 'Home' tab and clicking 'Advanced Editor'.
Inspecting M Code for Pivot/Unpivot
When you unpivot, Power Query generates a Table.UnpivotOtherColumns or Table.Unpivot function. For our sales example, an unpivot step might look like this:
= Table.UnpivotOtherColumns(#"Changed Type", {"Region", "Product"}, "Month", "Sales Value")
Here, #"Changed Type" refers to the previous step, {"Region", "Product"} are the identifier columns (those you *don't* want to unpivot), and "Month" and "Sales Value" are the names for the new attribute and value columns.
For pivoting, you'll see a Table.Pivot function:
= Table.Pivot(#"Renamed Columns", List.Distinct(#"Renamed Columns"[Product]), "Product", "Sales Value", List.Sum)
This example takes the previous step, identifies distinct values in the 'Product' column to create new columns, uses 'Product' as the pivot column, 'Sales Value' as the values, and applies List.Sum for aggregation. These power query m language examples are the building blocks of advanced data manipulation.
Customizing M for Advanced Scenarios
Directly modifying M code allows for dynamic pivoting (where column names aren't hardcoded) or handling specific data nuances. For instance, to ensure all possible months appear after an unpivot, even if a product had no sales, you might manipulate the list of column names dynamically before the unpivot operation. This level of customization ensures robust and adaptable excel power query solutions.
Microsoft Copilot: Your AI Co-Pilot for Excel ETL
As of July 2026, Microsoft Copilot integrates seamlessly across Microsoft 365, including Excel. For data analysts working with Power Query, Copilot can be a game-changer, especially when dealing with complex M code or trying to understand an unfamiliar data source.
Streamlining Power Query Steps with AI
Imagine you're in the Power Query Editor and need to perform a series of transformations, but you're unsure of the exact sequence or the M syntax. You can ask Copilot directly:
"Copilot, write Power Query M code to unpivot all columns except 'ID' and 'Date', and name the new columns 'Category' and 'Metric'."
Copilot can generate the M code for you, which you can then paste into the Advanced Editor. This dramatically reduces the learning curve for M and speeds up development, making power query copilot a powerful ally.
Troubleshooting and Optimization with Copilot
Encountering an error in a complex query? Copy the M code and ask Copilot:
"Copilot, analyze this Power Query M code and tell me why it's returning an 'Expression.Error: The column 'Value' of the table wasn't found' error."
Copilot can often pinpoint issues like misspelled column names, incorrect data types, or logical flaws in your query. It can also suggest ways to optimize your query for better performance, helping you to refine your excel etl with power query and copilot processes.
Best Practices for Robust Data Transformation
To ensure your excel power query solutions are reliable and performant, consider these best practices.
Ensuring Data Integrity: Data Types and Error Handling
Always define explicit data types for your columns as early as possible in your query. This prevents errors during calculations and ensures data consistency. Power Query's 'Change Type' step is crucial here. For potential errors (e.g., text in a numeric column), use 'Replace Errors' or conditional columns to handle them gracefully, rather than letting the query fail.
Optimizing Your Power Queries for Performance
Large datasets can slow down Power Query. Here are some tips:
Query Folding: Where possible, Power Query pushes transformation steps back to the data source (like SQL Server). This is highly efficient as the source system does the heavy lifting. Monitor your query steps in the Query Editor for the 'View Native Query' option to confirm folding.
Remove Unnecessary Columns Early: Don't load columns you don't need. Remove them in the early stages of your query to reduce memory usage.
Use Parameters: For dynamic filtering or connecting to different data sources, use parameters. This makes your queries reusable and more flexible.
Combine Operations: Look for opportunities to combine multiple simple steps into one more complex M function for efficiency.
Remember that robust ETL isn't just about getting the data transformed; it's about doing it reliably and efficiently. Understanding these best practices, combined with the power of M and Copilot, will set you apart.
FeaturePurposeCommon Use CasePivotTransforms unique values from a column into new columns, often with aggregation.Summarizing monthly sales by product, creating cross-tabulations.UnpivotTransforms multiple columns into two columns: one for attribute names, one for their values.Normalizing survey data, consolidating monthly budget columns into a single 'Month' column.
Mastering Pivot and Unpivot in excel power query is more than just learning a feature; it's about adopting a powerful paradigm for data preparation. By understanding the underlying Power Query M language and leveraging the intelligent assistance of Microsoft Copilot, you can transform complex data challenges into streamlined, automated workflows. This expertise is critical for any data professional aiming to produce accurate, timely, and impactful reports.
Ready to elevate your data transformation skills and become a true expert in data analysis and reporting? Enroll in our "Advanced Excel + Power Query + Microsoft Copilot" course today to unlock the full potential of these powerful tools and revolutionize your approach to messy datasets.
Originally published at Excel Logics Blog











