Forum Discussion
Detect type of column in Power Query Formula Language
I've got some query tables generated from imported data. All the data comes in as type 'Any', and I'm trying to automatically detect the type of the data in each column.
Some of the queries generate tables with columns based on the in-coming data - I don't know what the columns are going to be until the query runs and sets up the table (data comes from an Azure blob). As I will have quite a few tables to maintain (which columns can change with any data refresh - possibly new columns being added), it would be unmanageable to go through all of them each time and press 'Detect Data Type' on the columns.
So I'm trying to figure out how I can do a 'Detect Data Type' in the query formula language to attach to the end of the query that generates the table columns. I've tried grabbing the first entry in a column and do Value.Type(column{0}), however this seems to come out as 'Text' for a column which has integers in it. Pressing 'Detect Data Type' does however correctly identifies the type as 'Whole Number'.
Does anyone know how to detect a column's entry types?
P.S. I'm not too worried about a column possibly holding values of different data types
9 Replies
- ImkeFCommunity Champion
Table.Schema(YourTable) will deliver an analysis of all your table columns with a type-column as well.
- jPinhaoAdvocate II
Thanks for the reply ImkeF.
I had a look and this and tried it out. As I suspected this returns the current type of the columns- in this case Any.Type.What I am looking for is to be able to deduce a column's type based on it's content, in the same way that pressing 'Detect Data Type' in the ribbon does. I can't imagine this being impossible , but I'm having a hard time figuring out how to. The closest thing I thought of would be something like getting the values of a column in a list, and then
List.MatchesAll(column, each Values.Is(_, 'a type'))
and do that for a hierarchy of types until I find one that matches.
This would force me to have a hierarchy of types to check (do I have dates first? numbers? floats?), which seems incredibly easy to break, and it also seems quite wasteful.
Isn't there another way?- ImkeFCommunity Champion
Nothing I can think of but some simple stupid brute force: Convert a column to number and check if there are errors in there: then it's text. Convert to date and check if it is before ? and after y and no errors: then it's probably number...
- sminoneseFrequent Visitor
This should be an idea, as the current implementation requires you to specify the column names. Adding a new column to a table means you need to update the "Change data type" step manually. There should be a "detect data type on all columns, exluding some" step so even automatically added columns will automatically get an auto-detected data type.
Alternatively, you could think about unpivoting your data so you sort the data according to data types. One column for quantitative values, one column for text values, and one column for the "Attribute" name. Then it's only a matter of adding rows to the table, rather than adding columns.
- thakksHelper I
- MarcelBeugCommunity Champion
You can select the applicable columns and choose "Detect Data Type" on the Transform tab.
- AnonymousNot applicable
Please try this solution
P.S. Read question one more time - answer corresponds to question header, but not to question body...