Text.Repeat Function (Power Query M)

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 repeat text. A count of 0 returns 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:

ProductStars
Wireless Mouse4
USB Hub2
Laptop Stand5

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:

ProductRating 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.
  • count must be a number, not text. A text count throws Expression.Error: We cannot convert a value of type Text to type Number. Convert with Number.From or type the column first.
  • A negative count throws an error. Text.Repeat only accepts 0 or higher. Guard a column that might go negative with Number.Max([Col],0).
  • null text returns null, not an empty string. Text.Repeat(null,5) gives null. 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:

I am a huge fan of Microsoft Excel and love sharing my knowledge through articles and tutorials. I work as a business analyst and use Microsoft Excel extensively in my daily tasks. My aim is to help you unleash the full potential of Excel and become a data-slaying wizard yourself.