Stop 04 · Power Query TransformationsTopics · 3/11

Changing Data Types

Part of the Power BI Roadmap

In short

A number stored as text cannot be summed, and a date stored as text sorts April before January. Setting the right type on every column is the single most important cleaning habit.

Each column needs the correct type, such as whole number, decimal, date or text. Wrong types break sorting, maths and time intelligence later.

The jaadu sorts envelopes, parcels and small coin pouches into three differently shaped bins at a post office counter.

Think of it like this

Sorting the post into the right bins

At a post office, letters go in one bin, parcels in another and money orders in a locked drawer. Put a parcel in the letter bin and it gets crushed. Data types are those bins: whole number, decimal, date, text. The right bin decides what you can safely do with each value.

Watch it happen

An Amount column imported as text

"1,250"
0
"980"
1
"N/A"
2
"2,400"
3
""
4

1/4The CSV arrived as text. Summing this column is impossible.

The big ideas

What you need to know

Pick the narrowest type

Whole Number for counts and IDs, Fixed Decimal for money, Decimal for measurements, Date for dates without time.

Use locale for dates

Right-click a column, Change Type, Using Locale, then pick English (India) for DD/MM/YYYY files.

Errors are information

A cell that cannot convert becomes Error. Find out why before you replace or remove it.

Auto-detect is a guess

Power Query adds a Changed Type step automatically based on the first rows. Review it, don't trust it blindly.

See it in code

Walk through the code

m
1let2    Source = Csv.Document(File.Contents("D:\Data\sales.csv"), [Delimiter = ","]),3    Headers = Table.PromoteHeaders(Source, [PromoteAllScalars = true]),4    Typed = Table.TransformColumnTypes(5        Headers,6        {7            {"OrderID", Int64.Type},8            {"OrderDate", type date},9            {"City", type text},10            {"Amount", Currency.Type}11        },12        "en-IN"13    ),14    NoErrors = Table.ReplaceErrorValues(Typed, {{"Amount", null}})15in16    NoErrors

1/3Table.TransformColumnTypes takes a list of column-type pairs and an optional culture.

Set every column's type in one step, read with the en-IN culture, then turn unconvertible amounts into null.

Save yourself the pain

Common mistakes

Oops

Leaving phone numbers or PIN codes as Whole Number.

Do this instead

Store codes as text. You never add PIN codes, and leading zeros in some codes would be lost.

Oops

Changing type, then trimming spaces, then changing type again.

Do this instead

Clean text first and set types once near the end. Repeated Changed Type steps are messy and easy to break.

Your mission

Fix a badly typed file

~20 min

Quick check

Which type should a column of Indian PIN codes like 560001 usually have?

Go deeper

  1. 1Check that every date column is typed as Date, not text.
  2. 2Use 'Using locale' for dates written as DD-MM-YYYY.
  3. 3Remove automatic 'Changed Type' steps that guess wrongly.

More topics at this stop

Stuck on this topic? Ask an Insider

Get 1:1 guidance from people who've walked this exact path — on the InsideEdge app.

Download
InsideEdge