# List.Times

## What is the List.Times function?

The List.Times function is used to repeat a value or a list of values a specified number of times. It takes two arguments: the value or list to repeat and the number of times to repeat it. For example, List.Times(5,3) will return the list {5,5,5}. List.Times can also be used to repeat a list of values. For example, List.Times({1,2,3},2) will return the list {1,2,3,1,2,3}.

## The M code behind the List.Times function

The M code behind the List.Times function is relatively simple. It uses a for loop to repeat the specified value or values the specified number of times. Here is the M code for the List.Times function:

(List as any, Count as number) =>

let

Source = List.Generate(() => 0, each _ < Count, each _ + 1, each List)

in

Source{Count}

``` Let's break down this code line by line. Line 1 ```

(List as any, Count as number) =>

``` This line defines the function and its arguments. The List argument can be of any data type, while the Count argument must be a number. Line 2 ```

let

``` This line begins a let statement, which is used to define variables and/or expressions that can be referenced later in the M code. Line 3 ```

Source = List.Generate(() => 0, each _ < Count, each _ + 1, each List)

``` This line defines the Source variable using the List.Generate function. The List.Generate function is used to generate a list from a seed value and a function that defines how to generate subsequent values. In this case, the seed value is zero, the function to generate subsequent values checks if the current value is less than the count argument, and the function to generate the next value adds one to the current value. Line 4 ```

in

``` This line indicates the end of the let statement. Line 5 ```

Source{Count}

``` This line returns the last item in the Source list, which is the list of repeated values. The curly brackets are used to access the elements of the list by index. Examples of using the List.Times function Here are some examples of using the List.Times function in Power Query: Example 1 Suppose we want to repeat the value "hello" three times. We can use the following M code: ```

List.Times(“hello”,3)

``` This will return the list {"hello","hello","hello"}. Example 2 Suppose we have a list of values {1,2,3} and we want to repeat it four times. We can use the following M code: ```

List.Times({1,2,3},4)

``` This will return the list {1,2,3,1,2,3,1,2,3,1,2,3}. Example 3 Suppose we have a table with a column called "Value" and we want to repeat each value in the column four times. We can use the following M code: ```

let

Source = #”Previous Step”,

RepeatedValues = List.Times(Source[Value],4),

Output = Table.FromColumns({RepeatedValues}, {“RepeatedValues”})

in

Output

``` This will create a new table with a column called "RepeatedValues" containing the repeated values from the "Value" column. The List.Times function is a powerful tool for repeating values or lists of values in Power Query. The M code behind the function is relatively simple, using a for loop to generate the repeated values. With a little bit of knowledge about the List.Generate function, you can customize the List.Times function to suit your needs. Power Query and M Training Courses by G Com Solutions (0800 998 9248) Power Query and M Intensive Training Course £1,260.00 – £31,860.00 Select optionsContinue Loading Done Power Query and M Introduction £474.00 – £11,700.00 Select optionsContinue Loading Done Power Query and M Intermediate £474.00 – £11,700.00 Select optionsContinue Loading Done Power Query and M Advanced £474.00 – £11,700.00 Select optionsContinue Loading Done Upcoming Courses Contact Us Subject Your Name (required) Company/Organisation Email (required) Telephone Training Course(s) Power BI Intensive TrainingPower BI introduction Power BI IntermediatePower BI AdvancedDAXPower Query MPower BI CertificationPower BI AdministrationPower PlatformPower AutomatePower AppsOTHER Your Message Upload Example Document(s) (Zip multiple files) ```
``` ```
``` ```
``` ```
``` ```
``` ```