Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
PietroFarias
Resolver II
Resolver II

Return value from current table to a subquery (Power Query)

Hey guys!

I want the column of a "subquery" to be equal to the column of the current table.

Sample:

= Table.AddColumn(
	NbrMonth,
	"NmbCustomer",
	each
		Table.RowCount(
			Table.SelectRows(
				Table.Distinct(
					Table.SelectColumns(
						NbrMonth,{"Store","Op", "NF"}
					)
				),
			each ([NF] = [NF]) //I want the [NF] column of this "subquery" to be equal to [NF] of the NbrMonth table.
		)
	) 
2 ACCEPTED SOLUTIONS
OwenAuger
Super User
Super User

Hi @PietroFarias

 

This should do the trick:

 

= Table.AddColumn(
    NbrMonth,
    "NmbCustomer",
    each
        let OuterNF = [NF]
        in
        Table.RowCount(
            Table.SelectRows(
                Table.Distinct(
                    Table.SelectColumns(
                        NbrMonth,{"Store","Op", "NF"}
                    )
                ),
                each ([NF] = OuterNF)
            )
        )
    )

 

 Regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

View solution in original post

Glad it worked 🙂

 

Sure - a "let expression" is used to capture values from intermediate calculations before returning a result.

It takes the form

 

let
   variable1 = expression1,
   variable2 = expression2,
   ...
in
   result_expression

In your example, I used let OuterNF = [NF] to store the value of [NF] from the current row so that I could refer to it in the result expression. The resulting "let expression" formatted a little differently was:

let
   OuterNF = [NF]
in
   Table.RowCount(
      Table.SelectRows(
         Table.Distinct(
            Table.SelectColumns(
               NbrMonth,{"Store","Op", "NF"}
            )
         ),
         each ([NF] = OuterNF)
      )
   )

Here is a link to the Power Query M language specification which might be useful for reference - see page 85 for let expressions:

https://msdn.microsoft.com/en-us/query-bi/m/power-query-m-language-specification

 

Best regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

View solution in original post

3 REPLIES 3
OwenAuger
Super User
Super User

Hi @PietroFarias

 

This should do the trick:

 

= Table.AddColumn(
    NbrMonth,
    "NmbCustomer",
    each
        let OuterNF = [NF]
        in
        Table.RowCount(
            Table.SelectRows(
                Table.Distinct(
                    Table.SelectColumns(
                        NbrMonth,{"Store","Op", "NF"}
                    )
                ),
                each ([NF] = OuterNF)
            )
        )
    )

 

 Regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

@OwenAuger

It work! Great.
I'm a newbie in M. Can you explain me why the "LET"?

Glad it worked 🙂

 

Sure - a "let expression" is used to capture values from intermediate calculations before returning a result.

It takes the form

 

let
   variable1 = expression1,
   variable2 = expression2,
   ...
in
   result_expression

In your example, I used let OuterNF = [NF] to store the value of [NF] from the current row so that I could refer to it in the result expression. The resulting "let expression" formatted a little differently was:

let
   OuterNF = [NF]
in
   Table.RowCount(
      Table.SelectRows(
         Table.Distinct(
            Table.SelectColumns(
               NbrMonth,{"Store","Op", "NF"}
            )
         ),
         each ([NF] = OuterNF)
      )
   )

Here is a link to the Power Query M language specification which might be useful for reference - see page 85 for let expressions:

https://msdn.microsoft.com/en-us/query-bi/m/power-query-m-language-specification

 

Best regards,

Owen


Owen Auger
Did I answer your question? Mark my post as a solution!
Blog
Twitter
LinkedIn

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.