Microsoft is giving away 50,000 FREE Microsoft Certification exam vouchers. Get Fabric certified for FREE! Learn more
Hi
I have the following DAX statement that creates a calculated column:
Solved! Go to Solution.
DateTime.LocalNow and DateTime.FixedLocalNow
https://docs.microsoft.com/en-us/powerquery-m/datetime-localnow
@ImkeF probably knows about the switch statement.
While you can kind of do switch in M, for what you have if/else is good enough.
E.g.
= if [status] = "Open" and [Due Date] < DateTime.FixedLocalNow() then
"Not Paid"
else if [status] = "Open" and [Due Date] >= DateTime.FixedLocalNow() then
"To be Paid"
else
"Paid"
While you can kind of do switch in M, for what you have if/else is good enough.
E.g.
= if [status] = "Open" and [Due Date] < DateTime.FixedLocalNow() then
"Not Paid"
else if [status] = "Open" and [Due Date] >= DateTime.FixedLocalNow() then
"To be Paid"
else
"Paid"
I already did it like that, but I would be interested in knowing how to do a kind of switch function in M anyway.
Switch in M:
let
cases =
[
MyTerm1 = "My term one",
MyTerm2 = "My term two",
MyTerm3 = if [someOtherColumn] > 0 then "Yes" else "No"
]
in
try Record.Field(cases, [MyColumnValue]) otherwise "My default case"
There is also a similar one for table syntax. Also note that perf is bad if default case is hit a lot
Hi @artemus
would very much like to understand why performance only drops if the default-value is triggered.
I was doing some performance test with record lookups and found them very fast, as they evaluated lazily. But used them without the try ... otherwise - extension.
Just curious to understand why using try...otherwise wouldn't impact performance per se, but the occurrance of the "otherwise" will cause the drop.
Or did I understand this wrong?
Thanks heaps.
Imke Feldmann (The BIccountant)
If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!
How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries
Try .. otherwise uses exception handling nativly (at least it appears this way). Since an exception can handle several levels deep, going back up the stack isn't performant.
Thought about this a bit more, this perf issue can be avoided by using Record.FieldOrDefault function.
Thanks @artemus for the alternative solution.
Not sure how to interpret the "levels" in your explanation: We're dealing with a record here that (at least for my tests) evaluates lazily: Just evaluates the field that matches the condition/lookup. Where are there any levels? Doesn't it return just the one field from the record?
Imke Feldmann (The BIccountant)
If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!
How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries
try .. otherwise supports multiple levels.
E.g.
try
let
factorialSkip = (n) => if n > 0 then n * @factorialSkip(n - 2) else if n = 1 then error "Odd numbers not allowed" else 1
in
factorialSkip(9)
In this case, factorialSkip is called with 9, then 7, then 5, then 3, then 1, which results in an error.
DateTime.LocalNow and DateTime.FixedLocalNow
https://docs.microsoft.com/en-us/powerquery-m/datetime-localnow
@ImkeF probably knows about the switch statement.
Check out the April 2025 Power BI update to learn about new features.
Explore and share Fabric Notebooks to boost Power BI insights in the new community notebooks gallery.
User | Count |
---|---|
20 | |
11 | |
10 | |
7 | |
7 |