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
DSwezey
Helper III
Helper III

Create Date column from Year and Month columns

I have a table with a column of Month NUmber and a column for Year. I want to make a date column for the 1st of the month. The new column I want to be formatted [Month Number]/1/[Year] 

Example: the second row is February 2021. I want the new date column to look like 2/1/2021.

 

DSwezey_0-1637781833540.png

 

1 ACCEPTED SOLUTION
smpa01
Super User
Super User

@DSwezey  this was to be done in PQ.

For DAX use this 

Column = DATE('Table'[Year],'Table'[Month Number],1)
Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

View solution in original post

10 REPLIES 10
smpa01
Super User
Super User

@DSwezey  this was to be done in PQ.

For DAX use this 

Column = DATE('Table'[Year],'Table'[Month Number],1)
Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Here for the last date or maximum date, I want the last day of month instead of first day, eg if my data lies between Jan 2023 to Jan 2024, I want all records between Jan 2023 to Dec 2023 to show date for first day of month(MMM/01/YYYY) except the records having date in Jan 2024 which I want to show as last day of month(01/31/2024).

Hi, last day of month you can achieve this way:

Date.EndOfMonth(#date([Year],[Month Number],1))

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

I don't want the enddate for all the records, only for the records having last(latest) month and year.

For future requests, provide sample data as table so we can copy/paste (see note below my comment) and expected result based on sample data please.

 

Result

dufoq3_0-1711650610353.png

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMjRU0lEyMjAyVorVAfKMUHgQjgmYYwTnxAIA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Month = _t, Year = _t]),
    Ad_Date = Table.AddColumn(Source, "Date", each #date(Number.From([Year]),Number.From([Month]),1), type date),
    MaxDate = List.Max(Ad_Date[Date]),
    ReplaceMaxDate = Table.TransformColumns(Ad_Date, {{"Date", each if _ = MaxDate then Date.EndOfMonth(MaxDate) else _, type date}})
in
    ReplaceMaxDate

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Thanks

You're welcome. If my solution works, Accept is as Solution please. Thanks.


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

smpa01
Super User
Super User

@DSwezey  create a new column as 

#date([Year],[Month Number],1)

and then format as you wish through DAX

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

I get this error when adding the custom column. 

DSwezey_0-1637783203939.png

 

date <> #date

 

change 

 

date([Year],[Month Number],1)

 

to (as written by @smpa01 )

     

 

#date([Year],[Month Number],1)

 

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