Forum Discussion

sslavin's avatar
sslavin
New Member
8 years ago
Solved

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?

  • You can add a step with a condition like this:

     

    if MyParameter <> null then PreviousFilteredStep else PrePreviousUnfilteredStep

     

     

3 Replies

  • ImkeF's avatar
    ImkeF
    Community Champion

    You can add a step with a condition like this:

     

    if MyParameter <> null then PreviousFilteredStep else PrePreviousUnfilteredStep

     

     

    • reubster's avatar
      reubster
      Frequent Visitor

      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 

      ?

       

      • ImkeF's avatar
        ImkeF
        Community Champion

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