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
Npg3
New Member

Power BI time functions

Hi, 

 

I want to create a function in power bi to creates a new colum and adds a certain amount of time depending on a key word that is a period of time in a different column. 

example:

row 1  includes a date in column A and word in column B ( a word like ATO is 6 months) so when row 1 column B is ATO it adds 6 months onto row 1 column A and the new date is placed in new row 1 column C.

 

basically the amount of time added onto the row in column A is dependent on word in the corresponding row of column B

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @Npg3 ,

 

You can try M Code as below.

Date.AddMonths(
    [Column A],
    if [Column B] = "AOT" then 6 
    else if [Column B] = "XYZ" then 3 
    else if [Column B] = "ABC" then 1 
    else null)

Result is as below.

vrzhoumsft_0-1691475532915.png

 

Best Regards,
Rico Zhou

 

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

7 REPLIES 7
tamerj1
Super User
Super User

Hi @Npg3 

It depends. If not too many text variants and if only number of months to be added then you may try

Column C =
EDATE (
'Table'[Column A],
SWITCH ( 'Table'[Column B], "AOT", 6, "XYZ", 3, "ABC", 1 )
)

It shows expression.error: the name 'EDATE' wasn't recognized. Make sure it's spelled right

@Npg3 

The code is for DAX calculated column not Power Query M language. 

How can I create this function in a custom column in power BI?

Anonymous
Not applicable

Hi @Npg3 ,

 

You can try M Code as below.

Date.AddMonths(
    [Column A],
    if [Column B] = "AOT" then 6 
    else if [Column B] = "XYZ" then 3 
    else if [Column B] = "ABC" then 1 
    else null)

Result is as below.

vrzhoumsft_0-1691475532915.png

 

Best Regards,
Rico Zhou

 

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

This works but the problem is if I want column B to have AOT and ABC in one row it does not work. The way the data is pulled in is in some rows of columb B they have multiple (codes). How do I have power bi choose the code with the lowest timeframe. I tried using LOOKUPVALUE but it will not recognize LOOKUPVALUE people have been using power querey for that function which I do not have. Any advice?

@Npg3 

You may insert conditional column. Should be straightforward. 

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.