How can I use input from a TextBox in a PowerQuery Formula?

筅森魡賤 提交于 2021-02-08 10:20:30

问题


I would like to use a PowerQuery Formula which includes input from a text box like

Json.Document(Web.Contents("https://thesite.com/api/widgets",
              [Query=[name=TextBoxName.Text]]))

But I get this error

Expression.Error: The import TextBoxName.Text matches no exports. Did
 you miss a module reference?

How do I use TextBoxName.Text as a parameter in my query?


回答1:


Does it have to be a text box? Put the value in an Excel table instead and call that table "Parameters".

Then write a function that reads the parameters in the table.

(ParameterName as text) =>
let
   ParamSource = Excel.CurrentWorkbook(){[Name="Parameters"]}[Content],
   ParamRow = Table.SelectRows(ParamSource, each ([Parameter] ParameterName)),
   Value=
      if Table.IsEmpty(ParamRow)=true
      then null
      else Record.Field(ParamRow{0},"Value")
in
   Value

Give that function the name fnGetParameter and load it.

Now you can use this function in another query to get the value of any parameter in the table like this:

let
   MyQuery = fnGetParameter("JSONQuery"),
   Source = Json.Document(Web.Contents("https://thesite.com/api/widgets",
              [Query=[name=MyQuery]]))

Credit goes to Ken Puls who describes this technique on his blog and also in his book "M is for (Data) Monkey".



来源:https://stackoverflow.com/questions/34027392/how-can-i-use-input-from-a-textbox-in-a-powerquery-formula

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!