top of page

Excel Arena

This is the place, where we can test the boundaries of Excel functions.

Power Query Magic: Transform a Complex Cross-Tab Table into a Clean List Using UNPIVOT

32 minutes ago
5 min read

Have you ever come across an Excel table that looks perfectly organized at first glance but becomes a nightmare when you try to transform it?


Multiple groups, departments, scattered values, blank cells, and comma-separated numbers — all packed into a single table!


At first, this might look like a complicated data transformation problem. But what if I told you that the solution becomes surprisingly simple once you identify the underlying pattern?


Today, let's explore an interesting Power Query challenge where we transform a cross-tabular Excel table into a clean, structured list using the power of UNPIVOT, List.TransformMany, List.Split, and List.Alternate.


And the best part? We will explore two different approaches to achieve the same result!



Understanding the Challenge

Imagine we have the following Excel table.

Source Data

G1

HR

Sales

G2

IT

Admin

G3

Finance

Marketing

Purchasing

HR

1, 2, 3


IT

6, 7


Finance

10



Sales


4, 5

Admin


8, 9

Marketing


11, 12








Purchasing



13, 14, 15

Here is what makes this table interesting:

  • G1, G2, and G3 represent three different groups.

  • Each group contains multiple departments.

  • The department names are distributed across different columns.

  • Team numbers are stored as comma-separated values.

  • Blank cells are scattered throughout the table.

Our objective is to transform this data into a normalized three-column structure.

Expected Output

Group

Dept

Team

G1

HR

1

G1

HR

2

G1

HR

3

G1

Sales

4

G1

Sales

5

G2

IT

6

G2

IT

7

G2

Admin

8

G2

Admin

9

G3

Finance

10

G3

Marketing

11

G3

Marketing

12

G3

Purchasing

13

G3

Purchasing

14

G3

Purchasing

15

Now the question is:

How can we achieve this transformation without manually processing each group and department?

Let's discover the trick!


The Secret Ingredient: UNPIVOT

The first thing we need to understand is that Power Query's UNPIVOT operation can reveal patterns that are not immediately visible in the original table.

When we unpivot the source data, the wide structure gets converted into Attribute-Value pairs.

In Power Query, we can use:

Table.UnpivotOtherColumns(Source, {}, "A", "V")

This converts all columns into two columns:

  • A – Contains the original column names.

  • V – Contains the corresponding cell values.


At this stage, the data might not look particularly useful.

However, this is where things get interesting!

Instead of looking at the original table as a collection of departments and groups, start looking at the unpivoted data as a repeating sequence of records.


The key is to identify the relationship between the group name, department name, and corresponding values.

Once we recognize this pattern, we can reconstruct the required output using list operations.


Solution 1 – Using List.TransformMany and List.Split

Our first approach uses a combination of Power Query list functions.


let

Source = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content],

Result = Table.Sort(

Table.FromRows(

List.TransformMany(

List.Split(Table.ToRows(Table.UnpivotOtherColumns(Source, {}, "A", "V")), 2),

each Text.Split(Text.From(_{1}{1}), ", "),

(x, y) => x{0} & {y}

),

{"Group", "Dept", "Team"}

),

each Number.From([Team])

)

in

Result


The important functions are:

List.Split

This function divides a list into smaller lists based on the specified number of elements.

For example:

List.Split({1, 2, 3, 4, 5, 6}, 2)

Result:

{
    {1, 2},
    {3, 4},
    {5, 6}
}

List.TransformMany

This is particularly useful when one input element needs to produce multiple output elements.

For example, if one department contains the values 1, 2, 3, we need to generate three individual records rather than retain a single comma-separated value.

Text.Split

This function breaks the comma-separated text into individual values.

Text.Split("1, 2, 3", ", ")

Result:

{"1", "2", "3"}

By combining these functions, we can split the original values, reconstruct the group and department information, and generate individual team records.

The overall logic is:

  1. Unpivot the source table.

  2. Identify the repeating pattern.

  3. Split the unpivoted rows into the required structure.

  4. Expand comma-separated values into individual records.

  5. Reconstruct the Group, Dept, and Team columns.

  6. Sort the final output by Team number.

This approach demonstrates how multiple list functions can work together to solve a complex-looking transformation.


Solution 2 – Using List.Alternate

Now let's look at another interesting approach.


let

Source = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content],

Result = Table.FromRows(

List.Sort(

List.TransformMany(

List.Split(

List.Alternate(

List.Combine(Table.ToRows(Table.UnpivotOtherColumns(Source, {}, "A", "V"))),

1,

3,

1

),

3

),

each Text.Split(Text.From(List.Last(_)), ", "),

(x, y) => List.FirstN(x, 2) & {Number.From(y)}

),

each List.Last(_)

),

{"Group", "Dept", "Team"}

)

in

Result


Instead of relying only on splitting the unpivoted rows into fixed groups, we can use List.Alternate to identify and separate the repeating pattern.

What does List.Alternate do?

It allows us to select alternating elements from a list based on the number of elements to skip and take.

For example:

List.Alternate(
    {1, 2, 3, 4, 5, 6, 7, 8, 9},
    1,
    3,
    1
)

This function provides a way to isolate elements based on a repeating take-and-skip pattern.

In our transformation, this becomes useful when working with the flattened unpivoted data.


The approach combines:

  • Table.UnpivotOtherColumns to convert the source into Attribute-Value pairs.

  • List.Combine to flatten the resulting rows.

  • List.Alternate to identify the required pattern.

  • List.Split to reconstruct the logical groups.

  • List.TransformMany to expand multiple team values.

  • Number.From to convert the team numbers into numeric values.

The final result is the same structured table containing Group, Dept, and Team.

This approach is particularly interesting because it demonstrates how list manipulation can help solve data-shaping problems that are difficult to handle using conventional table transformations.


Why These Solutions Are Interesting

There are several ways to solve this problem in Power Query.

We could manually extract each group, process the departments separately, split the comma-separated values, and append everything together.

But that would make the solution lengthy and difficult to maintain.

The real beauty of these approaches is that they focus on the underlying data pattern rather than processing each department individually.


A few important lessons emerge:

1. UNPIVOT is more powerful than you think.

It is not just a way to convert columns into rows. It can also expose relationships hidden in complex cross-tab structures.

2. Think in terms of lists.

Power Query's list functions provide powerful ways to manipulate and reconstruct data.

3. Look for repeating patterns.

Many complicated data transformations become easier once the underlying sequence is identified.

4. Don't always think in terms of individual columns.

Sometimes, flattening a table into a list and reconstructing it is a much more interesting way to approach the problem.


Final Thoughts

This is a great example of how a seemingly complicated Excel data structure can be transformed into a clean, normalized dataset using Power Query.


The takeaway is simple:

Don't just look at the data. Look for the pattern hidden inside the data!

Once you identify that pattern, functions like UNPIVOT, List.Split, List.TransformMany, and List.Alternate can become incredibly powerful tools in your Power Query toolbox.

Sometimes, the most intimidating problems have surprisingly elegant solutions.


Happy Querying!


Have you ever used List.Alternate or List.TransformMany in a real-world Power Query transformation?

Share your experience in the comments!

Recent Posts

See All

Comments


  • LinkedIn
  • Facebook
  • Twitter
  • Instagram
bottom of page