Power Query Table.Partition: Step-by-Step Guide
In this article
Table.Partition splits a table into a list of smaller tables based on a hash of one or more column values. It is not a filter or a group-by; it is a low-level function that distributes rows into a fixed number of buckets, which is useful for parallel processing, sampling, or creating evenly sized chunks. To use it, you need Power Query (available in Excel and Power BI Desktop) and a table to work with. No add-ins or special permissions are required.
What You'll Need
- Power Query Editor open in Excel or Power BI Desktop (labels may differ slightly by version, device, or region).
- A table loaded into Power Query, either from a worksheet, a CSV file, or another data source.
- Basic familiarity with the Power Query formula language (M) and the Advanced Editor, since Table.Partition is typically used in a custom step.
Step-by-Step
- Load your data into Power Query. In Excel, select your table and go to Data > From Table/Range. In Power BI Desktop, use Home > Transform data to open Power Query Editor, then connect to your source. Ensure the table has a header row and consistent column types.
- Open the Advanced Editor. In Power Query Editor, go to Home > Advanced Editor. This shows the M code for your query. If you are not comfortable editing M directly, you can also add a custom step via the formula bar, but the Advanced Editor gives full control.
- Identify the step that returns your table. Look for the last step name (often something like #"Changed Type"). You will add a new step after it. Note the exact name, including any spaces or special characters.
- Write the Table.Partition expression. The syntax is: Table.Partition(table as table, column as text, groups as number, hash as function) as list. For example, to split a table named #"Changed Type" into 4 groups based on the "CustomerID" column using a simple hash, you could write: Table.Partition(#"Changed Type", "CustomerID", 4, each Number.Mod(Text.Length(_), 4)). This uses the length of the text value modulo 4 as the hash. You can replace the hash function with any deterministic function that returns a number from 0 to groups-1. The column argument must be a single column name; to partition on multiple columns, you would need to combine them into a helper column first.
- Add the new step to your query. In the Advanced Editor, add a comma after the previous step and then a new line like: Partitioned = Table.Partition(#"Changed Type", "CustomerID", 4, each Number.Mod(Text.Length(_), 4)). Then change the final output line to return Partitioned instead of the previous step. Click Done.
- Check the result. The query now returns a list of tables, not a single table. In the Power Query Editor, you will see a list with the number of items equal to the groups argument. You can click on each item to preview the rows in that partition. To use the partitions further, you can expand the list into a table of tables or process each partition individually with functions like List.Transform.
- Load or use the partitions. If you want to load the partitions to the worksheet or data model, you need to convert the list into a table. For example, use Table.FromList(Partitioned, Splitter.SplitByNothing(), {"Partition"}) to create a table with one column containing each partition table. Then you can expand or process as needed. Note that loading a list of tables directly is not supported; you must convert it to a table or combine the partitions back into one table if you need a single output.
Common Mistakes
- Using a non-deterministic hash function. The hash function must return the same number for the same input every time; otherwise, partitions will be inconsistent. Avoid functions like Number.Random.
- Forgetting that the column argument is a single column name. Table.Partition does not accept a list of columns. To partition on multiple columns, create a helper column that concatenates the values, then partition on that helper.
- Assuming the partitions are balanced. The hash function determines distribution; a poor hash can put most rows into one partition. Test with a small dataset and adjust the hash or number of groups.
- Expecting a table output. Table.Partition returns a list, not a table. If you need a table, you must convert the list or combine the partitions.
- Using a column name that does not exist or has typos. The column name must match exactly, including case and spaces.
Tips
- Use a hash function that distributes values evenly. For numeric columns, you can use Number.Mod(_, groups) directly. For text, consider using Text.Length or a simple checksum like List.Sum(Text.ToList(_)) modulo groups.
- If you need to process each partition separately (e.g., to apply a transformation), use List.Transform on the resulting list of tables.
- To combine the partitions back into one table, use Table.Combine on the list.
- Partitioning can improve performance when you later process partitions in parallel, but in Power Query itself, processing is sequential. The main benefit is organizational or for downstream tools that can handle lists.
- Document your hash function clearly so others understand how rows are assigned.
Troubleshooting
- Error: "We cannot convert a value of type List to type Table." This occurs when you try to load the list directly. Convert the list to a table first, or combine the partitions.
- Error: "The column 'X' of the table wasn't found." Check the column name spelling and case. Ensure the column exists in the table at the step where you apply Table.Partition.
- Partitions are empty or uneven. Your hash function may not be suitable. Try a different hash, such as using the first character's code point modulo groups, or increase the number of groups.
- The query is slow. Table.Partition itself is fast, but if you have many groups and a large table, the list of tables can be memory-intensive. Consider whether you need all partitions at once.
- Advanced Editor shows errors after adding the step. Check for missing commas, mismatched parentheses, or incorrect step names. Use the formula bar to see the exact syntax.
Frequently Asked Questions
Can I partition on multiple columns?
No, Table.Partition accepts only one column name. To partition on multiple columns, add a helper column that combines the values (e.g., using Text.From and &), then partition on that helper column.
What does the hash function need to return?
The hash function must return a number between 0 and groups-1 inclusive. Power Query uses this number to assign the row to a partition. If the number is outside this range, the row may be placed in an unexpected partition or cause an error.
Can I use Table.Partition in Power BI Desktop?
Yes, Table.Partition is available in Power Query within Power BI Desktop, as well as in Excel. The steps are the same.
How do I access individual partitions?
The result is a list. You can access a specific partition by its index, e.g., Partitioned{0} for the first partition. Use List.Transform to process all partitions.
Is Table.Partition the same as grouping?
No. Grouping (Table.Group) aggregates rows into summary tables based on unique values. Table.Partition assigns rows to a fixed number of buckets based on a hash, without aggregating. Each partition contains a subset of rows, but not necessarily grouped by a specific value.
Can I load the partitions to separate worksheets?
Power Query loads a single table per query. To load partitions separately, you would need to create separate queries that reference each partition, or combine them into one table with a partition identifier column and then use pivot or filter to split them in the worksheet. This is advanced and may require multiple queries.