How to get type name of value in Power Query?

anvg

Active Member
Joined
Feb 14, 2012
Messages
485
Hello.
Let we have a query
Code:
let
    source = #table({"Value"}, {{1}, {"text"}, {true}, {#date(2019, 1, 1)}, {null}}),
    addTypeColumn = Table.AddColumn(source, "Type", each Value.Type([Value]), Type.Type)
in
    addTypeColumn
If to click in cell of "Type" column we can see type name of cell value which power query UI shows. But how get that type name in new column of table?
Regards, Andrey.
 

Attachments

  • typeName.png
    typeName.png
    4.1 KB · Views: 10

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
I am not sure to understand the question.
To add a column named Total Sales (multiplies a column Qty by a Price) I use

Added Custom= Table.AddColumn(source, "Total Sales", each [Qty]*[Price])
If I want to change its type to a number, I use
ChangeType= Table.TransformColumnTypes(#"Added Custom",{{"Total Sales", Int64.Type}})
If I want a currency, I use
ChangeType= Table.TransformColumnTypes(#"Added Custom",{{"Total Sales", Currency.Type}})
 
Upvote 0
I can't find any built-in method to do it, but you can create a translation table:

Code:
let
    Source = #table({"Value"}, {{1}, {"text"}, {true}, {#date(2019, 1, 1)}, {null}}),
    addTypeColumn = Table.AddColumn(Source, "Type", each
      let
        TypeLookup = (inputType as type) as text =>
          Table.FromRecords(
            {
              [Type=type text, Value="Text"],
              [Type=type number, Value="Number"],
              [Type=type null, Value="Null"],
              [Type=type logical, Value="Logical"],
              [Type=type time, Value="Time"],
              [Type=type date, Value="Date"],
              [Type=type datetime, Value="DateTime"],
              [Type=type datetimezone, Value="DateTimeZone"],
              [Type=type duration, Value="Duration"],
              [Type=type binary, Value="Binary"],
              [Type=type type, Value="Type"],
              [Type=type list, Value="List"],
              [Type=type record, Value="Record"],
              [Type=type table, Value="Table"],
              [Type=type function, Value="Function"],
              [Type=type anynonnull, Value="Any Non Null"]
            }
        ){[Type=inputType]}[Value]
      in
        TypeLookup(Value.Type([Value])))
    in
    addTypeColumn
 
Upvote 0
Thank you a lot, friends!
My solution is
Code:
let
    getTypeName = (testing as type) as any =>
    let
        createdColumnType = #table(type table [field = testing], {}),
        tableSchema = Table.Schema(createdColumnType){0}[TypeName]
    in
        tableSchema,
    source = #table({"Value"}, {
        {1},
        {"text"},
        {true},
        {#date(2019, 1, 1)},
        {null},
        {{1, 2, 3}},
        {[a = null, b = 3]},
        {#table({"f1"}, {})},
        {#binary("1234")},
        {Value.ReplaceType(123, Int64.Type)}
    }),
    addTypeColumn = Table.AddColumn(source, "TypeName", each getTypeName(Value.Type([Value])), Text.Type)
in
    addTypeColumn
Regards, Andrey.
 
Upvote 0

Forum statistics

Threads
1,214,832
Messages
6,121,849
Members
449,051
Latest member
excelquestion515

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