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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

Reply
sandeshp
Frequent Visitor

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 @sandeshp ,

 

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.










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


Proud to be a Super User!









"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 @sandeshp ,

 

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.










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


Proud to be a Super User!









"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
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.