Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

Power Query Text.ToList Function: Usage and Guide

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

The Text.ToList function in Power Query (M language) converts a single text string into a list where each character from the original string becomes an individual item in that list. To use it, you simply wrap your text value or column reference in the function: Text.ToList("Hello") results in a list containing {"H", "e", "l", "l", "o"}. This is primarily used when you need to perform operations on a per-character basis, such as counting specific characters, validating string patterns, or transforming a string into a vertical table format for granular analysis.

What You'll Need

  • Microsoft Excel (with Power Query/Get & Transform) or Power BI Desktop.
  • A data source containing text strings that you wish to decompose into individual characters.
  • Basic familiarity with the Power Query Editor interface and the formula bar.

Step-by-Step

  1. Load Your Data: Open Power Query by going to the Data tab in Excel and selecting Get Data, or by opening Power BI Desktop and selecting Get Data. Import the table or file containing the text you want to process.
  2. Open the Custom Column Dialog: Navigate to the Add Column tab on the top ribbon. Click on Custom Column. This opens a dialog box where you can write M code to create a new column based on existing data.
  3. Enter the Function: In the Custom column formula box, type Text.ToList([YourColumnName]). Replace [YourColumnName] with the actual name of the column containing your text. Ensure you use the square brackets to reference the column correctly.
  4. Verify the Result: Click OK. You will see a new column added to your table. Every cell in this column will now say "List". This is because Power Query displays a summary label for list objects rather than expanding the contents immediately.
  5. Expand the List: To see the actual characters, click the small double-arrow icon (expand button) at the top right of the new column header. You have two primary choices here:
    • Expand to New Rows: This will duplicate the rest of the row data for every single character in the list. If a cell had 5 characters, that row becomes 5 separate rows. This is the most common choice for data cleaning and character counting.
    • Extract Values: This converts the list back into a text string, usually separated by a delimiter (like a comma). This is less common when using Text.ToList but useful for formatting.
  6. Set the Data Type: After expanding, the new column may be set to "Any". Click the data type icon next to the column header and select Text to ensure the characters are handled correctly in subsequent steps.

Common Mistakes

One of the most frequent errors is attempting to use Text.ToList on a column that is not explicitly typed as text. If the column is typed as "Any" or "Number", Power Query may throw an expression error stating that it cannot convert the value to text. To fix this, ensure you have applied a "Change Type" step to set the source column to Text before adding the custom column.

Another common mistake is confusing Text.ToList with Text.Split. While both create lists, Text.Split requires a delimiter (like a comma or space) and breaks the text into words or phrases. Text.ToList ignores delimiters and breaks the text into every single character, including spaces and punctuation. Using the wrong function leads to either a list of words when you wanted characters, or a massive list of characters when you wanted words.

Users also often forget that expanding a list to new rows can exponentially increase the size of the dataset. If you have a table with 100,000 rows and each row has a 50-character string, expanding Text.ToList will result in 5 million rows. This can lead to significant performance degradation or cause the application to crash if the system lacks sufficient RAM.

Tips

To optimize performance when dealing with large datasets, avoid expanding the list to new rows if you only need a summary count. Instead of expanding, you can wrap Text.ToList inside other functions. For example, if you want to count how many times the letter "a" appears in a string, you can use List.Count(List.Select(Text.ToList([Column]), each _ = "a")). This keeps the data in a single row and is much faster than expanding and filtering.

If you need to remove whitespace before converting to a list, use Text.Trim. By writing Text.ToList(Text.Trim([Column])), you ensure that leading or trailing spaces do not end up as individual items in your list, which prevents skewed results during character analysis.

For advanced users, combining Text.ToList with List.Transform allows for powerful character-level manipulation. You can convert every character to uppercase, replace specific symbols, or shift characters based on a logic map before expanding the list back into a table.

Troubleshooting

If you encounter an error saying "We cannot convert the value to type Text", the first step is to check your data types. Go to the step immediately preceding your custom column and ensure the target column is set to the Text type. If there are null values in the column, Text.ToList will return an error for those specific rows. To handle this, use an if statement: if [Column] <> null then Text.ToList([Column]) else null.

If Power Query becomes unresponsive after you click "Expand to New Rows", it is likely a memory issue caused by the volume of data. To resolve this, try filtering your source data to a smaller subset first to verify your logic. If the logic is correct but the data is too large, consider performing the character analysis using a custom function or a different aggregation method that does not require expanding the rows.

If the characters are not appearing as expected (e.g., strange symbols appearing), check the encoding of your source file. If the file was imported with the wrong origin (e.g., UTF-8 vs. Windows-1252), Text.ToList will faithfully convert those incorrect characters into a list. Re-import the data with the correct encoding settings.

Frequently Asked Questions

Does Text.ToList include spaces?

Yes, every single character in the string, including spaces, tabs, and punctuation marks, is treated as an individual element in the resulting list.

Can I use this function to count the length of a string?

While you could use List.Count(Text.ToList([Column])), it is much more efficient to use the built-in Text.Length([Column]) function for this specific purpose.

What is the difference between Text.ToList and Text.Split?

Text.ToList breaks a string into individual characters regardless of content. Text.Split breaks a string into a list based on a specific separator you provide, such as a comma or a semi-colon.

Can I convert the list back into a string?

Yes, you can use Text.Combine(list, separator) to merge the items of a list back into a single text string, optionally adding a character between each item.

Does this function work with numbers?

No, it only works with text. If your data is numeric, you must first wrap the column in Text.From([Column]) before applying Text.ToList.