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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
markwieland
Frequent Visitor

How to use SQL query criteria stored in a column to return data using a function or Table.SelectRows

I have following challenge:

Say I have a table like this:

Name

Type

Color

Size

A

Shirt

Red

XL

B

Shirt

Blue

XXL

C

Pants

Green

L


and a table like this:

Lookup filter name

Criteria

Shirts

(Type = “Shirt”)

Shirts XL

(Type = “Shirts” AND Size = “XL”)

Pants

(Type = “Pants”)


What I want to do is add data from the first table to the second table, like when you merge a query or use Table.SelectRows and then be able to expand the data to see which records from first table match the criteria of the lookup filter in the second.

 

I tried doing something like adding a custom column with “Table.SelectRows(LookupTable,Each[Criteria])” but received errors.

 

I think I need to write a function, but have no idea how to do that for this scenario?

 

Can someone help me?

3 ACCEPTED SOLUTIONS

Oh good grief @ImkeF - duh!

 

Thanks! So obvious. I was looking for something difficlut.

 

@markwieland - the final formula for the filter is:

= Table.AddColumn(
            #"Changed Type",
            "Custom", 
            each
            let 
                varFilter = [Criteria]
            in
                Table.SelectRows(Table, Expression.Evaluate("each" & varFilter, [_ = _]))
        )

 

Here is the final link. I keep files in a different folder when they are solved. The above link will not work.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post

Wow @camargos88 @edhans @ImkeF  I usually never post questions on forums like this because I always thought it took days to get answers. You proved me wrong, less than 24 hours and I have some info to work with. I will try to apply your solutions to my actual case and let you know the outcome! Thanks!

View solution in original post

Glad to help @markwieland 

 

Remember, everyone here is a volunteer. Sometimes stuff can take days, other times minutes. Always post. Even if you discover a workable solution first, you can get a different perspective here that might improve on your solution.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

View solution in original post

8 REPLIES 8
camargos88
Community Champion
Community Champion

Hi @markwieland ,

 

I worked here, changing a bit your source to:

 

Lookup filter name CriteriaFilter

Shirts[Type] = "Shirt"
Pants[Type] = "Pants"
Shirts XL[Type] = "Shirt" and [Size] = "XL"

 

I tried with the original but the brackets and lowercase logical operators were missing, maybe there is another way to do it.

 

And using the function that @edhans and @ImkeF provided:

 

Table.SelectRows(Table, Expression.Evaluate("each " & _filter, [_=_]))

 

Capture.PNG

 

 



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



Oh good grief @ImkeF - duh!

 

Thanks! So obvious. I was looking for something difficlut.

 

@markwieland - the final formula for the filter is:

= Table.AddColumn(
            #"Changed Type",
            "Custom", 
            each
            let 
                varFilter = [Criteria]
            in
                Table.SelectRows(Table, Expression.Evaluate("each" & varFilter, [_ = _]))
        )

 

Here is the final link. I keep files in a different folder when they are solved. The above link will not work.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting

Wow @camargos88 @edhans @ImkeF  I usually never post questions on forums like this because I always thought it took days to get answers. You proved me wrong, less than 24 hours and I have some info to work with. I will try to apply your solutions to my actual case and let you know the outcome! Thanks!

Glad to help @markwieland 

 

Remember, everyone here is a volunteer. Sometimes stuff can take days, other times minutes. Always post. Even if you discover a workable solution first, you can get a different perspective here that might improve on your solution.



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
camargos88
Community Champion
Community Champion

Hi @markwieland ,

 

Does your table Lookup have this the second column like this (Type = “Shirt”) ?

If yes, it's possible to query using table function, but more work.

 

If no, show us your base table and we can help you.



Did I answer your question? Mark my post as a solution!

Proud to be a Super User!



Hi Ricardo, 

 

Thanks for replying!

I can't share the actual data, but the criteria column contains information like this:

 

((Type = "Shirt" AND Size ="XL") OR (Type = "Shirt") OR (Color = "Red")) AND Someother field = "X"

 

The complexity is that each row is different and uses different fields in a different order.

Can you help me with a function I can use?

 

 

I feel like I am close here, but this is returning an error:

 

= Table.AddColumn(
            #"Changed Type",
            "Custom", 
            each
            let 
                varFilter = [Criteria]
            in
                Table.SelectRows(Table, Expression.Evaluate(varFilter, [_ = _]))
        )

The problem is I am getting this error:

2020-05-27 11_44_10-20200527 Dynamic Filter - Power Query Editor.png

My PBIX file is here.

@ImkeF - can you see what I'm doing wrong here?



Did I answer your question? Mark my post as a solution!
Did my answers help arrive at a solution? Give it a kudos by clicking the Thumbs Up!

DAX is for Analysis. Power Query is for Data Modeling


Proud to be a Super User!

MCSA: BI Reporting
ImkeF
Community Champion
Community Champion

Hi @edhans ,

you're missing the "each" keyword that defines the function:

 

Table.SelectRows(Table, Expression.Evaluate("each" & varFilter, [_ = _]))

 

 

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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

Top Solution Authors
Top Kudoed Authors