# Power Query - Mcode to sort only if cell is not blank



## HarveyH123 (Jun 14, 2018)

Hello,

I want to create a query that uses a paramater taken from my excel worksheet.

i have managed to do this with this code; 

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Filtered Rows" = Table.SelectRows(Source, each ([StepTag] = GetValue("Filter")))
in
    #"Filtered Rows"

However, i want the code to only filter if the cell (named *filter*) is not blank.

i.e. I dont want to filter at all if the cell in blank.

Hope this makes sense.

Thank you,


----------



## Norie (Jun 14, 2018)

What is GetValue?

Is the StepTag field text?

If it is try this.

let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Filtered Rows" = Table.SelectRows(Source, each (Text.Contains([Step_Tag],if GetValue("Filter")=null then "" else GetValue("Filter"))))
in
    #"Filtered Rows"


----------



## Cool blue (Jun 15, 2018)

Hey Norie! 'sup fella?

you could also do like...


```
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Filtered Rows" = Table.SelectRows(Source, each 
    let
        filterTarget = GetValue("Filter")
    in
        filterTarget = null or (Text.Contains([Step_Tag], filterTarget)))
in
    #"Filtered Rows"
```


----------

