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
wi11iamr
Advocate II
Advocate II

Create DateKey, but with only one entry per month

Hi there,

 

Using the Calendar function I can happily create datekey tables which have anentry for each day of a month.

I however need to create a datekey that only has the 1st day of each month, such as:

2017-01-01

2017-02-01

2017-03-01

...

 

instead of 

2017-01-01

2017-01-02

2017-01-03

2017-01-04

...

 

I suspect a custom function may be required to achieve this, however I'm at a bit of a loss in trying to achieve this.

1 ACCEPTED SOLUTION
v-ljerr-msft
Employee
Employee

Hi @wi11iamr,

 

In addition to Pfister's solution, you can also use STARTOFMONTH Function (DAX) to:

 

Add a new calculate column to your existing Calendar table.

First Day of Month = STARTOFMONTH('Date'[Date])

Or you can create a new table with the new Datekey column from your existing Calendar table.

Table = 
DISTINCT (
    SELECTCOLUMNS ( 'Date', "NewDateKey", STARTOFMONTH ( 'Date'[Date] ) )
)

t1.PNG

 

Regards

View solution in original post

3 REPLIES 3
v-ljerr-msft
Employee
Employee

Hi @wi11iamr,

 

In addition to Pfister's solution, you can also use STARTOFMONTH Function (DAX) to:

 

Add a new calculate column to your existing Calendar table.

First Day of Month = STARTOFMONTH('Date'[Date])

Or you can create a new table with the new Datekey column from your existing Calendar table.

Table = 
DISTINCT (
    SELECTCOLUMNS ( 'Date', "NewDateKey", STARTOFMONTH ( 'Date'[Date] ) )
)

t1.PNG

 

Regards

Thanks @Anonymous and @v-ljerr-msft.

The StartOfMonth function put me on the right track and I liked the possible conciseness of using the "DISTINCT (SelectColumns..." approach, however while struggling with this I came to the realisation that creating the calendar table manually, as opposed to having it as a Query, introduced a new problem in that I'm not able to join a data query with a data table.

 

I since pursued an option to create my Calendar data table as a query instead, and came across a great function from DataBear to create a dynamic data query.

Power BI Tip : Dynamic Calendar Table (Power Query)

 

 

 

 

 

Anonymous
Not applicable

Hi @wi11iamr

 

Use this formula in Power Query:

 

 #"Added Custom" = Table.AddColumn(<YOUR LAST STEP>, "DateKey", each Date.StartOfMonth([YOUR_COLUMN]))

 

 

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.