As part of a PowerQuery data load, I would like to add a field that classifies some data into broad groups. The data to be classified, in this case, is text, but starts with a index number, such as "01 Oranges", "02 Pears", "06 Cabbages", "08 Carrots". The function needs to group the indexes say 1-5 into "Fruits" and 6+ into "Vegetables". While I could do this with a separate look-up table and merge, a function offers more flexibility.
The problem has two parts; (1) extracting the index and (2) making the list. I can create functions to do these separately, but have not figured out how to combine them into one. The two separate functions are shown below.
// (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
When I try to put the two together, I get a Eof and comma-missing errors.
// (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
Very grateful for your insight into combining functions like this.
This code should work:
(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