Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
smpa01
Super User
Super User

Dynamically generate column schema as record

Is it possible to generate dynamically for all columns of a given table the following

 

 

[id=nullable Int64.Type, year=nullable Int64.Type, amount=nullable number, ing_est=nullable datetime]

 

 

 

sample query

 

let
    src=Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("lZBBCsMwDAT/4nMiVivLtvSO3EL+/4267aUlhWJdBIJhR3ueZUSUrRD0uXaFjjCprfX3te6IHfVAJDxJ0WaYU67tg60v1jkqhCugzYXlJOuNTuntm1RPjGQVRL+Tr//oGOai3WyF/aGpI9GTsytiRdOPmaNMhlhd1PzH3jT9UOQslE08FjVnzpN0ib6seWevBw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [id = _t, year = _t, amount = _t, ing_est = _t]),
    ct = Table.TransformColumnTypes(src,{{"id", Int64.Type}, {"year", Int64.Type}, {"amount", type number}, {"ing_est", type datetime}}),
    #"Grouped Rows" = Table.Group(ct, {"id"}, {{"ad", each _, type table [id=nullable Int64.Type, year=nullable Int64.Type, amount=nullable number, ing_est=nullable datetime]}})
in
    #"Grouped Rows"

 

 

@AlexisOlson 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
1 ACCEPTED SOLUTION

Now I probably see what you want to achive. You want to replace this part of Table.Group. Is that correct?

dufoq3_0-1725568025829.png

If yes, you can just delete that part of Table.Group and use Table.Combine to preserve column types, like this:

 

let
    src=Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("lZBBCsMwDAT/4nMiVivLtvSO3EL+/4267aUlhWJdBIJhR3ueZUSUrRD0uXaFjjCprfX3te6IHfVAJDxJ0WaYU67tg60v1jkqhCugzYXlJOuNTuntm1RPjGQVRL+Tr//oGOai3WyF/aGpI9GTsytiRdOPmaNMhlhd1PzH3jT9UOQslE08FjVnzpN0ib6seWevBw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [id = _t, year = _t, amount = _t, ing_est = _t]),
    ct = Table.TransformColumnTypes(src,{{"id", Int64.Type}, {"year", Int64.Type}, {"amount", type number}, {"ing_est", type datetime}}, "en-US"),
    GroupedRows = Table.Group(ct, {"id"}, {{"ad", each _, type table}}),
    Combined = Table.Combine(GroupedRows[ad])
in
    Combined

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

View solution in original post

10 REPLIES 10
dufoq3
Super User
Super User

Hi @smpa01, check this:

 

Output:

dufoq3_0-1725565384941.png

let
    src=Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("lZBBCsMwDAT/4nMiVivLtvSO3EL+/4267aUlhWJdBIJhR3ueZUSUrRD0uXaFjjCprfX3te6IHfVAJDxJ0WaYU67tg60v1jkqhCugzYXlJOuNTuntm1RPjGQVRL+Tr//oGOai3WyF/aGpI9GTsytiRdOPmaNMhlhd1PzH3jT9UOQslE08FjVnzpN0ib6seWevBw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [id = _t, year = _t, amount = _t, ing_est = _t]),
    ct = Table.TransformColumnTypes(src,{{"id", Int64.Type}, {"year", Int64.Type}, {"amount", type number}, {"ing_est", type datetime}}, "en-US"),
    GroupedRows = Table.Group(ct, {"id"}, {{"ad", each _, type table}, {"Schema", each Record.FromTable(Table.RenameColumns(Table.Schema(_)[[Name], [TypeName]], {{"TypeName", "Value"}}, MissingField.Ignore)), type record}})
in
    GroupedRows

 


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

@dufoq3  this is good but I want to dynamically pass on that bit to finally give me the following

 

smpa01_0-1725565716702.png

 

 

 

//didnot work
let
    src=Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("lZBBCsMwDAT/4nMiVivLtvSO3EL+/4267aUlhWJdBIJhR3ueZUSUrRD0uXaFjjCprfX3te6IHfVAJDxJ0WaYU67tg60v1jkqhCugzYXlJOuNTuntm1RPjGQVRL+Tr//oGOai3WyF/aGpI9GTsytiRdOPmaNMhlhd1PzH3jT9UOQslE08FjVnzpN0ib6seWevBw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [id = _t, year = _t, amount = _t, ing_est = _t]),
    ct = Table.TransformColumnTypes(src,{{"id", Int64.Type}, {"year", Int64.Type}, {"amount", type number}, {"ing_est", type datetime}}, "en-US"),
    GroupedRows = let x = Table.Group(ct, {"id"}, {{"ad", each _, type table}, {"Schema", each Record.FromTable(Table.RenameColumns(Table.Schema(_)[[Name], [TypeName]], {{"TypeName", "Value"}}, MissingField.Ignore)), type record}})[Schema]{0} in Table.Group(ct, {"id"}, {{"ad", each _, type table x}})
in
    GroupedRows

 

 

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

What is the purpose of this? Do you want to replase table types with another table types or what? If yes, there is more efficient way:

 

let
    Source = #table(type table[Name=text, Age=Int64.Type, Date of Birth=date], {{"Peter", 20, #date(2000,10,15)}, {"Martin", 25, #date(2003,12,10)}}),
    ChangedType = Table.TransformColumnTypes(Source,{{"Age", type number}, {"Date of Birth", type datetime}}),
    RestoredTypes = Value.ReplaceType(ChangedType, Value.Type(Source))
in
    RestoredTypes

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

The purpose is to create a function that can group any table with the dynamically generated schema of that table to be passed on to Table.Group.

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

Now I probably see what you want to achive. You want to replace this part of Table.Group. Is that correct?

dufoq3_0-1725568025829.png

If yes, you can just delete that part of Table.Group and use Table.Combine to preserve column types, like this:

 

let
    src=Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("lZBBCsMwDAT/4nMiVivLtvSO3EL+/4267aUlhWJdBIJhR3ueZUSUrRD0uXaFjjCprfX3te6IHfVAJDxJ0WaYU67tg60v1jkqhCugzYXlJOuNTuntm1RPjGQVRL+Tr//oGOai3WyF/aGpI9GTsytiRdOPmaNMhlhd1PzH3jT9UOQslE08FjVnzpN0ib6seWevBw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [id = _t, year = _t, amount = _t, ing_est = _t]),
    ct = Table.TransformColumnTypes(src,{{"id", Int64.Type}, {"year", Int64.Type}, {"amount", type number}, {"ing_est", type datetime}}, "en-US"),
    GroupedRows = Table.Group(ct, {"id"}, {{"ad", each _, type table}}),
    Combined = Table.Combine(GroupedRows[ad])
in
    Combined

Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Smashing !!!  Any performance implication you may think of for a large table?

Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs

No, there shouldn't be any. Another advantage of Table.Combine is that it will expand each column of every table dynamicaly, besides if you Expand Columns via button - it will hardcode column names (but only for first table if I'm correct).


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

Could you provide expected result based on sample data please?


Note: Check this link to learn how to use my query.
Check this link if you don't know how to provide sample data.

this is the end result I want to achieve once the schema is dynamically generated and passed on to Table.Group

 

smpa01_0-1725567986426.png

 

The challenge is to dynamically somehow replace the following portion

[id=nullable Int64.Type, year=nullable Int64.Type, amount=nullable number, ing_est=nullable datetime]

 with a function call in the code below

let
    src=Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("lZBBCsMwDAT/4nMiVivLtvSO3EL+/4267aUlhWJdBIJhR3ueZUSUrRD0uXaFjjCprfX3te6IHfVAJDxJ0WaYU67tg60v1jkqhCugzYXlJOuNTuntm1RPjGQVRL+Tr//oGOai3WyF/aGpI9GTsytiRdOPmaNMhlhd1PzH3jT9UOQslE08FjVnzpN0ib6seWevBw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [id = _t, year = _t, amount = _t, ing_est = _t]),
    ct = Table.TransformColumnTypes(src,{{"id", Int64.Type}, {"year", Int64.Type}, {"amount", type number}, {"ing_est", type datetime}}),
    #"Grouped Rows" = Table.Group(ct, {"id"}, {{"ad", each _, type table [id=nullable Int64.Type, year=nullable Int64.Type, amount=nullable number, ing_est=nullable datetime]}}),
    #"Expanded ad" = Table.ExpandTableColumn(#"Grouped Rows", "ad", {"id", "year", "amount", "ing_est"}, {"id.1", "year", "amount", "ing_est"})
in
    #"Expanded ad"
Did I answer your question? Mark my post as a solution!
Proud to be a Super User!
My custom visualization projects
Plotting Live Sound: Viz1
Beautiful News:Viz1, Viz2, Viz3
Visual Capitalist: Working Hrs
PwerQueryKees
Impactful Individual
Impactful Individual

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.