结合 2 个自定义 M 函数来解析文本和分类
Combining 2 custom M functions to parse text and classify
作为 PowerQuery 数据加载的一部分,我想添加一个字段,将一些数据分类为广泛的组。在这种情况下,要分类的数据是文本,但以索引号开头,例如“01 Oranges”、“02 Pears”、“06 Cabbages”、“08 Carrots”。该函数需要将索引 1-5 分组为 "Fruits",将 6+ 分组为 "Vegetables"。虽然我可以通过单独的查找 table 和合并来做到这一点,但函数提供了更大的灵活性。
问题有两个部分; (1) 提取索引和 (2) 制作列表。我可以创建函数来分别执行这些操作,但还没有想出如何将它们组合成一个。两个独立的函数如下所示。
// (1) this function takes the index value of the input text (and has place holders for the second function
let
class = (input) =>
// find the index
let
index = Number.FromText(Text.Start(input, 2))
in
index
// depending on the index, allocate the class
in class
// (2) this function allocates the index to a class
(input) =>
// depending on the index, allocate the class
let band =
{
{(x)=> x <= 5, "Fruit"},
{(x)=> x > 5, "Vegetable"}
},
result = List.First(List.Select(band, each _{0}(input))){1}
in
result
当我尝试将两者放在一起时,出现了 Eof 和缺少逗号的错误。
// (1) this bit takes the index value of the input text
let
class = (input) =>
// find the index
let
index = Number.FromText(Text.Start(input, 2))
in
index
// depending on the index, allocate the class
// (2) this bit allocates it to a class
class =>
// depending on the index, allocate the class
let band =
{
{(x)=> x <= 5, "Fruit"},
{(x)=> x > 5, "Vegetable"}
},
result = List.First(List.Select(band, each _{0}(input))){1}
in
result
in class
非常感谢您对像这样组合函数的见解。
此代码应该有效:
(input) =>
let band =
{
{Number.FromText(Text.Start(input, 2)) <= 5, "Fruit"},
{Number.FromText(Text.Start(input, 2)) > 5, "Vegetable"}
},
result = List.Select(band, each _{0}){0}{1}
in
result
作为 PowerQuery 数据加载的一部分,我想添加一个字段,将一些数据分类为广泛的组。在这种情况下,要分类的数据是文本,但以索引号开头,例如“01 Oranges”、“02 Pears”、“06 Cabbages”、“08 Carrots”。该函数需要将索引 1-5 分组为 "Fruits",将 6+ 分组为 "Vegetables"。虽然我可以通过单独的查找 table 和合并来做到这一点,但函数提供了更大的灵活性。
问题有两个部分; (1) 提取索引和 (2) 制作列表。我可以创建函数来分别执行这些操作,但还没有想出如何将它们组合成一个。两个独立的函数如下所示。
// (1) this function takes the index value of the input text (and has place holders for the second function
let
class = (input) =>
// find the index
let
index = Number.FromText(Text.Start(input, 2))
in
index
// depending on the index, allocate the class
in class
// (2) this function allocates the index to a class
(input) =>
// depending on the index, allocate the class
let band =
{
{(x)=> x <= 5, "Fruit"},
{(x)=> x > 5, "Vegetable"}
},
result = List.First(List.Select(band, each _{0}(input))){1}
in
result
当我尝试将两者放在一起时,出现了 Eof 和缺少逗号的错误。
// (1) this bit takes the index value of the input text
let
class = (input) =>
// find the index
let
index = Number.FromText(Text.Start(input, 2))
in
index
// depending on the index, allocate the class
// (2) this bit allocates it to a class
class =>
// depending on the index, allocate the class
let band =
{
{(x)=> x <= 5, "Fruit"},
{(x)=> x > 5, "Vegetable"}
},
result = List.First(List.Select(band, each _{0}(input))){1}
in
result
in class
非常感谢您对像这样组合函数的见解。
此代码应该有效:
(input) =>
let band =
{
{Number.FromText(Text.Start(input, 2)) <= 5, "Fruit"},
{Number.FromText(Text.Start(input, 2)) > 5, "Vegetable"}
},
result = List.Select(band, each _{0}){0}{1}
in
result