Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TinyToons/6ec9259e5aed2d97dbeb70fb076a8ea4 to your computer and use it in GitHub Desktop.
Save TinyToons/6ec9259e5aed2d97dbeb70fb076a8ea4 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
& ", {})"
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 with no rows based on a table schema (Table.Schema). Modified by Adel SASSI to return a table with no rows and to manage spaces and special characters in column names.",
Documentation.Category = " Text.Transformations ",
Documentation.Source = " www.TheBIcountant.com https://wp.me/p6lgsG-2tJ . ",
Documentation.Version = " 1.1 ",
Documentation.Author = " Imke Feldmann - Tailored by Adel SASSI ",
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
Author

Revision 3:
Column names are quoted to handle names containing spaces or special characters.

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