List.Repeat

Updated on

List.Repeat is a Power Query M function that creates a list containing a specified number of repetitions of the original list. The function returns the newly created list with the repeated elements.

Compatible with: Power BI Service Power BI Desktop Excel Microsoft 365

Syntax

List.Repeat(
   list as list,
   count as number,
) as list

Description

The List.Repeat function creates a new list by repeating the elements of a given list for a specified number of times. It takes two arguments: the list of values to be repeated and the count, which represents the number of repetitions. After performing the operation this function returns a single list with the values repeated consecutively.

Examples

Let’s look at a few examples of the List.Repeat function.

You can create a list that has {1, 2, 3 } repeated 3 times.

// Output: { 1, 2, 3, 1, 2, 3, 1, 2, 3 }
List.Repeat( { 1, 2, 3 }, 3 )

If you’re working on a mathematical or algorithmic problem, you might need to generate a sequence pattern. For instance, if you want a list that repeats the sequence 1 to 5 for 4 times:

// Output: { 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5, 1, 2, 3, 4, 5 }
List.Repeat( List.Numbers( 1, 5 ), 4 )

The List.Repeat function also works with other values, like text. Suppose you’re categorizing items into groups, and you want to label each group with a repeated identifier. For example, if you have 15 items and you want to label them in groups of 5 with “Group A”, “Group B”, and “Group C”:

/* "Group A", "Group A", "Group A", "Group A", "Group A", "Group B", "Group B", 
"Group B", "Group B", "Group B", "Group C", "Group C", "Group C", "Group C", "Group C" */
List.Combine( 
  { 
    List.Repeat( { "Group A" }, 5 ), 
    List.Repeat( { "Group B" }, 5 ), 
    List.Repeat( { "Group C" }, 5 ) 
  } 
)

These are just a few examples, but the potential applications are numerous. The function can be particularly useful when combined with other Power Query functions.

Learn more about List.Repeat in the following articles:

Other functions related to List.Repeat are:

Contribute » | Contributors: Rick de Groot
Microsoft documentation: https://learn.microsoft.com/en-us/powerquery-m/list-repeat

2023-2024 © BI Gorilla. All rights are reserved. Information from Microsoft docs is property of Microsoft Corp. | Privacy Policy