Skip to content

Lesson 12 ยท Joining and Aggregating

GroupBy, Pivot, and Unpivot in KNIME

Summarize data with the KNIME GroupBy node, build cross tables with Pivot, turn wide tables long with Unpivot, and check that your totals still add up.

Managers rarely want 50,000 order lines. They want revenue by region, orders per month, or average basket size per channel. Aggregation turns detail into answers, and pivoting reshapes those answers for reading. This lesson covers both.

GroupBy

The GroupBy node has two decisions:

  1. Group columns: which columns define a group, such as region and month.
  2. Aggregations: what to calculate per group, such as the sum of amount or the count of order_id.

The output has one row per unique combination of group values. Pick an aggregation that matches your question:

Question Aggregation
How much? Sum
How many rows? Count
How many different customers? Unique count
Typical value? Median or mean
Range? Minimum and maximum

Two traps are common. First, counting rows is not counting customers, since one customer may have many orders. Second, the mean is pulled by extreme values, while the median is not. Also check how your chosen aggregation treats missing values, and read the output column names: they usually show both the method and the source column, and you may want to rename them.

SQL Stand-in: GroupBy with sum, count, and unique customers

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

North has three orders but only two customers, and its mean is pulled up by one large order.

Totals must still match

A simple test: sum the grouped revenue and compare it with the revenue before grouping. They should match. If they do not, rows were lost or duplicated somewhere, perhaps by a filter or join.

KNIME's best practices mention an option to process GroupBy and Pivot work in memory to reduce disk use, which needs more memory. Try it only when speed matters and you have the capacity.

Pivot

The Pivot node builds a cross table. You choose:

  • group columns, which become the rows,
  • pivot columns, whose values become new columns,
  • aggregations, which fill the cells.

For example, group by region, pivot on month, and sum amount:

TEXT
region | 2026-10 | 2026-11
North  |     500 |     650
South  |     420 |     ...

This sketch is simplified: the real column names usually also name the aggregation, so check the output before later nodes depend on them. The node also offers extra output tables with totals, which are handy for checks. Think about two things:

  • Column explosion. Pivoting on a column with thousands of distinct values creates thousands of columns. Group the values first.
  • Changing columns. A new month creates a new column. Later nodes that expect fixed names may break or skip it.
Python Stand-in: a pivot built by hand

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.

South has no November sales, so the cell is missing. Decide whether it should show as zero or stay empty. An empty cell can mean "no data yet", which is different from zero.

Unpivot

The Unpivot node does the opposite: it turns selected columns into rows while repeating the others. A spreadsheet with columns Jan, Feb, Mar is easy for people to read but awkward for analysis. Unpivoting gives one row per product and month, with a column for the month name and one for the value. Long format works well with GroupBy and most charts.

Wide and long

Wide One column per month
Long One row per month

Unpivot goes wide to long; Pivot goes long to wide

1 / 2

Other summaries

For row-wise summaries across several columns, such as the largest of three scores in each row, use a Column Aggregator. GroupBy summarizes down rows; Column Aggregator summarizes across columns.

Do not average averages. If one region has 2 orders and another has 2,000, the mean of their two averages is misleading. Recalculate from totals, or weight by count.

Aggregating text and dates

GroupBy is not only for numbers. You can take the first or last value, the earliest or latest date, or a combined list of values, depending on the aggregation you choose. Be careful with "first" and "last": they depend on row order, so sort first or choose the earliest or latest value by date instead. An aggregation that depends on accidental order can change when the data source changes.

Choosing the level of detail

Every summary has a grain, which means what one row represents. "One row per region and month" is a grain. Write it down before you build the node. Many reporting errors come from mixing grains, such as joining a monthly target to daily sales and then summing the target, which multiplies it by the number of days. Aggregate each input to the same grain before joining.

Input Grain Safe to join with monthly targets?
Daily sales One row per day Only after grouping by month
Monthly targets One row per month Yes

Practice task

Create sales rows for two regions over three months, leave one region without data in one month, and include one very large order. Group by region and month, then pivot. Check that the pivot totals equal the original sum, and decide how the empty cell should look in the final report. Then unpivot the report back to long form and compare it with your grouped table. Pay attention to the missing combination: it may appear as a row with a missing value, and you should decide whether to keep it.

Filtering groups

Sometimes you want only the groups that pass a test, such as regions with at least ten orders. Group first, then filter on the aggregated column with a row filter. This differs from filtering individual rows before grouping. The first approach removes whole groups, while the second changes what each group contains. State which one you mean in the node label, because both produce a plausible table.

Check your understanding

Quick check ยท 3 questions

of 3 answered You scored / 3 ยท perfect!

  1. 1. Which aggregation counts different customers in each region?

    A unique count ignores repeated customers.
  2. 2. Which node turns monthly columns into rows?

    Unpivot converts wide columns into long rows.
  3. 3. What is a good check after grouping revenue?

    Grouping should preserve the overall sum.

Summary

  • GroupBy needs group columns and aggregations that fit the question.
  • Counts, unique counts, means, and medians answer different questions.
  • Verify that totals match before and after.
  • Pivot makes cross tables; watch missing cells and changing columns.
  • Unpivot reshapes wide data into long form.

Next: Charts and Views

Your progress is saved in this browser. No account needed.