Power Query List.Random Function: How to Use It
In this article
The List.Random function in Power Query generates a list of randomly selected items from an existing list, useful for sampling, testing, or simulating random outcomes. It requires no external tools or setup beyond Power Query in Excel or Power BI. Use it when you need to pull a subset of data without bias or pattern.
What You'll Need
- Microsoft Excel 2016 or later, or Power BI Desktop
- A list of values (text, numbers, dates, or any data type) in a column or defined as a list
- Basic familiarity with Power Query Editor (opening it, loading data, writing M code)
Step-by-Step
- Open Excel or Power BI Desktop and load the data you want to sample. For example, if you have a table with customer names in a column called "CustomerName", make sure it's loaded into Power Query Editor by selecting the data range and choosing "From Table/Range" in Excel, or "Get Data" in Power BI.
- In Power Query Editor, locate the column containing the list you want to sample from. Right-click the column header and select "Extract Values" if you need to convert a table column into a standalone list. Alternatively, create a new list manually using the formula bar: enter
{"Apple", "Banana", "Cherry", "Date", "Elderberry"}as a sample list. - Go to the "Add Column" tab in the ribbon, click "Custom Column", and name the new column something like "RandomSample".
- In the formula box, type:
List.Random({"Apple", "Banana", "Cherry", "Date", "Elderberry"}, 3). This will return a list of three randomly selected items from the five-item list. Replace the list literals with a reference to your actual column if needed (see Step 5). - To reference a column as a list, use
List.Random(Table.Column(#"PreviousStep", "CustomerName"), 5). Replace "PreviousStep" with the name of your last step in the Applied Steps pane (e.g., "Changed Type" or "Source"). The first argument must be a list, so use Table.Column() to extract the column as a list. - Press OK. A new column will appear containing a list of random items. If you want to expand this list into individual rows, click the expand icon (two arrows) in the column header and select "Expand to New Rows".
- If you need to sample without replacement (no duplicates), List.Random already handles this by default—it never repeats items within the same sample. If you want to allow duplicates, you must write a custom function using List.Generate or List.Buffer with repeated random indexing.
- Click "Close & Load" to return the transformed data to Excel or Power BI. The random sample will regenerate each time the query refreshes, ensuring fresh randomness.
Common Mistakes
- Using List.Random on a table instead of a list—this causes an error because the function only accepts lists. Always extract the column first using Table.Column().
- Supplying a number greater than the list length in the second parameter. For example, calling List.Random({1,2,3}, 5) will throw an error since you can't pick 5 items from 3 without repetition, and List.Random doesn't allow duplicates.
- Forgetting to use quotes around text items in the list literal. Writing
List.Random({Apple, Banana}, 2)without quotes causes a syntax error because Power Query interprets Apple and Banana as undefined variables. - Assuming the random sample is static. List.Random recalculates on every refresh. If you need a fixed sample, copy the results and paste as values outside Power Query.
- Trying to use List.Random on a column that contains null values without filtering them out first. Nulls are valid list items, but they may skew your sample. Use Table.SelectRows() to remove nulls before extraction.
Tips
- Use List.Random() for A/B testing: pull 100 random customer IDs from a database to assign to test groups.
- Combine with List.Buffer() to lock the original list in memory before sampling, especially useful if your source data changes during query execution.
- For larger datasets, combine List.Random with List.FirstN() to limit output:
List.FirstN(List.Random(SourceList, 10), 5)gives you 5 items from a random 10-item subset. - Use a fixed seed for reproducible results by wrapping the function in a custom function that sets Random.RandomSeed() before calling List.Random().
- Test your sample size with small datasets first. If you have only 12 items and need 10 random samples, you’ll get 10 unique items. If you need 15, you must allow duplicates or use a different method.
- When sampling from a table with multiple columns, extract only the column you need to avoid bloating the random output with unnecessary data.
Troubleshooting
- Error: "We cannot convert a value of type Table to type List." This means you passed a table instead of a list. Fix: Use
Table.Column(YourTable, "ColumnName")to extract the column as a list before passing it to List.Random. - Error: "The number of elements in the list must be greater than or equal to the count parameter." You requested more items than exist. Reduce the second parameter to match or be less than the list length. Check the list length with
List.Count(YourList)in a new custom column to verify. - No output or blank results Check if your source column contains only nulls. Filter out nulls using
Table.SelectRows(Source, each [ColumnName] <> null)before applying List.Random. - Sample changes every time I refresh This is intentional. If you need static results, right-click the column with the random list, select "Copy", then paste into Excel as "Values Only" outside Power Query.
- Power Query crashes when sampling large lists List.Random can be memory-heavy on lists over 100,000 items. Try sampling a smaller subset first, or use a custom sampling function with List.Positions() and List.Select() for better control.