top of page

You are learning Power Query in MS Excel

How to use advanced functions like List.Generate or List.Range in Power Query?

Using advanced functions like `List.Generate` or `List.Range` in Power Query allows you to generate lists of values based on specified conditions or ranges. These functions are powerful for creating dynamic lists or performing complex iterations within your data transformations. Here’s how you can use `List.Generate` and `List.Range` in Power Query:

Using List.Generate

Syntax of List.Generate:

The syntax of `List.Generate` in Power Query is:

```m
List.Generate(
initial state as any,
condition as function,
next as function,
optional transform as function
) as list
```

- initial state: Specifies the initial state or starting value of the list.
- condition: A function that evaluates whether to continue generating the list.
- next: A function that defines how to generate the next value in the list.
- transform (optional): A function that transforms each generated value before adding it to the list.

Example: Generating a List of Fibonacci Numbers

Let's create a custom function using `List.Generate` to generate a list of Fibonacci numbers up to a specified limit.

1. Open Power Query Editor:
- Go to the `Data` tab and select `Get Data` > `Launch Power Query Editor`.

2. Create a New Blank Query:
- Go to the `Home` tab and select `New Source` > `Blank Query`.

3. Enter the Function Using List.Generate:
- Go to the `Home` tab and select `Advanced Editor`.
- Enter the following function code:
```m
let
Fibonacci = List.Generate(
() => {0, 1}, // Initial state: Start with the first two Fibonacci numbers
each _[0] <= 100, // Condition: Continue generating until the first number exceeds 100
each {_[1], _[0] + _[1]}, // Next: Generate the next Fibonacci number
each _[0] // Transform: Return the current Fibonacci number
),
Output = Fibonacci
in
Output
```
- Click `Done` to save the function.

4. Use the Custom Function:
- The custom function will generate a list of Fibonacci numbers `[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]` up to the point where the first number exceeds 100.

Using List.Range

Syntax of List.Range:

The syntax of `List.Range` in Power Query is:

```m
List.Range(
list as list,
offset as number,
count as number
) as list
```

- list: The input list from which to extract elements.
- offset: The index of the first element to include in the range (0-based index).
- count: The number of elements to include in the range.

Example: Creating a Range of Numbers

Let's create a range of numbers from 1 to 10 using `List.Range`.

1. Open Power Query Editor:
- Go to the `Data` tab and select `Get Data` > `Launch Power Query Editor`.

2. Create a New Blank Query:
- Go to the `Home` tab and select `New Source` > `Blank Query`.

3. Enter the Function Using List.Range:
- Go to the `Home` tab and select `Advanced Editor`.
- Enter the following function code:
```m
let
Range = List.Range({1..10}, 0, 10),
Output = Range
in
Output
```
- Click `Done` to save the function.

4. Use the Custom Function:
- The custom function will generate a list `[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]` representing numbers from 1 to 10.

Tips for Using Advanced Functions in Power Query

- Parameter Types: Ensure that you provide the correct data types as parameters to functions (`number`, `text`, etc.) for proper execution.
- Iterative Functions: Use `List.Generate` for iterative tasks where the next value depends on the previous value.
- Range Functions: Use `List.Range` to create a sequence of values from a specified starting point and count.
- Performance: Be mindful of performance implications, especially with large datasets. Optimize your queries to minimize processing times.

By utilizing `List.Generate` and `List.Range` in Power Query, you can perform advanced data transformations and generate dynamic lists tailored to your specific needs, enhancing your data analysis capabilities significantly.

bottom of page