Forked from ImkeF/Text.CreateEmptyTableFromSchema.pq
Last active
August 2, 2023 21:03
-
-
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).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Revision 3:
Column names are quoted to handle names containing spaces or special characters.