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

Enhance your career with this limited time 50% discount on Fabric and Power BI exams. Ends August 31st. Request your voucher.

Reply
LUCASM
Helper IV
Helper IV

Create a date calculation

I need to create an if the else statement based on the following rules and am pulling my hair out.

This should be fairly simple so why I cant fathom it out is beyound me at the moment.

 

The rule is (assume its 20/02/2020 UK date)

If today is < 26 of the month then "26/02/2020" else add 1 month to make it "26/03/2020"

Obviously need to take into consideration the end of year so assuming its 26/12/2020

then the returned date would be "26/01/2021"

 

Can someone help me here.

 

 

 

 

 

1 ACCEPTED SOLUTION

Here is the Power Query code, same logic as the DAX code and the DAX is prettier...

 

= Table.AddColumn(#"Changed Type", "Custom", each let
  Year = Date.Year(DateTime.Date([Date])),
  Month = Date.Month(DateTime.Date([Date])),
  Day = Date.Day(DateTime.Date([Date]))
in
 if (Day >= 26) and (Month = 12) then #date(Year+1,1,26) else if (Day >= 26) then #date(Year,Month+1,26) else #date(Year,Month,26))


Follow on LinkedIn
@ 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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

7 REPLIES 7
Greg_Deckler
Community Champion
Community Champion

Try this:

 

Column = 
    VAR __Year = YEAR([Date])
    VAR __Month = MONTH([Date])
    VAR __Day = DAY([Date])
RETURN
    SWITCH(TRUE(),
        __Day>=26 && __Month=12,DATE(__Year+1,1,26),
        __Day>=26,DATE(__Year,__Month+1,26),
        DATE(__Year,__Month,26)
    )

 



Follow on LinkedIn
@ 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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler 

Thank you for this,

Is this added as a Custom Column? if so I am getting a "Token Eof expected" error

when I select show error, it highlights __Year

 

Martin

Yes and I don't get that, attached the PBIX. I did add some spaces between things. I have seen that internationally sometimes having numbers butted up against commas causes problems. If it isn't that, not sure. Seems to work for me.



Follow on LinkedIn
@ 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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Hi @Greg_Deckler 

 

Is this for Power BI or Power Query - (or both)

as you dropped in a pbix file

I need this to be added to PowerQuery (get and transform in Excel)

Maybe the two are synonymouos but as written it ios not recognised by PowerQuery in Excel as a Custom Column

Here is the Power Query code, same logic as the DAX code and the DAX is prettier...

 

= Table.AddColumn(#"Changed Type", "Custom", each let
  Year = Date.Year(DateTime.Date([Date])),
  Month = Date.Month(DateTime.Date([Date])),
  Day = Date.Day(DateTime.Date([Date]))
in
 if (Day >= 26) and (Month = 12) then #date(Year+1,1,26) else if (Day >= 26) then #date(Year,Month+1,26) else #date(Year,Month,26))


Follow on LinkedIn
@ 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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler 

Thats awesome. I now know why I didnt have a snowballs chance of resolving that on my own.

many many thanks

Guess I should have checked the forum. This is for DAX, let me see if I an whip up a Power Query version.



Follow on LinkedIn
@ 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!:
DAX For Humans

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

Check out the July 2025 Power BI update to learn about new features.