List.Product

List.Product

D

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

Understanding List.Product Function

Before we dive into the M code, let’s understand the List.Product function in Power Query. The List.Product function takes two or more lists as inputs. It then multiplies the items in each of the lists and returns the product of all the items.

For example, let’s say we have two lists: “ListA” with 2, 4, and 6 as its items, and “ListB” with 3, 5, and 7 as its items. The List.Product function will multiply 2 x 3, 4 x 5, and 6 x 7, and then return the product of these three results: 2 x 3 x 4 x 5 x 6 x 7 = 2520.

The M Code Behind List.Product Function

Now that we understand what the List.Product function does, let’s take a closer look at the M code behind this function. The M code behind the List.Product function is:


(List1 as list, List2 as list, optional Lists as list) =>

List.Accumulate(

List.Combine({List1, List2} & Lists),

1,

(state, current) => state current

)


Let's break down this code to understand how it works.

Inputs

The List.Product function takes two or more lists as inputs. These inputs are represented by the parameters "List1", "List2", and "optional Lists". The "optional Lists" parameter is optional, which means that we can pass any number of additional lists to the function.

List.Combine Function

The List.Combine function is used to combine all the lists passed to the function. This function takes a list of lists as its input and returns a single list that contains all the items in the input lists. In the M code for the List.Product function, the List.Combine function is used to combine the two input lists ("List1" and "List2") and any additional lists passed to the function ("Lists").


List.Combine({List1, List2} & Lists)


The "&" operator is used to concatenate the two lists ("List1" and "List2") with any additional lists passed to the function ("Lists").

List.Accumulate Function

The List.Accumulate function is used to iterate over the list of combined items and multiply them together. This function takes three arguments:

1. The list of items to iterate over

2. The initial state (in this case, 1)

3. The function that performs the iteration (in this case, multiplying the state and the current item together)


List.Accumulate(

List.Combine({List1, List2} & Lists),

1,

(state, current) => state current

)


The List.Accumulate function returns the final result of the iteration, which is the product of all the items in the input lists.

In summary, the List.Product function in Power Query takes two or more lists as inputs and returns the product of all the items in the lists. The M code behind this function uses the List.Combine and List.Accumulate functions to combine the input lists and iterate over the combined items, multiplying them together to get the final result. Understanding the M code behind the List.Product function can help you to better understand how it works and how to use it effectively in your data transformation and analysis 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: