Skip to content

Instantly share code, notes, and snippets.

@ImkeF
Last active March 14, 2023 10:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save ImkeF/5bc77778d1eea2d8bcff209838ff03ef to your computer and use it in GitHub Desktop.
Save ImkeF/5bc77778d1eea2d8bcff209838ff03ef to your computer and use it in GitHub Desktop.
Creates the M-code for an empty table based on a table schema (Table.Schema).
let
fnFormatted = let
func = (myTable as table) =>
let
#"Table1 Schema" = Table.Schema(myTable),
TypesList = Table.AddColumn(
#"Table1 Schema",
"TypeRecord",
each [Name] & "=" & [TypeName]
)[TypeRecord],
TypeRecordString = "[" & Text.Combine(TypesList, ", ") & "]",
TableString = "#table(type table "
& TypeRecordString
& ", {{"
& Text.Combine(List.Repeat({"null"}, List.Count(TypesList)), ", ")
& "}})"
in
TableString,
documentation = [
Documentation.Name = " Text.CreateEmptyTableFromSchema ",
Documentation.Description
= " Creates the M-code for an empty table based on a table schema. ",
Documentation.LongDescription
= " Creates the M-code for an empty table based on a table schema (Table.Schema). ",
Documentation.Category = " Text.Transformations ",
Documentation.Source = " www.TheBIcountant.com https://wp.me/p6lgsG-2tJ . ",
Documentation.Version = " 1.0 ",
Documentation.Author = " Imke Feldmann ",
Documentation.Examples = {
[Description = " ",
Code =
" let
myTable = #table( type table [myText = Text.Type, myNumber = Int64.Type, myDate = Date.Type],
// myText| myNumber| myDate|
{//-------|---------|------------------|
{ ""A"", 10, #date(2022, 01, 01) } } ),
FunctionCall = fnText_CreateEmptyTableFromSchema( myTable )
in
FunctionCall "
,
Result = " #table(type table [myText=Text.Type, myNumber=Int64.Type, myDate=Date.Type], {{null, null, null}}) "
]}]
in
Value.ReplaceType(func, Value.ReplaceMetadata(Value.Type(func), documentation))
in
fnFormatted
@TinyToons
Copy link

Hi @ImkeF
Thank you for this very useful function that helps me to handle query errors and compare table schemas.
I forked it to quote the column's name to handle names containing spaces and special characters. What do you think about that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment