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

Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. Register now.

Reply
jk100
Frequent Visitor

Changing a variable amount of columns to type date

Hi,
I have a column with rows that contain date and email pairs as a string separated by a delimiter. The amount of date/email pairs varies. In this example I have two pairs but it also has to work for a variable amount of 3, 4, 5 or more entries.powerQueryString.png

 
In a first step I split the entries of the rows into seperate columns. This works, however the date columns are of type text and not of type date. I tried to convert them using Table.TransformColumnTypes() but I could not get it to work.

Any help would be appreciated.

powerQueryHelp.png

 

 

let
    Source = #table({"String"}, {{"10/26/2022,email@email.com;11/02/2022,email@email.com;"}}),
    #"Number Of Columns to Split" = Table.AddColumn(Source, "Number of Columns Needed for Split", each if [String] <> null then List.Count(Text.PositionOfAny([String], {";", ","}, Occurrence.All)) else null),
    DynamicCols = List.Transform(
    {1..List.Max(#"Number Of Columns to Split"[Number of Columns Needed for Split])},
    each "Entry" & Text.From(_)
    ),
    Custom1 = #"Number Of Columns to Split",
    #"Split Column by Delimiter" = Table.SplitColumn(Custom1, "String", Splitter.SplitTextByAnyDelimiter({";", ","}, QuoteStyle.Csv), DynamicCols),
    #"Rename Column Names" = Table.TransformColumnNames(#"Split Column by Delimiter", (columnName as text) as text => if Number.IsEven(try Number.FromText(Text.End((columnName as text), 1)) otherwise 0) then Text.Replace(columnName, "Entry", "Name") else Text.Replace(columnName, "Entry", "Date"))
in
    #"Rename Column Names"

 

 

 

1 ACCEPTED SOLUTION
jk100
Frequent Visitor

I found a solution. Maybe not the most beautiful, but it works.

let
    Source = #table({"String"}, {{"26/10/2022,email@email.com;11/02/2022,email@email.com;"}}),
    #"Number Of Columns to Split" = Table.AddColumn(Source, "Number of Columns Needed for Split", each if [String] <> null then List.Count(Text.PositionOfAny([String], {";", ","}, Occurrence.All)) else null),
    DynamicCols = List.Transform(
    {1..List.Max(#"Number Of Columns to Split"[Number of Columns Needed for Split])},
    each "Entry" & Text.From(_)
    ),
    Custom1 = #"Number Of Columns to Split",
    #"Split Column by Delimiter" = Table.SplitColumn(Custom1, "String", Splitter.SplitTextByAnyDelimiter({";", ","}, QuoteStyle.Csv), DynamicCols),
    #"Rename Column Names" = Table.TransformColumnNames(#"Split Column by Delimiter", (columnName as text) as text => if Number.IsEven(try Number.FromText(Text.End((columnName as text), 1)) otherwise 0) then Text.Replace(columnName, "Entry", "Name") else Text.Replace(columnName, "Entry", "Date")),
    columns = Table.ColumnNames(#"Rename Column Names"),
    dateColumns = List.Select(columns, each Text.StartsWith(_, "Date")),
    transferTypeToDate = Table.TransformColumnTypes(#"Rename Column Names", List.Transform(dateColumns, each {_, type date}))
in
    #"transferTypeToDate"

 dateConversion.png

View solution in original post

3 REPLIES 3
jk100
Frequent Visitor

I found a solution. Maybe not the most beautiful, but it works.

let
    Source = #table({"String"}, {{"26/10/2022,email@email.com;11/02/2022,email@email.com;"}}),
    #"Number Of Columns to Split" = Table.AddColumn(Source, "Number of Columns Needed for Split", each if [String] <> null then List.Count(Text.PositionOfAny([String], {";", ","}, Occurrence.All)) else null),
    DynamicCols = List.Transform(
    {1..List.Max(#"Number Of Columns to Split"[Number of Columns Needed for Split])},
    each "Entry" & Text.From(_)
    ),
    Custom1 = #"Number Of Columns to Split",
    #"Split Column by Delimiter" = Table.SplitColumn(Custom1, "String", Splitter.SplitTextByAnyDelimiter({";", ","}, QuoteStyle.Csv), DynamicCols),
    #"Rename Column Names" = Table.TransformColumnNames(#"Split Column by Delimiter", (columnName as text) as text => if Number.IsEven(try Number.FromText(Text.End((columnName as text), 1)) otherwise 0) then Text.Replace(columnName, "Entry", "Name") else Text.Replace(columnName, "Entry", "Date")),
    columns = Table.ColumnNames(#"Rename Column Names"),
    dateColumns = List.Select(columns, each Text.StartsWith(_, "Date")),
    transferTypeToDate = Table.TransformColumnTypes(#"Rename Column Names", List.Transform(dateColumns, each {_, type date}))
in
    #"transferTypeToDate"

 dateConversion.png

Anonymous
Not applicable

Hi @jk100 ,

 

The preferred solution is to use "Detect Data Type" or change type as Date manually under Transform.

21.png

vstephenmsft_1-1676358090611.png

If it is unsuccessful, your date format is different from your local date format. Then try the second solution below.

Right-click the date column and click "Using Locale...".

vstephenmsft_2-1676358157426.png

Select the Locale you need, and there are sample input values for reference.

vstephenmsft_3-1676358337849.png

Click OK and the type will be date.

About more details, you can refer to

Data types in Power Query - Power Query | Microsoft Learn

                                                                                                                                                         

Best Regards,

Stephen Tao

 

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

 

Hi Stephen Tao,

thank you for your response.

The power query should run automatically so I cannot manually trigger "Detect Data Type". Another issue is that I never know how many Date columns will be created in the previous steps.

My idea was to create a list of the column names using "List.Transform()" 
- as in the #"Rename Column Names" step - and then use "Table.TransformColumnTypes()" to change the column type if the column names matches a certain string pattern. Unfortunately, I did not get it to work yet:

let
    Source = #table({"String"}, {{"10/26/2022,email@email.com;11/02/2022,email@email.com;"}}),
    #"Number Of Columns to Split" = Table.AddColumn(Source, "Number of Columns Needed for Split", each if [String] <> null then List.Count(Text.PositionOfAny([String], {";", ","}, Occurrence.All)) else null),
    DynamicCols = List.Transform(
    {1..List.Max(#"Number Of Columns to Split"[Number of Columns Needed for Split])},
    each "Entry" & Text.From(_)
    ),
    Custom1 = #"Number Of Columns to Split",
    #"Split Column by Delimiter" = Table.SplitColumn(Custom1, "String", Splitter.SplitTextByAnyDelimiter({";", ","}, QuoteStyle.Csv), DynamicCols),
    #"Rename Column Names" = Table.TransformColumnNames(#"Split Column by Delimiter", (columnName as text) as text => if Number.IsEven(try Number.FromText(Text.End((columnName as text), 1)) otherwise 0) then Text.Replace(columnName, "Entry", "Name") else Text.Replace(columnName, "Entry", "Date")),
    #"Changed Type" = Table.TransformColumnTypes(#"Rename Column Names", List.Transform(Table.ColumnNames(#"Rename Column Names"), (column) => if Text.StartsWith(column as text, "Date") then type date else type any))
in
    #"Changed Type"

 

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

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.

Top Kudoed Authors