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
jonclay
Helper IV
Helper IV

IF statement with multiple DATEBETWEEN commands - new to DAX!

Hi everyone

I'm very new to DAX and I'm getting quite a muddle with my DAX code below. What I want to achieve is the FinYear column to show Pre2000 if the date shown in 'dc1'[si_transactiondateofpayments is between 01/09/1900 and 31/08/2000, then show 2001/2002 if the date is between 01/09/2001 and 31/08/2002 and so on.

I know there should be an ELSE NULL type statement in there too (for those dates that don't fall into any of the parameters), but I'm well and truely stuck with the entire statement.

 

FinYear =
IF(
DATESBETWEEN('dc1'[si_transactiondateofpayment], DATE(01/09/1900),DATE(31/08/2000),
'Pre 2000',
IF(
DATESBETWEEN('dc1'[si_transactiondateofpayment], DATE(01/09/2001),DATE(31/08/2002),
'2001/2002',NULL
)
)))
 
Any help would be much appreciated!

Thank you
Jon
1 ACCEPTED SOLUTION
Greg_Deckler
Super User
Super User

@jonclay Avoid TI functions if you can. You can do this easily with simple conditions. Also, use SWITCH TRUE instead of nested IF statements. And your DATE parameters are not correct and you want to use BLANK instead of NULL. Oh, and your quotes were wrong. Other than that, solid code. 🙂

 

 

FinYear =
  SWITCH(TRUE(),
    'dc1'[si_transactiondateofpayment] >= DATE(1900,1,9) &&
      'dc1'[si_transactiondateofpayment] <= DATE(2000,8,31),"Pre 2000",
    'dc1'[si_transactiondateofpayment] >= DATE(2001,1,9) &&
      'dc1'[si_transactiondateofpayment] <= DATE(2002,8,31),"2001/2002",
    BLANK()
  )

 

 

 

You may find this helpful - https://community.powerbi.com/t5/Community-Blog/To-bleep-With-Time-Intelligence/ba-p/1260000

Also, see if my Time Intelligence the Hard Way provides a different way of accomplishing what you are going for.

https://community.powerbi.com/t5/Quick-Measures-Gallery/Time-Intelligence-quot-The-Hard-Way-quot-TIT...


@ 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

3 REPLIES 3
jonclay
Helper IV
Helper IV

Thanks so much for helping out. This now works perfectly!

Anonymous
Not applicable

@jonclay 

 

Do not avoid TI (time-intel) functions but USE THEM IN THE RIGHT CONTEXT. Then you'll be safe.

Greg_Deckler
Super User
Super User

@jonclay Avoid TI functions if you can. You can do this easily with simple conditions. Also, use SWITCH TRUE instead of nested IF statements. And your DATE parameters are not correct and you want to use BLANK instead of NULL. Oh, and your quotes were wrong. Other than that, solid code. 🙂

 

 

FinYear =
  SWITCH(TRUE(),
    'dc1'[si_transactiondateofpayment] >= DATE(1900,1,9) &&
      'dc1'[si_transactiondateofpayment] <= DATE(2000,8,31),"Pre 2000",
    'dc1'[si_transactiondateofpayment] >= DATE(2001,1,9) &&
      'dc1'[si_transactiondateofpayment] <= DATE(2002,8,31),"2001/2002",
    BLANK()
  )

 

 

 

You may find this helpful - https://community.powerbi.com/t5/Community-Blog/To-bleep-With-Time-Intelligence/ba-p/1260000

Also, see if my Time Intelligence the Hard Way provides a different way of accomplishing what you are going for.

https://community.powerbi.com/t5/Quick-Measures-Gallery/Time-Intelligence-quot-The-Hard-Way-quot-TIT...


@ 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