Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Switching from DAX to M for nested IFs and TODAY condition

Hi

I have the following DAX statement that creates a calculated column:

Paid =
SWITCH(
TRUE(),
'Dataset'[status] = "Open" && 'Dataset'[Due Date] < TODAY(), "Not Paid",
'Dataset'[status] = "Open" && 'Dataset'[Due Date] >= TODAY(), "To be Paid",
"Paid"
)

I would like to move this calculation to M. I have two questions:

1- What is the function in M that returns the current day?
2-Is there a way to perform multiple conditions with a switch function in M? Or do I have to write every if-else for each condition?

Thanks
2 ACCEPTED SOLUTIONS
Greg_Deckler
Super User
Super User

DateTime.LocalNow and DateTime.FixedLocalNow

 

https://docs.microsoft.com/en-us/powerquery-m/datetime-localnow

 

@ImkeF probably knows about the switch statement.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

artemus
Employee
Employee

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"

View solution in original post

8 REPLIES 8
artemus
Employee
Employee

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"
Anonymous
Not applicable

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.

Greg_Deckler
Super User
Super User

DateTime.LocalNow and DateTime.FixedLocalNow

 

https://docs.microsoft.com/en-us/powerquery-m/datetime-localnow

 

@ImkeF probably knows about the switch statement.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors