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
amandabus21
Helper V
Helper V

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*

amandabus21_0-1681766665229.png

 

3 ACCEPTED SOLUTIONS
m_dekorte
Super User
Super User

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

View solution in original post

v-stephen-msft
Community Support
Community Support

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

vstephenmsft_0-1682044156485.png

vstephenmsft_1-1682044165502.png

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.           

View solution in original post

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.

 

m_dekorte_0-1684443319929.png

 

View solution in original post

9 REPLIES 9
v-stephen-msft
Community Support
Community Support

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

vstephenmsft_0-1682044156485.png

vstephenmsft_1-1682044165502.png

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.           

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.

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 )

 

 

Thanks! Kind of extenidng this here but do you know if I can modify so that if the the previous month is not complete, it wouldnt count as 1 month. 

Todays Date 5/10/23

 

For example with a Calenday day of 5/1/23 and greater it would give me a 0.

 

a calendar day of 4/1/23 - 4/30/23 would still give me a 0

(because since it hasnt been a full month from todays date, the whole month is technically not complete)

 

a calendar day of 3/31/23 would give me a 1

 

a calendar day of 2/28/23 would give me a 2 etc

Hi @amandabus21 

 

You can see if this meets your requirement

let
    fxMonthsDif = (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,
    Source = Table.FromColumns(
        {{#date( 2023, 5, 1), #date( 2023, 4, 1), #date( 2023, 3, 30), #date( 2023, 2, 28)}},
        type table [ Date = date ]
    ),
    InvokedFunction = Table.AddColumn(Source, "Months Dif", each fxMonthsDif([Date]), Int64.Type)
in
    InvokedFunction

 

Today  May 5th 2023 it returns this

m_dekorte_0-1683754890274.png

Ps. If this helps solve your query please mark this post as Solution, thanks!

hi yes, this does work but how can I add to my original query so that it automates the dates without me having to hard code the dates?

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.

 

m_dekorte_0-1684443319929.png

 

this is perfect, thank you so much!!!!

m_dekorte
Super User
Super User

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

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