Home > Net >  Why does Table.FromList() fail on this trivial conversion from integers?
Why does Table.FromList() fail on this trivial conversion from integers?

Time:01-25

Consider Microsoft's first example for Table.FromList. Invoking it in the trivial query

let
    result = Table.FromList({"a", "b", "c", "d"}, null, {"Letters"})
in
    result

results in a table that looks like

| Letters |
 --------- 
| a       |
| b       |
| c       |
| d       |

Substituting numbers for letters results in the query

let
    result = Table.FromList({1,2,3,4},null,{"Integers"})
in
    result

which produces the error

Expression.Error: We cannot convert the value 1 to type Text.

Details: Value=1 Type=Type

I expected the table

| Integers |
 ---------- 
| 1        |
| 2        |
| 3        |
| 4        |
  1. How do I get the expected table?
  2. What is happening that is causing this problem?

CodePudding user response:

it's written in the function description:

By default, the list is assumed to be a list of text values that is split by commas.

if you convert the list to table using the UI, you can notice that the default splitter is replaced:

let
    Source = {1,2,3,4},
    #"Converted to Table" = Table.FromList(Source, Splitter.SplitByNothing(), null, null, ExtraValues.Error)
in
    #"Converted to Table"

so the solution to your problem is

let
    result = Table.FromList({1,2,3,4}, Splitter.SplitByNothing(), {"Integers"})
in
    result

CodePudding user response:

Because numbers and text are stored differently in computer memory. You can't perform calculations with text, so 1 as a number has to be treated different from 1 as a symbol, to computers. Remember that a computer is just a machine and not a person, it lacks common sense sometimes. The solution is that you need to put quotation marks around those numbers. When the computer sees quotation marks it will know you are talking about the symbols

Try this:

let
    result = Table.FromList({"1","2","3","4"},null,{"Integers"})
in
    result

CodePudding user response:

Try

letList={1..4},
result =Table.FromList(List.Transform(List, each Text.From(_)), null, {"Numbers"})
in result
  • Related