Forum Discussion
Variables in if statement
- 3 years ago
Hi iainh,
I know what you mean :). M is a "flow" language, which is quite different to VBA, which is "transactional".
Therefore, you are programming transitions rather than states as in a more traditional languages.
I think, this is quite important to bear in mind this (seemgly simple) difference. For me, comming from VB + C# this was quite a change in paradigm.
In your case, this is enough to see each conditions as a separate function (kind of a delegate in C# if you wish). Therefore, it is possible to record it as return "Actual Start is a future date" if condition1 is met, combine with return "Task Ends before it begins" if condition 2 is met:
#"Added custom" = Table.AddColumn(#"Changed column type", "Error Checking", each Text.Combine( { if [Task Actual Start Date] <> null and [Task Actual Start Date] > Date.From(DateTime.LocalNow()) then "Actual Start is a future date" else "", if ([Task Actual Finish Date] <> null and [Task Actual Start Date] <> null) and [Task Actual Finish Date] < [Task Actual Start Date] then "Task Ends before it begins" else "" }, " AND " )I do not test the above, but I guess even if this does not work straight from the source, you will be able to see the idea and tweak it to make it work.
Cheers,
John
Hi iainh,
I know what you mean :). M is a "flow" language, which is quite different to VBA, which is "transactional".
Therefore, you are programming transitions rather than states as in a more traditional languages.
I think, this is quite important to bear in mind this (seemgly simple) difference. For me, comming from VB + C# this was quite a change in paradigm.
In your case, this is enough to see each conditions as a separate function (kind of a delegate in C# if you wish). Therefore, it is possible to record it as return "Actual Start is a future date" if condition1 is met, combine with return "Task Ends before it begins" if condition 2 is met:
#"Added custom" =
Table.AddColumn(#"Changed column type", "Error Checking",
each
Text.Combine(
{
if [Task Actual Start Date] <> null and [Task Actual Start Date] > Date.From(DateTime.LocalNow()) then "Actual Start is a future date" else "",
if ([Task Actual Finish Date] <> null and [Task Actual Start Date] <> null) and [Task Actual Finish Date] < [Task Actual Start Date] then "Task Ends before it begins" else ""
},
" AND "
) I do not test the above, but I guess even if this does not work straight from the source, you will be able to see the idea and tweak it to make it work.
Cheers,
John
- iainh3 years agoFrequent Visitor
Hi John
Your right. I've never consider what type of language M is before. I've only seen it as the natrual succesor (for my purposes at least!) to VBA where transforming data sets for reporting. Thank you for the suggestion, and to other that have viewed this post. I'll have a go at using the code today and see if I can expand my knowledge somewhat!
Thanks again
Iain