Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Vote for your favorite vizzies from the Power BI Dataviz World Championship submissions. Vote now!

Reply
Anonymous
Not applicable

Extract month name from a date-range

I have a "Memberships" table which contains this data:

 

Membership start date.          Membership end date          Membership type
2024-01-152024-02-28Type-1
2024-02-032024-03-20Type-2
2023-10-21nullType-3

 

I want to create a bar chart that shows how many members had a valid membership each month, over a given date range.

The date-range will be used as a page-level filter. (I am using PowerBI embedded, so this is necessary)

 

The membership is valid in a given month if:

 

 

"Membership start date" <= "date range end"
AND
(
    "Membership end date" IS NULL
    OR
    "Membership end date" >= "Membership start date"
)

 

 

So if I pass the date range as: (2023-12-01 to 2024-04-30), the resulting data should be:

 

Membership start date            Membership end date           Membership type           Month Name       
2024-01-152024-02-28Type-1Jan-24
2024-01-152024-02-28Type-1Feb-24
2024-02-032024-03-20Type-2Feb-24
2024-02-032024-03-20Type-2Mar-24
2023-10-21 Type-3Dec-23
2023-10-21 Type-3Jan-24
2023-10-21 Type-3Feb-24
2023-10-21 Type-3Mar-24
2023-10-21 Type-3Apr-24

 

(Notice that the month names "Oct-23" and "Nov-23" should not be there since the passed date range starts from 2023-12-01)

 

I am thinking of creating a calendar table and CROSSJOIN it with the "Memberships" table, and filter the data afterwards, but that would create a huge new table in Power BI(Actual "Memberships" table would contain more than 250k rows).
I am also concerned about how it would refresh the data if the data from "Memberships" table refreshes.

 

How do I achieve this in Power BI?

(PowerBI file with the CROSSJOIN and FILTER approach: dropbox link)

1 ACCEPTED SOLUTION
danextian
Super User
Super User

Hi @Anonymous ,

 

You can try the sample M code below.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjTVNzDUNzIwMlHSUTKy0DcwgnFCKgtSdQ2VYnWilQyMkcSNDPSBfGRFRmBFRob6hgYgcWOgeF5pTg5M2lgpNhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Membership start date" = _t, #"Membership end date" = _t, #"Membership type" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Membership start date", type date}, {"Membership end date", type date}, {"Membership type", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Dates + One Month", each let 
start = [Membership start date],
end = if [Membership end date] = null then Date.From(DateTime.LocalNow()) else [Membership end date],
Diff = Number.Round(Number.From((end - start)/( 365.25 / 12 )) ,0 )
in List.Transform({0..Diff}, each Date.AddMonths(start, _))),
    #"Expanded Dates + One Month" = Table.ExpandListColumn(#"Added Custom", "Dates + One Month"),
    #"Added Custom1" = Table.AddColumn(#"Expanded Dates + One Month", "Period", each Date.ToText([#"Dates + One Month"], "MMM-yy")),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom1",{{"Dates + One Month", type date}, {"Period", type text}})
in
    #"Changed Type1"

danextian_0-1712586498686.png

You can then filter the query based on your desired date range.





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

View solution in original post

1 REPLY 1
danextian
Super User
Super User

Hi @Anonymous ,

 

You can try the sample M code below.

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjTVNzDUNzIwMlHSUTKy0DcwgnFCKgtSdQ2VYnWilQyMkcSNDPSBfGRFRmBFRob6hgYgcWOgeF5pTg5M2lgpNhYA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [#"Membership start date" = _t, #"Membership end date" = _t, #"Membership type" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Membership start date", type date}, {"Membership end date", type date}, {"Membership type", type text}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Dates + One Month", each let 
start = [Membership start date],
end = if [Membership end date] = null then Date.From(DateTime.LocalNow()) else [Membership end date],
Diff = Number.Round(Number.From((end - start)/( 365.25 / 12 )) ,0 )
in List.Transform({0..Diff}, each Date.AddMonths(start, _))),
    #"Expanded Dates + One Month" = Table.ExpandListColumn(#"Added Custom", "Dates + One Month"),
    #"Added Custom1" = Table.AddColumn(#"Expanded Dates + One Month", "Period", each Date.ToText([#"Dates + One Month"], "MMM-yy")),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom1",{{"Dates + One Month", type date}, {"Period", type text}})
in
    #"Changed Type1"

danextian_0-1712586498686.png

You can then filter the query based on your desired date range.





Dane Belarmino | Microsoft MVP | Proud to be a Super User!

Did I answer your question? Mark my post as a solution!


"Tell me and I’ll forget; show me and I may remember; involve me and I’ll understand."
Need Power BI consultation, get in touch with me on LinkedIn or hire me on UpWork.
Learn with me on YouTube @DAXJutsu or follow my page on Facebook @DAXJutsuPBI.

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

Vote for your favorite vizzies from the Power BI World Championship submissions!

Sticker Challenge 2026 Carousel

Join our Community Sticker Challenge 2026

If you love stickers, then you will definitely want to check out our Community Sticker Challenge!

January Power BI Update Carousel

Power BI Monthly Update - January 2026

Check out the January 2026 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.