Forum Discussion

Anonymous's avatar
Anonymous
Not applicable
8 years ago
Solved

Cannot convert value to type table

Hello

 

I'm trying to split a string into multiple columns and I found out this code:

 SplitByDelimiter = (table, column, delimiter) =>
        let
            Count = List.Count(List.Select(Text.ToList(Table.Column(table, column){0}), each _ = delimiter)) + 1,
            Names = List.Transform(List.Numbers(1, Count), each column & "." & Text.From(_)),
            Types = List.Transform(Names, each {_, type text}),
            Split = Table.SplitColumn(table, column, Splitter.SplitTextByDelimiter(delimiter), Names),
            Typed = Table.TransformColumnTypes(Split, Types)
        in
            Typed, 

    #"Split Column by Delimiter" = SplitByDelimiter("My table", "my column", " ") 

It seems a good candidate to test, but I'm getting the following error:

 

Expression.Error: We cannot convert the value "My Table" to type Table.
Details:
    Value=My Table
    Type=Type

My orginal column of type text is something like:

A B C D

A

B C

A B C D E

 

Since I do not know very well how to debug power query I'm a bit stuck.

 

Thanks

  • Somehow you need to determine how many coluns you need for the entry with the largest number of delimiters.

    One way is to have a "trial split" and count the number of items, as in the code below (with adjusted "Count" step).

     

    let
        SplitByDelimiter = (table, column, delimiter) =>
            let
                Count = List.Max(List.Transform(Table.Column(table, column), each List.Count(Text.Split(_,delimiter)))),
                Names = List.Transform(List.Numbers(1, Count), each column & "." & Text.From(_)),
                Types = List.Transform(Names, each {_, type text}),
                Split = Table.SplitColumn(table, column, Splitter.SplitTextByDelimiter(delimiter), Names),
                Typed = Table.TransformColumnTypes(Split, Types)
            in
                Typed, 
    
        MyTable = #table(type table[my column = text],{{"A B C D"},{"A"},{"B C"},{"A B C D E"}}),
        #"Split Column by Delimiter" = SplitByDelimiter(MyTable, "my column", " ")
    in
        #"Split Column by Delimiter"

9 Replies

  • Greg_Deckler's avatar
    Greg_Deckler
    Community Champion

    I believe that you are showing a function "SplitByDelimiter" and a step in a query that calls that function. You need to substitute "My table" with the actual name of your table, no double quotes. Same thing with "my column".

    • Anonymous's avatar
      Anonymous
      Not applicable

      Hello Greg_Deckler

       

      I tried. But my table is called My Table... so how can I call it? I tried no quotes, single quotes and always get an error.

       

      Thanks