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

How to conditionally filter a column in Power Query

I have a table in Excel I am extracting rows from user Power Query. I also have an input cell on the output worksheet where a user can enter a value to filter the results.  I have successfully set the power query perform the filter if the input cell is populated. However, if the user leaves the filter value blank, Power Query does not return any rows.

 

I would like to allow the user to leave the filter value blank and return all rows from the query. However do not know how to conditionally apply a filter in Power Query. 

 

Any idea?

1 ACCEPTED SOLUTION
ImkeF
Super User
Super User

You can add a step with a condition like this:

 

if MyParameter <> null then PreviousFilteredStep else PrePreviousUnfilteredStep

 

 

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

View solution in original post

3 REPLIES 3
ImkeF
Super User
Super User

You can add a step with a condition like this:

 

if MyParameter <> null then PreviousFilteredStep else PrePreviousUnfilteredStep

 

 

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

I was just trying to figure out how to do this best myself, and found this reply. 

I am wondering whether declaring it this way will actually lead to the filter in the previous step being executed, and then the conditional parameter line evaluated? Or whether the mashup engine is smart enough to postpone evaluating the filter until it knows it needs to? 

I'm writing mine as;

if MyParameter <> null then ( .. filter expression ) else previousunfilteredstep 


... just in case 

?

 

Yes, M is a partially lazy language. So it will mostly only evaluate what's actually needed.

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
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.