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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
FZOU
Helper IV
Helper IV

Dax Soustract DATE

Hello,

I need your help guys to achieve a formula.

 

I have a column that containt "Year-Month" for exp :

YearMonth

2019-01

2019-02

2019-03

N..

 

I have another column that contains number of monthsf for exp :

 

MonthN

10 

15 

N..

 

The demanded task is to have the difference between the two columns for exp : 

 

"2019-10" - 9 = "2019-01"

 

Anyone have an idea how to do it please !

 

1 ACCEPTED SOLUTION

Hi @FZOU ,

Can you change the data type of column "YearMonth" from Text to Date? If yes, maybe you could try this:

Column =
DATE ( YEAR ( 'Table'[YearMonth] ), MONTH ( 'Table'[YearMonth] ) - 'Table'[MonthN], DAY ( 'Table'[YearMonth] ) )

And then format the new column as "yyyy-mm".

3.PNG 

Best Regards,
Xue Ding
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

9 REPLIES 9
amitchandak
Super User
Super User

@FZOU 

Try like

format(date(year(left([month-year]),4),month(right([month-year]),2)-9,1),"YYYY-MM")

 

Here -9 is the number you want to subtract

 

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

@amitchandak  it's impssible to format text as date !

Hi @FZOU ,

Can you change the data type of column "YearMonth" from Text to Date? If yes, maybe you could try this:

Column =
DATE ( YEAR ( 'Table'[YearMonth] ), MONTH ( 'Table'[YearMonth] ) - 'Table'[MonthN], DAY ( 'Table'[YearMonth] ) )

And then format the new column as "yyyy-mm".

3.PNG 

Best Regards,
Xue Ding
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

@v-xuding-msftThank you for your help,it's work perfectly 🙂

Try like

format(date(year(left([month-year]),4),month(mid([month-year],search("-",[month-year],1,0)+1,len([month-year])))-9,1),"YYYY-MM")

 

To get the correct value of the month please check this separately - mid([month-year],search("-",[month-year],1,0)+1,len([month-year]))

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here
Greg_Deckler
Super User
Super User

Perhaps:

 

Column =
  VAR __YearMonth = VALUE(SUBSTITUTE([YearMonth],"-",""))
  VAR __NewYearMonth = (__YearMonth - [MonthN]) & ""
RETURN
  LEFT(__NewYearMonth,4) & "-" & RIGHT(__NewYearMonth,2)


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!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Decklerthank you for your response but i had this result 

 

NewYearMonth.PNG

 

Right, forgot the padded zero, try this one:

 

Column =
  VAR __YearMonth = VALUE(SUBSTITUTE([YearMonth],"-",""))
  VAR __NewYearMonth = (__YearMonth - [MonthN]) & ""
RETURN
  LEFT(__NewYearMonth,4) & "-" & FORMAT(RIGHT(__NewYearMonth,2),"0#")

 

Might be ## or 0#, one of them or both give a padded right-zero. 



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!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckleri keep get the same wrong result

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.