Text.Repeat returns a text value made by repeating a piece of text a set number of times. Available in Excel (Power Query), Power BI Desktop, and Power BI Service.
If you want to repeat a string a fixed number of times, whether to build a separator line or a quick text rating bar, Text.Repeat is the function you reach for.
Syntax of Text.Repeat Function
Text.Repeat(text as nullable text, count as number) as nullable text
where
text(required, nullable text). The text value you want to repeat.count(required, number). How many times to repeattext. A count of0returns an empty string.
Returns: a text value that is text repeated count times. If text is null, it returns null.
In plain terms, you hand it a string and a number, and it glues that many copies of the string together back to back.
Example 1: Repeat a short string five times
Repeat the two-character string -= five times to build a small pattern.
Text.Repeat("-=",5)
Result: -=-=-=-=-=
The two characters are joined end to end five times, with no separator added between copies.
Example 2: A count of zero returns an empty string
Text.Repeat("abc",0)
Result: "" (an empty string)
A count of 0 produces no copies, so you get an empty string back rather than an error.
Example 3: Build a separator line
You want a divider line of 30 asterisks to break up a text report.
Text.Repeat("*",30)
Result: ******************************
Repeating a single character is a quick way to make a fixed-width separator without typing it out by hand.
Example 4: Build a text rating bar from a number column
Text.Repeat is handy when the count comes from a column instead of a literal.
Say you have a Feedback query with a Product and a Stars column.
You want to turn each star count into a bar of # characters.
Here is the starting data:
| Product | Stars |
|---|---|
| Wireless Mouse | 4 |
| USB Hub | 2 |
| Laptop Stand | 5 |
Add a column that repeats # once per star, then keep only the columns you need:
let
Source = Excel.CurrentWorkbook(){[Name="Feedback"]}[Content],
Typed = Table.TransformColumnTypes(Source,{{"Stars",Int64.Type}}),
Added = Table.AddColumn(Typed,"Rating Bar",each Text.Repeat("#",[Stars])),
Result = Table.SelectColumns(Added,{"Product","Rating Bar"})
in
Result
Each row repeats # as many times as the Stars value for that product.
The result produces:
| Product | Rating Bar |
|---|---|
| Wireless Mouse | #### |
| USB Hub | ## |
| Laptop Stand | ##### |
The Stars column is typed to Int64.Type first, because count has to be a number for Text.Repeat to use it.
Things to keep in mind with Text.Repeat
- It adds no separator between copies. Copies are joined directly. To put a delimiter between them, use
Text.Combine(List.Repeat({"text"},count),", ")instead. countmust be a number, not text. A text count throwsExpression.Error: We cannot convert a value of type Text to type Number.Convert withNumber.Fromor type the column first.- A negative
countthrows an error.Text.Repeatonly accepts0or higher. Guard a column that might go negative withNumber.Max([Col],0). nulltext returnsnull, not an empty string.Text.Repeat(null,5)givesnull. Replace blanks first with[Col] ?? ""if you need an empty string instead.
Common questions about Text.Repeat
What is the difference between Text.Repeat and Text.PadStart?
Text.Repeat repeats a string a literal number of times. Text.PadStart pads a value up to a target total length, so it stops once the text reaches that width. Reach for Text.PadStart for leading-zero padding and Text.Repeat for a fixed count of copies.
Can I repeat a whole list instead of text?
Not with Text.Repeat, which only works on text. Use List.Repeat({1,2},3) to repeat list items, then Text.Combine if you need the result as a single string.
List of All Power Query Functions
Related Power Query Functions / Articles: