List.Repeat

List.Repeat

D

The M Code Behind the Power Query M function List.Repeat

Understanding the Function Syntax

Before we dive into the M code, it’s important to understand the syntax of the List.Repeat function. The function takes two arguments: the value that you want to repeat, and the number of times that you want to repeat it. Here’s the syntax:


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


The function returns a list of the repeated values. For example, if you want to repeat the value "hello" three times, the function call would look like this:


List.Repeat(“hello”, 3)


And the output would be:


{“hello”, “hello”, “hello”}


Breaking Down the M Code

Now that we understand the basic syntax of the function, let's take a closer look at the M code behind it. Here's what the code looks like:


let

repeat = List.Generate(

() => 0,

each _ < count,

each _ + 1,

each value

)

in

repeat


Let's break this down line by line.

Line 1: Defining the Variable

The first line of the code defines a variable called "repeat" that will hold the repeated values:


let

repeat = …


Line 2: Using List.Generate

The List.Generate function is a powerful tool in Power Query M that enables you to generate a list of values based on a set of conditions. In this case, we're using List.Generate to repeat a specific value a certain number of times.


let

repeat = List.Generate(

)


Line 3: Defining the Starting Value

The first argument to List.Generate is a function that defines the starting value for the list. In this case, we're starting with a value of 0:


let

repeat = List.Generate(

() => 0,

)


Line 4: Defining the Repeat Condition

The second argument to List.Generate is a condition that determines whether the list generation should continue. In this case, we want to repeat the value until we've reached the specified count:


let

repeat = List.Generate(

() => 0,

each _ < count,

)


The "_" variable is a placeholder variable that represents the current value of the list being generated.

Line 5: Defining the Next Value

The third argument to List.Generate is a function that generates the next value in the list. In this case, we're simply adding 1 to the current value:


let

repeat = List.Generate(

() => 0,

each _ < count,

each _ + 1,

)


Line 6: Defining the Value to Repeat

The fourth argument to List.Generate is a function that generates the value to repeat. In this case, we're using the "value" argument passed to the function:


let

repeat = List.Generate(

() => 0,

each _ < count,

each _ + 1,

each value

)


Line 7: Returning the Repeated Values

The final line of the code simply returns the "repeat" variable, which holds the repeated values:


let

repeat = List.Generate(

() => 0,

each _ < count,

each _ + 1,

each value

)

in

repeat


The List.Repeat function in Power Query M is a powerful tool that enables you to repeat a specific value a certain number of times. The M code behind this function shows how List.Generate can be used to generate a list of values based on a set of conditions. By understanding how this code works, you'll be able to use List.Repeat more effectively in your Power Query projects.

Power Query and M Training Courses by G Com Solutions (0800 998 9248)

Upcoming Courses

Contact Us

Subject

Your Name (required)

Company/Organisation

Email (required)

Telephone

Training Course(s)

Your Message

Upload Example Document(s) (Zip multiple files)

Copyright, G Com Solutions Ltd, 2024.
Tower Bridge Business Centre, 46-48 East Smithfield, London E1W 1AW
0800 998 9248   |   9:00 a.m. till 5:30 p.m.

Connect With Me: