cancel
Showing results for 
Search instead for 
Did you mean: 

Fabric is Generally Available. Browse Fabric Presentations. Work towards your Fabric certification with the Cloud Skills Challenge.

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
PBI November 2023 Update Carousel

Power BI Monthly Update - November 2023

Check out the November 2023 Power BI update to learn about new features.

Community News

Fabric Community News unified experience

Read the latest Fabric Community announcements, including updates on Power BI, Synapse, Data Factory and Data Activator.

Dashboard in a day with date

Exclusive opportunity for Women!

Join us for a free, hands-on Microsoft workshop led by women trainers for women where you will learn how to build a Dashboard in a Day!

Power BI Fabric Summit Carousel

The largest Power BI and Fabric virtual conference

130+ sessions, 130+ speakers, Product managers, MVPs, and experts. All about Power BI and Fabric. Attend online or watch the recordings.

Top Solution Authors
Top Kudoed Authors