Power Query function to count number of months between two dates

Power Query has a built-in Duration.Days function. But what about Duration.Months?

Why don’t we create a custom function for that?

(Date1 as date , Date2 as date) =>
let
    Source =
        List.Generate(
            () => [x = 1, y = Date.AddMonths(Date1,x)],
            each [y] <= Date2,
            each [x = [x] + 1, y = Date.AddMonths(Date1,x)],
            each [y]
        ),
    Months = List.Count(Source)
in
    Months

Note: Date1 should be earlier than Date2.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top