问题
I want to write a Function for cleaning numbers. While leaving them as text.
So, in my queries I want to use e.g.:
= CleanRN(PrevQueryTable, {"NumericTextColumn"})
The function is supposed to remove all non numeric characters and leading zeroes in not already numeric texts. (But the function is not the point for this question.)
So in the queries themselves I can go with
= Table.ReplaceValue(Benutzerdefiniert2,"_","",Replacer.ReplaceText,{"ONKZ/RufNr"})
or
= Table.ReplaceValue(#"Ersetzter Wert1", each try Number.From([RUFNUMMER]) otherwise true, each Text.TrimStart(Text.Select([ZMM_onkz], {"0".."9"}),"0")) ,Replacer.ReplaceValue,{"RUFNUMMER"})
or something similar.
But I want to wrap in a function for reusability and better reading. (And a better understanding of M)
My first go:
(Tbl_InputTable as table, Txt_Column as text) as table => let
Result = Table.ReplaceValue(
Tbl_InputTable,
each try Number.From(Table.Column(Tbl_InputTable, Txt_Column)) otherwise true,
each Text.TrimStart(Text.Select(Table.Column(Tbl_InputTable, Txt_Column), {"0".."9"}),"0"),
Replacer.ReplaceValue,
{Txt_Column}
)
in
Result
This didn't work for the now known reason that [col] and Table.Column() are not equal.
So I go again:
= (Tbl_InputTable as table, Lst_Column as list) as table => let
Result = Table.TransformColumns(
Tbl_InputTable,
List.Transform(
Lst_Column,
each {_, Text.TrimStart(Text.Select(_, {"0".."9"}), "0")}
)
)
in
Result
which fails with a
Fehler in der Abfrage ''. Expression.Error: Der Wert """" kann nicht in den Typ "Function" konvertiert werden.
Which is kinda fine and not exactly a surprise - I assume - as _ is refering to the elements of Lst_Column.
UPDATE: 3rd attempt
(Tbl_InputTable as table, Lst_Column as list) as table => let
Result = Table.TransformColumns(
Tbl_InputTable,
List.Transform(
Lst_Column,
each {
_,
Table.TransformColumns(
Tbl_InputTable,
{
_,
each Text.TrimStart(Text.Select(_, {"0".."9"}),"0")
}
)
}
)
)
in
Result
So, I hoped by wrapping it in a second Table.TransformColumns to go one layer deeper so that _ refers to the elements of the column. But no.
Nondescriptive Error:
Fehler in der Abfrage ''. Expression.Error: Ein Wert vom Typ "Table" kann nicht in den Typ "Function" konvertiert werden.
So.. How do I use functions within functions? How can I refer to a column as [column] if I only have the "column"?
回答1:
You're actually very close. The only bit you are missing is that in your List.Transform
you want the second element to be a function, not the extracted numbers in your column name. To fix this, just add an each
so it's telling how to transform each value rather than what that value is.
(Tbl_InputTable as table, Lst_Column as list) as table => let
Result = Table.TransformColumns(
Tbl_InputTable,
List.Transform(
Lst_Column,
each {_, each Text.TrimStart(Text.Select(_, {"0".."9"}), "0")}
)
)
in
Result
With this function defined, you should be able to call it like this:
CleanRN(PrevQueryTable, {"NumericTextColumn"})
Edit:
each ... is a function that transforms _
into whatever you specify (equivalent to syntax (_) => ...
, a function with parameter _
). In this case, we have a function within a function. The outer one is transforming each _
into a list {_, function}
and the inner one is specifying what that function is. The reason we want this list is more clear from the question you linked that I answered previously.
来源:https://stackoverflow.com/questions/60889219/how-can-i-loop-trough-elements-in-columns-in-a-powerbi-function