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
im_newbie_123
Frequent Visitor

Create a custom column to get month value

I have two columns (Hire Date and Current Date)

I have to find date difference between the two columns using the formula :

(Current Date - Hire Date)/12 = Tenure (new column) 

 

Example :

Current Date 

10/23/2019

 

Hire Date 

10/23/2018

 

Tenure

1.0

1 ACCEPTED SOLUTION
v-frfei-msft
Community Support
Community Support

Hi @im_newbie_123 ,

 

To create a custom column as below.

((((Date.Year([Current Date]) - Date.Year([#"Hire Date "]))-1)*12)+(12-Date.Month([#"Hire Date "]))+(Date.Month([Current Date])))/12

Also we can make it dynamicly by using DateTime.LocalNow().

((((Date.Year(DateTime.LocalNow()) - Date.Year([#"Hire Date "]))-1)*12)+(12-Date.Month([#"Hire Date "]))+(Date.Month(DateTime.LocalNow())))/12

Full code for your reference as well.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwtNQ3NNA3MlHSAXEsoJzYWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Current Date" = _t, #"Hire Date " = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Current Date", type date}, {"Hire Date ", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each ((((Date.Year(DateTime.LocalNow()) - Date.Year([#"Hire Date "]))-1)*12)+(12-Date.Month([#"Hire Date "]))+(Date.Month(DateTime.LocalNow())))/12),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.1", each ((((Date.Year([Current Date]) - Date.Year([#"Hire Date "]))-1)*12)+(12-Date.Month([#"Hire Date "]))+(Date.Month([Current Date])))/12)
in
    #"Added Custom1"

full.PNG

 

 

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.

View solution in original post

2 REPLIES 2
v-frfei-msft
Community Support
Community Support

Hi @im_newbie_123 ,

 

To create a custom column as below.

((((Date.Year([Current Date]) - Date.Year([#"Hire Date "]))-1)*12)+(12-Date.Month([#"Hire Date "]))+(Date.Month([Current Date])))/12

Also we can make it dynamicly by using DateTime.LocalNow().

((((Date.Year(DateTime.LocalNow()) - Date.Year([#"Hire Date "]))-1)*12)+(12-Date.Month([#"Hire Date "]))+(Date.Month(DateTime.LocalNow())))/12

Full code for your reference as well.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjIwtNQ3NNA3MlHSAXEsoJzYWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"Current Date" = _t, #"Hire Date " = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Current Date", type date}, {"Hire Date ", type date}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each ((((Date.Year(DateTime.LocalNow()) - Date.Year([#"Hire Date "]))-1)*12)+(12-Date.Month([#"Hire Date "]))+(Date.Month(DateTime.LocalNow())))/12),
    #"Added Custom1" = Table.AddColumn(#"Added Custom", "Custom.1", each ((((Date.Year([Current Date]) - Date.Year([#"Hire Date "]))-1)*12)+(12-Date.Month([#"Hire Date "]))+(Date.Month([Current Date])))/12)
in
    #"Added Custom1"

full.PNG

 

 

Community Support Team _ Frank
If this post helps, then please consider Accept it as the solution to help the others find it more quickly.
Nathaniel_C
Super User
Super User

Hi @im_newbie_123 

 

In Power Query = Table.AddColumn(#"Changed Type", "Custom", each ([Current Date]-[Hire Date])/365)
Go to Power Query, Select Add Column, Custom Column and fill in the code.
Let me know if you have any questions.

If this solves your issues, please mark it as the solution, so that others can find it easily. Kudos are nice too.
Nathaniel
date in m.PNG

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




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