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
JWard_CC_work
New Member

Dynamic Filter or Query table based on column name in another table (or list)?

In Power Query, is there a way to dynamically filter (or merge) a primary table based on a column name (and values) found in another table (or list)?

 

Hopefully this visual helps understand my question:

 

Main Table (to filter):

RowNumberTier1Tier2Details
1Tier1Val1Tier2val1blah blah 1
2Tier1Val2Tier2val2blah blah 2

 

Filter Table (where I define which column to filter or merge on, and the value to match):

FilterColumnFilterValue
Tier2Tier2val2

 

Can this be done (without pivoting or unpivoting the first column)?

 

Thanks in advance!

2 ACCEPTED SOLUTIONS
m_dekorte
Super User
Super User

Hi @JWard_CC_work 

 

You could use Record.Field within Table.SelectRows, copy this script into a new blank query. 

Note that "Filters" is included in this query but you can just as easily place that in a separate query as long as you give that new query the same name OR update the name in the expression below.

let
    Filters = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCslMLTJS0oHQZYk5RkqxsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [FilterColumn = _t, FilterValue = _t]),
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUQrJTC0yDEvMgbGNyiDspJzEDAUwYagUqxOtZISk1ghJrRGKWiOl2FgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [RowNumber = _t, Tier1 = _t, Tier2 = _t, Details = _t]),
    SelectRows = Table.SelectRows( Source, each Record.Field( _, Filters{0}[FilterColumn] ) = Filters{0}[FilterValue] )
in
    SelectRows

 

Here's the result

m_dekorte_0-1683894430532.png

 

Ps. If this helps solve your query please mark this post as Solution, thanks!

View solution in original post

BA_Pete
Super User
Super User

Hi @JWard_CC_work ,

 

I'd buffer your filter table column as a list and pass it into a Table.SelectRows, like this:

Table.SelectRows(
    previousStepName,
    each List.Contains(List.Buffer(FilterTable[FilterValue]), [Tier2])
)

 

The advantage with this over @m_dekorte 's answer, and others, is that this method will fold to the source, even if the second (filter) table isn't on the server.

Power query will buffer the filter table column then stream it to the foldable source in the WHERE clause of the native query, making it clean, tidy, and fast.

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




View solution in original post

2 REPLIES 2
BA_Pete
Super User
Super User

Hi @JWard_CC_work ,

 

I'd buffer your filter table column as a list and pass it into a Table.SelectRows, like this:

Table.SelectRows(
    previousStepName,
    each List.Contains(List.Buffer(FilterTable[FilterValue]), [Tier2])
)

 

The advantage with this over @m_dekorte 's answer, and others, is that this method will fold to the source, even if the second (filter) table isn't on the server.

Power query will buffer the filter table column then stream it to the foldable source in the WHERE clause of the native query, making it clean, tidy, and fast.

 

Pete



Now accepting Kudos! If my post helped you, why not give it a thumbs-up?

Proud to be a Datanaut!




m_dekorte
Super User
Super User

Hi @JWard_CC_work 

 

You could use Record.Field within Table.SelectRows, copy this script into a new blank query. 

Note that "Filters" is included in this query but you can just as easily place that in a separate query as long as you give that new query the same name OR update the name in the expression below.

let
    Filters = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCslMLTJS0oHQZYk5RkqxsQA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [FilterColumn = _t, FilterValue = _t]),
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUQrJTC0yDEvMgbGNyiDspJzEDAUwYagUqxOtZISk1ghJrRGKWiOl2FgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [RowNumber = _t, Tier1 = _t, Tier2 = _t, Details = _t]),
    SelectRows = Table.SelectRows( Source, each Record.Field( _, Filters{0}[FilterColumn] ) = Filters{0}[FilterValue] )
in
    SelectRows

 

Here's the result

m_dekorte_0-1683894430532.png

 

Ps. If this helps solve your query please mark this post as Solution, thanks!

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.

Top Solution Authors
Top Kudoed Authors