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

Join us for an expert-led overview of the tools and concepts you'll need to become a Certified Power BI Data Analyst and pass exam PL-300. Register now.

Reply
PowerBIET
Frequent Visitor

Power Query replace day with current year.

Please assist me to create a custom column in Power Query that replaces the day with the current year. 

 

I didn't want to hardcode 2024 using the Replace Values button in the ribbon, cause the year would change after December 31.  

 

Power BI File Link 

 

Screenshot 2024-04-09 124825.png

 

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi @PowerBIET ,

I can't open your pbix file for company security policy reasons, but I created the dataset myself and tested it. Please follow the steps below:

vjunyantmsft_0-1712717101884.png

vjunyantmsft_1-1712717135533.png

The output is as below:

vjunyantmsft_2-1712717151649.png

Then use this M function:

= Table.TransformColumns(#"Split Column by Delimiter", {{"Date.1", each Text.From(Date.Year(DateTime.LocalNow())), type text}})

vjunyantmsft_3-1712717196894.png

Finally, column merging:

vjunyantmsft_4-1712717240819.png

vjunyantmsft_5-1712717260863.png

 

 And the final output is as below:

vjunyantmsft_6-1712717285644.png


Here is the whole M function in the Advanced Editor:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8s3P01FwLCjKzFEwsNBRMLMyNFcI8FWK1cGQMrcyNsYhZWZlZIZTysQEh5ShkZUxRFssAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type text}}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Changed Type", "Date", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"Date.1", "Date.2", "Date.3"}),
    #"ReplacedValues" = Table.TransformColumns(#"Split Column by Delimiter", {{"Date.1", each Text.From(Date.Year(DateTime.LocalNow())), type text}}),
    #"Inserted Merged Column" = Table.AddColumn(ReplacedValues, "Merged", each Text.Combine({[Date.1], [Date.2], [Date.3]}, ","), type text)
in
    #"Inserted Merged Column"


Best Regards,
Dino Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

Hi @PowerBIET ,

I can't open your pbix file for company security policy reasons, but I created the dataset myself and tested it. Please follow the steps below:

vjunyantmsft_0-1712717101884.png

vjunyantmsft_1-1712717135533.png

The output is as below:

vjunyantmsft_2-1712717151649.png

Then use this M function:

= Table.TransformColumns(#"Split Column by Delimiter", {{"Date.1", each Text.From(Date.Year(DateTime.LocalNow())), type text}})

vjunyantmsft_3-1712717196894.png

Finally, column merging:

vjunyantmsft_4-1712717240819.png

vjunyantmsft_5-1712717260863.png

 

 And the final output is as below:

vjunyantmsft_6-1712717285644.png


Here is the whole M function in the Advanced Editor:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45W8s3P01FwLCjKzFEwsNBRMLMyNFcI8FWK1cGQMrcyNsYhZWZlZIZTysQEh5ShkZUxRFssAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Date = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type text}}),
    #"Split Column by Delimiter" = Table.SplitColumn(#"Changed Type", "Date", Splitter.SplitTextByDelimiter(",", QuoteStyle.Csv), {"Date.1", "Date.2", "Date.3"}),
    #"ReplacedValues" = Table.TransformColumns(#"Split Column by Delimiter", {{"Date.1", each Text.From(Date.Year(DateTime.LocalNow())), type text}}),
    #"Inserted Merged Column" = Table.AddColumn(ReplacedValues, "Merged", each Text.Combine({[Date.1], [Date.2], [Date.3]}, ","), type text)
in
    #"Inserted Merged Column"


Best Regards,
Dino Tao
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

johnbasha33
Super User
Super User

@PowerBIET 
here is the code, you can easily create one for above scenario.
let
CurrentYear = Date.Year(DateTime.LocalNow()),
Source = YourPreviousDataSource, // Replace YourPreviousDataSource with the name of your previous data source
ReplacedDate = Table.TransformColumns(Source, {{"DateColumn", each Date.FromText(Text.Middle(Text.From(_), 1, 10) & "-" & Text.From(CurrentYear)), type date}})
in
ReplacedDate

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

Do you know why this expression error is happening? Or can you please update a copy of the PBIX Link.

 

Here is a link to the file with the error.

 

 

= Table.AddColumn(#"Promoted Headers", "Custom", each let
CurrentYear = Date.Year(DateTime.LocalNow()),
Source = Source, // Replace YourPreviousDataSource with the name of your previous data source
ReplacedDate = Table.TransformColumns(Source, {{"Date", each Date.FromText(Text.Middle(Text.From(_), 1, 10) & "-" & Text.From(CurrentYear)), type date}})
in
ReplacedDate)

 

Screenshot 2024-04-09 135125.jpg

 

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

Check out the June 2025 Power BI update to learn about new features.

June 2025 community update carousel

Fabric Community Update - June 2025

Find out what's new and trending in the Fabric community.