PowerQuery 中用户定义函数的参数选项
Parameter options for User Defined Functions in PowerQuery
你好,我一直在尝试制作一个用户定义的函数,允许用户select函数将从列表中使用的值。
我已经尝试将我想要的参数设置为列表以在我的函数中键入列表,但这似乎只接受列而不是用户可以 select 来自的值列表。
let
ListOfDays = {1.1,0.5,2,3,1},
DayOfTheWeek = (Day as list, HoursWorked ) =>
let
Earnings = Day * HoursWorked
in
Earnings
in
DayOfTheWeek
我希望允许用户select ListOfDays 列表中的单个值。我在我的函数参数中使用了类型列表,这样它就可以为用户提供一个下拉列表类型的选项。
如果用户能够打开查询编辑器,则他们可以从下拉列表中选择一个 Day
参数并将其自动应用于查询。
您可以从“管理参数”>“新建参数”菜单创建参数
图片右上角的drop-down是用户select的选择。
您的用户定义函数 fn_DayOfTheWeek
如下:
let
DayOfTheWeek = (Day as number, HoursWorked as number) =>
let
Earnings = Day * HoursWorked
in
Earnings
in
DayOfTheWeek
请注意,Day
是一个数字,而不是列表。您想从列表中选择,而不是将列表传递给函数。
现在您可以使用参数调用函数来实际生成结果。
let
Source = fn_DayOfTheWeek(Day, <HoursWorked value here>)
in
Source
此结果将在您更改参数时更新。
如您所见,用户是否有权访问查询编辑器是这种方法的一个关键问题。我不确定是否可以以某种方式直接在自定义连接器对话框中设置参数,但这在功能上应该是等效的。
我相信这是您正在寻找的相关文档:
github.com/microsoft/DataConnectors/docs/function-docs.md: Adding Function Documentation
特别是,查看 Documentation.AllowedValues
的定义:
List of valid values for this parameter. Providing this field will change the input from a textbox to a drop down list. Note, this does not prevent a user from manually editing the query to supply alternate values.
这个(和其他 Documentation
字段)是函数参数元类型的一部分。向下滚动到显示如何使用它们的代码片段:
[DataSource.Kind="HelloWorldWithDocs", Publish="HelloWorldWithDocs.Publish"]
shared HelloWorldWithDocs.Contents = Value.ReplaceType(HelloWorldImpl, HelloWorldType);
HelloWorldType = type function (
message as (type text meta [
Documentation.FieldCaption = "Message",
Documentation.FieldDescription = "Text to display",
Documentation.SampleValues = {"Hello world", "Hola mundo"}
]),
optional count as (type number meta [
Documentation.FieldCaption = "Count",
Documentation.FieldDescription = "Number of times to repeat the message",
Documentation.AllowedValues = { 1, 2, 3 }
]))
as table meta [
Documentation.Name = "Hello - Name",
Documentation.LongDescription = "Hello - Long Description",
Documentation.Examples = {[
Description = "Returns a table with 'Hello world' repeated 2 times",
Code = "HelloWorldWithDocs.Contents(""Hello world"", 2)",
Result = "#table({""Column1""}, {{""Hello world""}, {""Hello world""}})"
],[
Description = "Another example, new message, new count!",
Code = "HelloWorldWithDocs.Contents(""Goodbye"", 1)",
Result = "#table({""Column1""}, {{""Goodbye""}})"
]}
];
HelloWorldImpl = (message as text, optional count as number) as table =>
let
_count = if (count <> null) then count else 5,
listOfMessages = List.Repeat({message}, _count),
table = Table.FromList(listOfMessages, Splitter.SplitByNothing())
in
table;
他们还提供了调用时的截图:
你好,我一直在尝试制作一个用户定义的函数,允许用户select函数将从列表中使用的值。
我已经尝试将我想要的参数设置为列表以在我的函数中键入列表,但这似乎只接受列而不是用户可以 select 来自的值列表。
let
ListOfDays = {1.1,0.5,2,3,1},
DayOfTheWeek = (Day as list, HoursWorked ) =>
let
Earnings = Day * HoursWorked
in
Earnings
in
DayOfTheWeek
我希望允许用户select ListOfDays 列表中的单个值。我在我的函数参数中使用了类型列表,这样它就可以为用户提供一个下拉列表类型的选项。
如果用户能够打开查询编辑器,则他们可以从下拉列表中选择一个 Day
参数并将其自动应用于查询。
您可以从“管理参数”>“新建参数”菜单创建参数
图片右上角的drop-down是用户select的选择。
您的用户定义函数 fn_DayOfTheWeek
如下:
let
DayOfTheWeek = (Day as number, HoursWorked as number) =>
let
Earnings = Day * HoursWorked
in
Earnings
in
DayOfTheWeek
请注意,Day
是一个数字,而不是列表。您想从列表中选择,而不是将列表传递给函数。
现在您可以使用参数调用函数来实际生成结果。
let
Source = fn_DayOfTheWeek(Day, <HoursWorked value here>)
in
Source
此结果将在您更改参数时更新。
如您所见,用户是否有权访问查询编辑器是这种方法的一个关键问题。我不确定是否可以以某种方式直接在自定义连接器对话框中设置参数,但这在功能上应该是等效的。
我相信这是您正在寻找的相关文档: github.com/microsoft/DataConnectors/docs/function-docs.md: Adding Function Documentation
特别是,查看 Documentation.AllowedValues
的定义:
List of valid values for this parameter. Providing this field will change the input from a textbox to a drop down list. Note, this does not prevent a user from manually editing the query to supply alternate values.
这个(和其他 Documentation
字段)是函数参数元类型的一部分。向下滚动到显示如何使用它们的代码片段:
[DataSource.Kind="HelloWorldWithDocs", Publish="HelloWorldWithDocs.Publish"]
shared HelloWorldWithDocs.Contents = Value.ReplaceType(HelloWorldImpl, HelloWorldType);
HelloWorldType = type function (
message as (type text meta [
Documentation.FieldCaption = "Message",
Documentation.FieldDescription = "Text to display",
Documentation.SampleValues = {"Hello world", "Hola mundo"}
]),
optional count as (type number meta [
Documentation.FieldCaption = "Count",
Documentation.FieldDescription = "Number of times to repeat the message",
Documentation.AllowedValues = { 1, 2, 3 }
]))
as table meta [
Documentation.Name = "Hello - Name",
Documentation.LongDescription = "Hello - Long Description",
Documentation.Examples = {[
Description = "Returns a table with 'Hello world' repeated 2 times",
Code = "HelloWorldWithDocs.Contents(""Hello world"", 2)",
Result = "#table({""Column1""}, {{""Hello world""}, {""Hello world""}})"
],[
Description = "Another example, new message, new count!",
Code = "HelloWorldWithDocs.Contents(""Goodbye"", 1)",
Result = "#table({""Column1""}, {{""Goodbye""}})"
]}
];
HelloWorldImpl = (message as text, optional count as number) as table =>
let
_count = if (count <> null) then count else 5,
listOfMessages = List.Repeat({message}, _count),
table = Table.FromList(listOfMessages, Splitter.SplitByNothing())
in
table;
他们还提供了调用时的截图: