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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Anonymous
Not applicable

Create Date custom column from source name.

Hi,

 

Help me to create Mcode using adding custom column from 'Source name'

that year take first 4 number of source name which is 2024(YYYY),

if source name contains Q1 then 03/31(MM/DD),

if source name contains Q2 then 06/30(MM/DD),

if source name contains Q3 then 09/30(MM/DD),

if source name contains Q4 then 12/31(MM/DD),

 

e.g. for below example > 12/31/2024 in 'Date' format in custom column which will be created by Mcode.

 

jeongkim_0-1735896011548.png

 

3 ACCEPTED SOLUTIONS
saud968
Super User
Super User

I can help you create the M code for this. Here's how you can add a custom column in Power Query to generate the date based on the 'Source name':

Open Power Query Editor.
Go to the Add Column tab.
Click on Custom Column.
Enter the following M code in the Custom Column formula box:
let
Source = ... // Your data source here
AddCustom = Table.AddColumn(Source, "Custom Date", each
let
SourceName = [Source name],
Year = Text.Start(SourceName, 4),
Quarter =
if Text.Contains(SourceName, "Q1") then "03/31"
else if Text.Contains(SourceName, "Q2") then "06/30"
else if Text.Contains(SourceName, "Q3") then "09/30"
else if Text.Contains(SourceName, "Q4") then "12/31"
else null,
DateString = Quarter & "/" & Year,
DateValue = Date.FromText(DateString)
in
DateValue
)
in
AddCustom
This code will create a new column named "Custom Date" with the date formatted as specified based on the 'Source name'. Make sure to replace ... // Your data source here with your actual data source.

Best Regards
Saud Ansari
If this post helps, please Accept it as a Solution to help other members find it. I appreciate your Kudos!

View solution in original post

Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Added Custom2" = Table.AddColumn(Source, "Date", each Date.EndOfMonth(#date(Number.From(Text.Start([Source name],4)),if Text.Contains([Source name],"Q1") then 3 else if Text.Contains([Source name],"Q2") then 6 else if Text.Contains([Source name],"Q3") then 9 else 12,1)))
in
    #"Added Custom2"

Hope this helps.

Ashish_Mathur_0-1736049687464.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

View solution in original post

Anonymous
Not applicable

Hi @Anonymous ,

 

Take the code given by Ashish as an example:

let
......,
#"Removed Other Columns2" = Table.SelectColumns(#"Renamed Columns",{"Source name", "Sheet name", "Internal Site ID", "Category", "공사번호#(lf)Service No."}),
#"Added Conditional Column" = Table.AddColumn(#"Removed Other Columns2", "Date", each Date.EndOfMonth(#date(Number.From(Text.Start([Source name],4)),if Text.Contains([Source name],"Q1") then 3 else if Text.Contains([Source name],"Q2") then 6 else if Text.Contains([Source name],"Q3") then 9 else 12,1)))
#"Changed Type" = Table.TransformColumnTypes(#"Added Conditional Column",{{"공사번호#(lf)Service No.", Int64.Type}}),
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1, Int64.Type)
in
#"Added Index"

 

Best Regards,
Gao

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum

View solution in original post

5 REPLIES 5
Ashish_Mathur
Super User
Super User

Hi,

This M code works

let
    Source = Excel.CurrentWorkbook(){[Name="Data"]}[Content],
    #"Added Custom2" = Table.AddColumn(Source, "Date", each Date.EndOfMonth(#date(Number.From(Text.Start([Source name],4)),if Text.Contains([Source name],"Q1") then 3 else if Text.Contains([Source name],"Q2") then 6 else if Text.Contains([Source name],"Q3") then 9 else 12,1)))
in
    #"Added Custom2"

Hope this helps.

Ashish_Mathur_0-1736049687464.png

 


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/
saud968
Super User
Super User

I can help you create the M code for this. Here's how you can add a custom column in Power Query to generate the date based on the 'Source name':

Open Power Query Editor.
Go to the Add Column tab.
Click on Custom Column.
Enter the following M code in the Custom Column formula box:
let
Source = ... // Your data source here
AddCustom = Table.AddColumn(Source, "Custom Date", each
let
SourceName = [Source name],
Year = Text.Start(SourceName, 4),
Quarter =
if Text.Contains(SourceName, "Q1") then "03/31"
else if Text.Contains(SourceName, "Q2") then "06/30"
else if Text.Contains(SourceName, "Q3") then "09/30"
else if Text.Contains(SourceName, "Q4") then "12/31"
else null,
DateString = Quarter & "/" & Year,
DateValue = Date.FromText(DateString)
in
DateValue
)
in
AddCustom
This code will create a new column named "Custom Date" with the date formatted as specified based on the 'Source name'. Make sure to replace ... // Your data source here with your actual data source.

Best Regards
Saud Ansari
If this post helps, please Accept it as a Solution to help other members find it. I appreciate your Kudos!

Anonymous
Not applicable

Can you help me write a full code? 

 

jeongkim_0-1735988649769.png

 

maybe you can write code referring to below my current code pls. 

FYI,
#"Removed Other Columns2" = Table.SelectColumns(#"Renamed Columns",{"Source name", "Sheet name", "Internal Site ID", "Category", "공사번호#(lf)Service No."}),
#"Added Conditional Column" = (Solution)
#"Changed Type" = Table.TransformColumnTypes(#"Added Conditional Column",{{"공사번호#(lf)Service No.", Int64.Type}}),
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1, Int64.Type)
in
#"Added Index"

Anonymous
Not applicable

Hi @Anonymous ,

 

Take the code given by Ashish as an example:

let
......,
#"Removed Other Columns2" = Table.SelectColumns(#"Renamed Columns",{"Source name", "Sheet name", "Internal Site ID", "Category", "공사번호#(lf)Service No."}),
#"Added Conditional Column" = Table.AddColumn(#"Removed Other Columns2", "Date", each Date.EndOfMonth(#date(Number.From(Text.Start([Source name],4)),if Text.Contains([Source name],"Q1") then 3 else if Text.Contains([Source name],"Q2") then 6 else if Text.Contains([Source name],"Q3") then 9 else 12,1)))
#"Changed Type" = Table.TransformColumnTypes(#"Added Conditional Column",{{"공사번호#(lf)Service No.", Int64.Type}}),
#"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 1, 1, Int64.Type)
in
#"Added Index"

 

Best Regards,
Gao

Community Support Team

 

If there is any post helps, then please consider Accept it as the solution  to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!

How to get your questions answered quickly --  How to provide sample data in the Power BI Forum

Anonymous
Not applicable

Thanks it works and has accepted. 

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

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.