Sub topic 5
Setting Data Types and Locale
This chapter assumes you can connect Power BI Desktop to a CSV or Excel file, open the Power Query Editor, and read the Applied Steps pane. You should also know the basic data types: text, whole number, decimal number, date, and true/false.
Why data types matter
A data type tells Power Query how to read and store the values in a column. It controls sorting, filtering, and which transformations are available. A text column sorts "10" before "9", while a number column sorts them in numeric order. Power BI also uses types downstream: you can only sum a numeric column, and only a date column supports date hierarchies.
The icon at the left of each column header shows its current type. ABC means text, 123 means whole number, 1.2 means decimal, and a calendar icon means date. ABC123 means the type is "any", which means no type is set.
Setting a type in the editor
You can set a type in three ways:
- Click the type icon in the column header and choose a type.
- Select the column, then use Transform > Data Type.
- Right-click the header and choose Change Type.
Each change adds a Changed Type step. Power Query may also add one automatically after you promote headers, because type detection is on by default. Check what it chose. It samples the first rows, so a column with a stray text value further down can be typed wrongly.
Locale: why "1,5" is not always 1.5
Text such as 03/04/2025 is a different date in the US (March 4) than in the UK (3 April). Likewise 1.234,50 is one thousand two hundred thirty-four and a half in Germany, but is not valid in the US. The locale is the set of regional rules used to read text into a type.
By default, Power Query uses your machine's regional settings. That means the same query can give different results on another computer. To fix this, use Change Type > Using Locale, pick the type, and pick the locale the source data was written in. The editor passes that locale as the last argument of the M function.
The first example loads a CSV, promotes headers, and sets types with en-US:
let
Source = Csv.Document(File.Contents("C:\Data\sales.csv"), [Delimiter = ",", Encoding = 65001]),
PromotedHeaders = Table.PromoteHeaders(Source, [PromoteAllScalars = true]),
ChangedType = Table.TransformColumnTypes(PromotedHeaders, {{"OrderID", Int64.Type}, {"OrderDate", type date}, {"Amount", type number}}, "en-US")
in
ChangedTypeTable.TransformColumnTypes takes the table, a list of {column, type} pairs, and an optional culture. Here the culture "en-US" makes OrderDate and Amount parse the same way on every machine. The type names in M are Int64.Type, type number, type date, type text, and type logical.
Common errors
- Cell errors after a type change. If a value cannot be converted, such as
abcin a number column, the cell showsError. A later chapter covers handling these. - Wrong day and month. A date parsed with the wrong locale may swap day and month without any error, or fail only for days above 12. Check values after the change.
- Typing too early. Changing a type before you remove junk rows or promote headers can fail or be redone. Set types after headers are correct.
- Leaving "any" columns. Columns of type any can behave unpredictably in the model. Give every column a real type before loading.
- Number to text and back. Converting an ID with leading zeros to a number drops the zeros. Keep such IDs as text.
Exercise
A CSV from a German supplier contains the Amount value 1.234,50. On your US-configured machine, a plain type change to decimal number gives an error or a wrong value. What should you do, and what does the M step look like?
Show answer
Use Change Type > Using Locale, choose Decimal Number, and select German (Germany) as the locale. The step becomes Table.TransformColumnTypes(PreviousStep, {{"Amount", type number}}, "de-DE"), which reads the value as 1234.5.