Power Query to use a cell reference as a filter

Trebor76

Well-known Member
Joined
Jul 23, 2007
Messages
5,365
Office Version
  1. 365
Platform
  1. Windows
Hi there,

I have this line in my Power Query which is returning the desired value from a single cell named "ProcessDate_From':

Power Query:
strDateFrom = Text.From(Excel.CurrentWorkbook(){[Name="ProcessDate_From"]}[Content]{0}[Column1])

But I can't it to work using this ("ProcessDate" is the field name the SQL Db)...

Power Query:
[ProcessDate] = [strDateFrom]

...but this works fine:

Power Query:
[ProcessDate] = '2023-09-01'
(note this part of larger M code)

Any advice would be greatly appreciated.

Regards,

Robert
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Why are you using [strDateFrom] rather than strDateFrom, and how/where are you actually using that test?
 
Upvote 0
Hi Rory,

Thanks for the reply.

I was using square brackets out of habit as that's I reference fields in a SQL statement. Even without them I still get an "Invalid column name 'strDateFrom'" error message.

I use the variable in a M statement like "AND [Source] <> 'Rates' AND [ProcessDate] = strDateFrom". If it were VBA I'd use this:

VBA Code:
AND [Source] <> 'Rates' AND [ProcessDate] = '" & strDateFrom & "'"

Hope that helps.

Robert
 
Upvote 0
You’d do the same in PQ assuming that’s part of a SQL string
 
Upvote 1
Solution
Ah thanks Rory seems to have done it. I should have used the KISS principal.

I need to format the date variables and I'm trying this but it keeps saying the function expects three arguments but I've given one:

Power Query:
strDateFrom = Date.ToText(#date(Excel.CurrentWorkbook(){[Name="ProcessDate_From"]}[Content]{0}[Column1]),"YYYY-MM-DD")

Do you know what I'm missing?

Many thanks
 
Upvote 0
Actually this seems to work:

Power Query:
strDateFrom = Text.From(Excel.CurrentWorkbook(){[Name="ProcessDate_From"]}[Content]{0}[Column1],"en-CA")

Thanks Rory :)
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top