Power Query - changing data type for each column

zico8

Board Regular
Joined
Jul 13, 2015
Messages
227
Hi,

How can I change data type to text for all columns no matter what steps has been done before and what columns I have currently??
Is it possible?
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,List.Transform(Table.ColumnNames(Source), each {_,type text}))
in
    #"Changed Type"
 
Upvote 0
Horseyride, this enumeration through the columns seems a powerful tool. Is it possible to do this conditionally - for example, only apply the transformation to columns with "Comments" in the header name, or to skip the transformation for columns with type date?
 
Upvote 0
There's probably a one line version that someone could come up with, by my longer versions are:

Find and only change columns containing "Comments" to text
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Apple", type any}, {"Car", Int64.Type}, {"Bob", type any}, {"Comments1", type any}, {"Comments2", type any}, {"Date1", type date}, {"Date2", type date}}),
    Headers = Table.ColumnNames(Source),
    #"Converted to Table" = Table.FromList(Headers, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    //filter for anything. Here I filter to find the word Comments
    #"Filtered Rows" = Table.SelectRows(#"Converted to Table", each Text.Contains([Column1], "Comments")),
    ListHeaders = Table.ToList(#"Filtered Rows"),
    Count = List.Count(ListHeaders),
    Transform = Table.TransformColumnTypes( #"Changed Type", Table.ToRows(Table.FromColumns({ListHeaders, List.Repeat({type text}, Count  )})))
in
    Transform

Change all columns to text except columns formatted as date
Code:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
   #"Changed Type" = Table.TransformColumnTypes(Source,{{"Apple", type any}, {"Car", Int64.Type}, {"Bob", type any}, {"Comments1", type any}, {"Comments2", type any}, {"Date1", type date}, {"Date2", type date}}),
    Schema = Table.Schema(#"Changed Type"),
    #"Filtered Rows" = Table.SelectRows(Schema, each ([TypeName] <> "Date.Type")),
    #"Removed Other Columns" = Table.SelectColumns(  #"Filtered Rows",{List.First(Table.ColumnNames(#"Filtered Rows"))}),
    ListHeaders = Table.ToList(#"Removed Other Columns"),
    Count = List.Count(ListHeaders),
    Transform = Table.TransformColumnTypes( #"Changed Type", Table.ToRows(Table.FromColumns({ListHeaders, List.Repeat({type text}, Count  )})))
in
    Transform
 
Last edited:
Upvote 0
This is good horseyride, thanks for it.

Could you also show how can I set Data Type - Text for columns from the 4th to the last one?
 
Upvote 0

Forum statistics

Threads
1,224,821
Messages
6,181,163
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top