Forum Discussion

amandabus21's avatar
amandabus21
Helper V
3 years ago
Solved

Months Between

How can I find the months between the Calendar Day and today. 

 

This should be for each Material.Material level  01 key.

 

*Power Query Preferred*

 

  • Hi amandabus21 ,

     

    You can try this method to calculate month diff between two dates.

    Copy the full script into a new blank query.

     

    let
      CalendarDayExample = #date(2015, 12, 22), 
      Today = Date.From(DateTime.FixedLocalNow()),
      Result = (12 * Date.Year(Today) + Date.Month(Today)) - ((12 * Date.Year(CalendarDayExample )) + Date.Month(CalendarDayExample ))
    in
      Result

     

    I hope this is helpful

  • Anonymous's avatar
    Anonymous
    3 years ago

    Hi amandabus21 ,

     

    Did you want to calculated the number of the months between calendar day and current day?

    You can try adding a custom column in Power Query.

    = Duration.Days(Date.From(DateTime.LocalNow())-[Calendar Day])/30

    If you don't want to get a value with decimals, you can also adjust to an integer type.

     

    Best Regards,

    Stephen Tao

     

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

  • m_dekorte's avatar
    m_dekorte
    3 years ago

    Hi amandabus21,

     

    Sure just copy this bit into a new blank query

    (Date as date) as number =>
        let
            Today = Date.From(DateTime.FixedLocalNow()),
            Calc = (12 * Date.Year(Today) + Date.Month(Today)) - ((12 * Date.Year(Date)) + Date.Month(Date)) -1,
            Result = if Calc < 0 then 0 else Calc
        in
            Result

     

    Rename this query: fxMonthsDif

    Now select the query you want to invoke it on, go to the "Add Column" tab on the ribbon and select "Invoke Custom Function" 

     

    In the dialog box, enter a new column name, select the function query and select the Date column you want to invoke it on.

     

     

9 Replies

  • m_dekorte's avatar
    m_dekorte
    Resident Rockstar

    Hi amandabus21 ,

     

    You can try this method to calculate month diff between two dates.

    Copy the full script into a new blank query.

     

    let
      CalendarDayExample = #date(2015, 12, 22), 
      Today = Date.From(DateTime.FixedLocalNow()),
      Result = (12 * Date.Year(Today) + Date.Month(Today)) - ((12 * Date.Year(CalendarDayExample )) + Date.Month(CalendarDayExample ))
    in
      Result

     

    I hope this is helpful

  • Anonymous's avatar
    Anonymous
    Not applicable

    Hi amandabus21 ,

     

    Did you want to calculated the number of the months between calendar day and current day?

    You can try adding a custom column in Power Query.

    = Duration.Days(Date.From(DateTime.LocalNow())-[Calendar Day])/30

    If you don't want to get a value with decimals, you can also adjust to an integer type.

     

    Best Regards,

    Stephen Tao

     

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

    • amandabus21's avatar
      amandabus21
      Helper V

      Is there a way I can make it only round down unless its the next full number?

       

      so 0.97 would be "0"

       

      but 1.97 would be "1"  etc.

      • m_dekorte's avatar
        m_dekorte
        Resident Rockstar

        Hi amandabus21 

         

        You can use Number.RoundDown

        Number.RoundDown(number as nullable number, optional digits as nullable number) as nullable number

        Returns the result of rounding number down to the previous highest integer. If number is null, this function returns null. If digits is provided, number is rounded to the specified number of decimal digits.

         

        Just wrap it around the calculation, like so:

         

        Number.RoundDown( Duration.Days(Date.From(DateTime.LocalNow())-[Calendar Day])/30 )