Forum Discussion
Anonymous
5 years agoNot applicable
IF with multiple statements in M language
Hi, in Power Query, by a custom M code, is it possible to use IF with more statements for a satisfied condition? In another terms, is it possibile to have a block of instructions to run for an IF, d...
Anonymous
5 years agoNot applicable
Which other details do you need?
I asked if it is possible to write in M language a such IF with a statement block:
IF <condition>
BEGIN
<statement 1>
<statement 2>
<statement 3>
...
<statement n>
END
In other programming language this is possible (e.g. C, C++, T-SQL, etc.).
Thanks
Anonymous
5 years agoNot applicable
Yes. Here's a silly example.
let
examples = { 1, 1.34, "foo", #date(2021, 1, 1), null },
detectTypes = List.Transform(
examples,
(item) =>
if item is text then "text"
else if item is date then
DateTime.LocalNow() - item
else if item is null then "Bad News"
else if item is number then item + 1.4
else error Error.Record(
"UnhandledTypeException",
"There is no handler for this type",
Value.Type(item)
)
),
#"Converted to Table" = Table.FromList(
detectTypes, Splitter.SplitByNothing(),
type table[Value = any], null, ExtraValues.Error)
in
#"Converted to Table"
If you have an example with names, I could give a better suggestion.
To give you an idea, think about this. The dependency chain of the final step, "joinedNames" variable never refers to statement1 or statement2.
This means statement1 an statement2 are never executed.
let
names = {"Ted", "Jack"},
statement1 = somethingExpensive(),
statement2 = somethingElseExpensive(),
joinedNames = Text.Combine(names, ", " )
in
joinedNamesAlso errors propagate untill you catch them. this means you don't have to not run certain logic if it's already thrown an exception.