跳到正文

第 14 课 · Parsing and Transforming

Transpose and Cross Tab

Reshape data in Alteryx with Transpose (wide to long) and Cross Tab (long to wide), choose key fields and aggregations and handle changing column names.

Data comes in two shapes. A wide table has one column per period or category. A long table has one row per observation, with a column saying which period it is. Reporting often needs wide tables; analysis and databases usually prefer long ones.

Alteryx converts between them with Transpose and Cross Tab, in the Transform category.

Wide and long

Wide:

store jan feb
A 10 12
B 7 9

Long:

store month sales
A jan 10
A feb 12
B jan 7
B feb 9

The long form keeps the same information. Adding March adds rows, not a new column, so downstream tools keep working.

Transpose: wide to long

In Transpose you choose:

  • Key fields: the columns that stay as they are and are repeated on each new row, such as store.
  • Data fields: the columns to turn into rows.

The output has your keys plus two columns, typically named Name (the old column name) and Value (the cell). Rename them right after the tool with a Select, for example to month and sales.

Because all the data fields now share one Value column, mixed types are a problem. If you transpose a text column together with numbers, the Value column must be text. Transpose only fields of a compatible type.

Python Stand-in: transpose in Python

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

If your source adds a new month column each time, use the option that includes new (unknown) columns as data fields, so the workflow keeps working. Check that your key fields stay selected correctly.

Cross Tab: long to wide

Cross Tab is the reverse. You choose:

  • Group by: the fields that identify each output row, such as store.
  • Column headers field: its unique values become new columns, such as month.
  • Values field: what fills the cells.
  • Aggregation method: what to do when several values land in the same cell, such as Sum, Count or Concatenate.

The aggregation is important. If you have two rows for store A in January and you choose Sum, they are added without a warning. Check for duplicates first if you expect one value per cell.

SQL Stand-in: cross tab with conditional sums

Press Run (or Ctrl + Enter) to see the result here.

Store B has no February value, so its cell is empty (null). Decide whether that means zero or unknown. A missing sale record is often zero; a missing measurement is usually not.

Column names and order

Cross Tab builds column names from your data values. Two things can go wrong:

  • Spaces and special characters may be changed (often to underscores) in the new column names. Check the actual names before another tool refers to them.
  • New values create new columns, and a missing value means a missing column. A later tool that expects feb may fail in a month where no February data exists.

Column order is usually alphabetical, so apr sorts before jan. Use header values like 01, 02, or 2026-01 for natural ordering.

A robust trick: join your data with a list of all expected categories before the Cross Tab, so every column always exists.

Python Stand-in: force the expected columns

Press Run (or Ctrl + Enter) to see the result here.

Program input · what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Here missing values become zero because we chose that rule on purpose.

When to use which

  • Receiving a spreadsheet with months across the top? Transpose it early.
  • Building the final report layout? Cross Tab near the end.
  • Filtering, grouping or joining? Do it in long form.

The Arrange tool in the same category can also restructure columns in more complex ways; check its help when plain Transpose is not enough.

Check the shape after reshaping

You can predict the row count after a reshape, which makes errors easy to spot.

  • After Transpose, the number of rows is the input rows multiplied by the number of data fields. Two stores and twelve month columns give 24 rows.
  • After Cross Tab, the number of rows is the number of distinct combinations of the group-by fields.

If your count differs, a field was probably placed in the wrong box. For example, if you leave a unique order ID in the Cross Tab grouping, each order stays on its own row and the table is full of empty cells. Group only by the fields that should identify a row in the final layout.

Also compare totals. For numeric data, the sum of the Value column after Transpose should equal the sum of all the original data cells.

A round trip as a test

If you transpose a table and then cross tab it again, you should get back the original values, apart from details such as column order and types. This round trip is a quick way to learn the settings of both tools and to confirm that no rows were combined by accident.

Practice task

Type a wide table of three products and four quarters, with one empty cell. Transpose it and predict the number of rows first. Then rename the Name and Value columns and cross tab the data back.

Compare the final table with the original. Look at the column order, the data types and the empty cell. Next, remove all rows for one quarter before the Cross Tab and see which columns remain. Add a list of expected quarters and join it to your data so the missing column returns, filled according to a rule that you choose and write down.

Common mistakes

Do not assume Cross Tab will keep the same columns every run. Protect later tools with expected categories or dynamic column handling.

  • Transposing a key column along with the data fields.
  • Using Sum or First on duplicates without noticing.
  • Mixing text and numbers in a transposed value column.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. What does Transpose normally do?

    Transpose moves wide columns into Name and Value rows.
  2. 2. In Cross Tab, what happens when two rows fall into the same cell?

    The method such as Sum or Concatenate merges the values.
  3. 3. Why can a later tool fail after Cross Tab in some months?

    Columns come from the data values, so missing values mean missing columns.

Summary

  • Long data is easier to analyze; wide data is often easier to read.
  • Transpose turns columns into Name and Value rows.
  • Cross Tab turns values into columns with an aggregation method.
  • Check duplicates, null cells, column names and missing categories.

Next: The Formula Tool and Expressions

你的学习进度保存在此浏览器中,无需账号。