Forum Discussion
Capture column names and data types from an earlier step in a query
- Anonymous4 years ago
Yes, you can refer to any other step in the query, whether before or after the current step, and it will return a table, list, value, whatever it might be. So NewStep = ThreeStepsAgo will return ThreeStepsAgo, and if it's a table, you have a table. You can also do
NewColumns = ThreeStepsAgo[[Column1], [Column5], [Column9]] to return the table step with only certain columns.
--Nate
- 4 years ago
Hi JimJaggers,
You don't need to work hard to do that. You can simply use the existing table type. Like this:let Table1 = #table(type table [Num=Int64.Type,String=text], {{1, "One"}}), Table2 = #table(type table [String=text], {{"two"}}), AlternativeOutput = #table(Value.Type(Table1),{{0,"Zero"}}) in AlternativeOutput
SpartaBI thank you. This solution does appeal to me. But I never could quite get it to work. Do you have any idea how I could get the schema table generated by
#"Table1 Schema" = Table.Schema(Table1)
into a format that I could use to create a table with that schema? Here is what I tried
Columns = Table.AddColumn(TableSchema, "ColumnDefinitions", each [Name]&"="&[Kind]),//create column with Name-Data Type values
A = Table.Column(Columns, "ColumnDefinitions"),//convert Name=Data Type column into a list
B = Table.TransformColumns(Columns, {"ColumnDefinitions", each Text.Combine(_,",")}),//convert list into a CSV list
//Use CSV list to generate table
AlternativeOutput = #table(type table B,
{{"DNU","DNU","DNU","DNU","DNU",
"DNU","DNU","DNU","DNU","DNU",
0,"DNU","DNU",0,
0,0,0,
false,0,0,
0,"DNU",
false,0,
0,"DNU","DNU","DNU"
}}
),
But I get an error stating "We cannot convert a value to type Table to type Type." I've tried a few things, such as removing the "type table" from the beginning of the #table() function and applying various conversion functions to the CSV string in #"B" or the list in #"A", but to no avail. I think my problem is not really understanding the compund data types, particularly what a type Type is.
Any thoughts you have on the matter are appreciated, for my elucidation. But not really necessary for me to address the problem.
Thank you again for your suggestion.
Hi JimJaggers,
You don't need to work hard to do that. You can simply use the existing table type. Like this:
let
Table1 = #table(type table [Num=Int64.Type,String=text], {{1, "One"}}),
Table2 = #table(type table [String=text], {{"two"}}),
AlternativeOutput = #table(Value.Type(Table1),{{0,"Zero"}})
in
AlternativeOutput
- JimJaggers4 years agoFrequent Visitor
That is a beautiful solution.