Forum Discussion

Km93's avatar
Km93
New Member
1 year ago
Solved

Date AddMonths

Hello,

I am trying to using the dates in the left column and add the months listed in the right column to create a new column. Can someone explain what I’m doing wrong?

Thank you!

  • Hi Km93 

    The issue seems to be a missing else clause.

    In M, the syntax of an if expression must be

    if <if-condition> then <true-expression> else <false-expression>

    For example, if you want to return null when the condition is false, you could write:

    if [Contact Frequency] = 1 then Date.AddMonths([Last Contact Attempt],1) else null

    See here for more examples/discussion:

    https://bengribaudo.com/blog/2020/01/06/4844/power-query-m-primer-part14-control-structure#conditional-expressions-if

     

     

  • hi Km93 ,

    Use this corrected formula to handle the text and numeric parts in Contact Frequency:

     

    
    if Text.Contains([Contact Frequency], "mo") then
    Date.AddMonths([Last Contact Attempt], Number.FromText(Text.BeforeDelimiter([Contact Frequency], " ")))
    else
    null



    It extracts the numeric value before "mo" and applies Date.AddMonths accordingly.

5 Replies

  • Hi Km93 

    The issue seems to be a missing else clause.

    In M, the syntax of an if expression must be

    if <if-condition> then <true-expression> else <false-expression>

    For example, if you want to return null when the condition is false, you could write:

    if [Contact Frequency] = 1 then Date.AddMonths([Last Contact Attempt],1) else null

    See here for more examples/discussion:

    https://bengribaudo.com/blog/2020/01/06/4844/power-query-m-primer-part14-control-structure#conditional-expressions-if

     

     

  • hi Km93 ,

    Use this corrected formula to handle the text and numeric parts in Contact Frequency:

     

    
    if Text.Contains([Contact Frequency], "mo") then
    Date.AddMonths([Last Contact Attempt], Number.FromText(Text.BeforeDelimiter([Contact Frequency], " ")))
    else
    null



    It extracts the numeric value before "mo" and applies Date.AddMonths accordingly.

  • v-hashadapu's avatar
    v-hashadapu
    Community Support

    Hi Km93 , Hope your issue is solved. If it is, please consider marking the answer 'Accept as solution', so others with similar issues may find it easily. If it isn't, please share the details. Thank you.

  • v-hashadapu's avatar
    v-hashadapu
    Community Support

    Hi Km93, Hope your issue is solved. If it is, please consider marking the answer 'Accept as solution', so others with similar issues may find it easily. If it isn't, please share the details.
    Thank you.

  • v-hashadapu's avatar
    v-hashadapu
    Community Support

    Hi @Km93, Hope your issue is solved. If it is, please consider marking the answer 'Accept as solution', so others with similar issues may find it easily. If it isn't, please share the details.
    Thank you.