Forum Discussion
monojchakrab
Resolver III
4 years agoSelect columns with specified data type
I have a table with 50+ columns... I want to select only the columns which have numeric data type. Is it possible to use some combination of Table.ColumnNames or Table.SelectColumns with Valu...
- 4 years ago
Hi,
If you want to select columns using columns' defined type then you could use code like this:
let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSkxMVNJRMgRiAz0waa5vYKZvZGBkpBSrA5M2AksbQaXNodKxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [text = _t, int = _t, decimal = _t, date = _t]), ChangedType = Table.TransformColumnTypes(Source,{{"date", type date}, {"decimal", type number}, {"int", Int64.Type}, {"text", type text}}, "en-US"), TableSchemaCall = Table.Schema(ChangedType), FilteredRows = Table.SelectRows(TableSchemaCall, each ([TypeName] = "Number.Type")), ToList = FilteredRows[Name], SelectionOfColumns = Table.SelectColumns( #"ChangedType",ToList) in SelectionOfColumnsArtur
- 4 years ago
If you see my code, second argument was {type nullable number} and you had put {type number}. Unless you use nullable keyword, it will not give you the result.
If you need name of the columns only, then
= Table.ColumnsOfType(#"Filtered Rows",{type number}) - 4 years ago
If you would like to only select the columns with the type of percentage you can't use Table.ColumsOfType function because it treats all number kinds (whole numbers, decimals, percentage, currency) as numbers. IF for example if you want to select only currencies, you would have to Table.ScemaFunction.
Artur
artpil
Resolver II
4 years agoHi,
If you want to select columns using columns' defined type then you could use code like this:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WSkxMVNJRMgRiAz0waa5vYKZvZGBkpBSrA5M2AksbQaXNodKxAA==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [text = _t, int = _t, decimal = _t, date = _t]),
ChangedType = Table.TransformColumnTypes(Source,{{"date", type date}, {"decimal", type number}, {"int", Int64.Type}, {"text", type text}}, "en-US"),
TableSchemaCall = Table.Schema(ChangedType),
FilteredRows = Table.SelectRows(TableSchemaCall, each ([TypeName] = "Number.Type")),
ToList = FilteredRows[Name],
SelectionOfColumns = Table.SelectColumns( #"ChangedType",ToList)
in
SelectionOfColumns
Artur