Power Query dynamic reference to a column

cr731

Well-known Member
Joined
Sep 17, 2010
Messages
611
I'm trying to filter a table but use a dynamic reference to determine column is filtered, like this,

Code:
Table.SelectRows(MyTable, each ("[" & FieldToFilter & "]" = "FilterValue"))

This isn't working... what am I missing? I'm assuming something is amiss in the "[" & FieldToFilter & "]" section but I can't figure out the correct syntax. I'm trying to replicate [FieldToFilter] but M doesn't seem to work that way.

Thanks
 

Excel Facts

Wildcard in VLOOKUP
Use =VLOOKUP("Apple*" to find apple, Apple, or applesauce
You should use slicer. At the field level put a dummy formula to tell Excel that you are making calculation. Then use the newly create onto your pivot table.
 
Upvote 0
You can use Record.Field() to allow a dynamic column name:
Code:
Table.SelectRows(MyTable, each (Record.Field(_, FieldToFilter) = "FilterValue"))
 
Upvote 0
You should use slicer. At the field level put a dummy formula to tell Excel that you are making calculation. Then use the newly create onto your pivot table.

I'm trying to filter my source data before it gets to PowerPivot though - my source has a few hundred thousand rows and I only need a small selection - so I'm trying to get it filtered down. However, that filter might occur based upon different fields, so I need to have a dynamic way to filter the table.
 
Upvote 0
You can use Record.Field() to allow a dynamic column name:
Code:
Table.SelectRows(MyTable, each (Record.Field(_, FieldToFilter) = "FilterValue"))

Thanks... that works perfectly. What does the underscore represent?
 
Upvote 0
That's good :)

In this case, the underscore represents a record corresponding to each row of MyTable, which Table.SelectRows iterates over.

In general
each < expression involving _ >
is shorthand for a function taking a single argument represented by the underscore.

So
Code:
each (Record.Field(_, FieldToFilter) = "FilterValue")
could be rewritten as
Code:
 (CurrentRow) =>  (Record.Field(CurrentRow, FieldToFilter) = "FilterValue")

The Table.SelectRows function expects (as its 2nd argument) a function which takes a single argument of type record and returns a value of type logical. The record argument (represented by the underscore) iterates over each row of the table.
 
Last edited:
Upvote 0
I'm trying to filter a table but use a dynamic reference to determine column is filtered, like this,

Code:
Table.SelectRows(MyTable, each ("[" & FieldToFilter & "]" = "FilterValue"))

This isn't working... what am I missing? I'm assuming something is amiss in the "[" & FieldToFilter & "]" section but I can't figure out the correct syntax. I'm trying to replicate [FieldToFilter] but M doesn't seem to work that way.

Thanks

e="each " & pemail & " <> " & """""",

// either
#"Filtered Rows" = Table.SelectRows(#"Changed Type1", Expression.Evaluate("each " & pemail & " <> " & """"""))
// or
#"Filtered Rows" = Table.SelectRows(#"Changed Type1", Expression.Evaluate(e))
 
Upvote 0

Forum statistics

Threads
1,223,333
Messages
6,171,511
Members
452,407
Latest member
Broken Calculator

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top