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

Join the FabCon + SQLCon recap series. Up next: Power BI, Real-Time Intelligence, IQ and AI, and Data Factory take center stage. All sessions are available on-demand after the live show. Register now

Reply
smpa01
Community Champion
Community Champion

Applying schema programatically

I have a source table from which I have retrievd schema. How can I apply the same schema programatically from a source table to a target table.

@AlexisOlson 

For example,

 

//src
let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXIEYiMDIxNdQyNdQ0Ol2FgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [colA = _t, colB = _t, colC = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"colA", Int64.Type}, {"colB", type text}, {"colC", type date}}),
    Custom1 = Table.Schema(#"Changed Type"),
    #"Removed Columns" = Table.RemoveColumns(Custom1,{"Kind", "IsNullable", "NumericPrecisionBase", "NumericPrecision", "NumericScale", "DateTimePrecision", "MaxLength", "IsVariableLength", "NativeTypeName", "NativeDefaultExpression", "NativeExpression", "Description", "IsWritable", "FieldCaption"})
in
    #"Removed Columns"

//target
let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXIEYiMDIxNdQyNdQ0Ol2FgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [colA = _t, colB = _t, colC = _t]),
    Custom1 = Table.ColumnNames(Source),
    #"Converted to Table" = Table.FromList(Custom1, Splitter.SplitByNothing(), null, null, ExtraValues.Error),
    #"Merged Queries" = Table.NestedJoin(#"Converted to Table", {"Column1"}, src, {"Name"}, "src", JoinKind.LeftOuter),
    #"Expanded src" = Table.ExpandTableColumn(#"Merged Queries", "src", {"TypeName"}, {"TypeName"}),
    #"Added Custom" = Table.AddColumn(#"Expanded src", "Custom", each {[Column1],[TypeName]}),
    Custom = #"Added Custom"[Custom],
    Custom2 = Table.TransformColumnTypes(Source,Custom)
in
    Custom2

//desired
let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WMlTSUXIEYiMDIxNdQyNdQ0Ol2FgA", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [colA = _t, colB = _t, colC = _t]),
    Custom1 = Table.TransformColumnTypes(Source,{{"colA", Int64.Type}, {"colB", Text.Type}, {"colC", Date.Type}})
in
    Custom1

 

Thank you in advance.

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
m_dekorte
Resident Rockstar
Resident Rockstar

Hi @smpa01 

 

You can use Expression evaluate and specify an environment, to illustrate:

 

     #"Added Custom" = Table.AddColumn(#"Expanded src", "Custom", each 
            { [Column1], Expression.Evaluate([TypeName], 
                [   #"Any.Type" = Any.Type,
                    #"Date.Type" = Date.Type,
                    #"DateTime.Type" = DateTime.Type,
                    #"DateTimeZone.Type" = DateTimeZone.Type,
                    #"Day.Type" = Day.Type,
                    #"Duration.Type" = Duration.Type,
                    #"Logical.Type" = Logical.Type,
                    #"Currency.Type" = Currency.Type,
                    #"Decimal.Type" = Decimal.Type,
                    #"Double.Type" = Double.Type,
                    #"Int16.Type" = Int16.Type,
                    #"Int32.Type" = Int32.Type,
                    #"Int64.Type" = Int64.Type,
                    #"Int8.Type" = Int8.Type,
                    #"Number.Type" = Number.Type,
                    #"Percentage.Type" = Percentage.Type,
                    #"Single.Type" = Single.Type,
                    #"Text.Type" = Text.Type,
                    #"Time.Type" = Time.Type
                ] )}
            ),

 

I hope this is helpful

 

View solution in original post

1 REPLY 1
m_dekorte
Resident Rockstar
Resident Rockstar

Hi @smpa01 

 

You can use Expression evaluate and specify an environment, to illustrate:

 

     #"Added Custom" = Table.AddColumn(#"Expanded src", "Custom", each 
            { [Column1], Expression.Evaluate([TypeName], 
                [   #"Any.Type" = Any.Type,
                    #"Date.Type" = Date.Type,
                    #"DateTime.Type" = DateTime.Type,
                    #"DateTimeZone.Type" = DateTimeZone.Type,
                    #"Day.Type" = Day.Type,
                    #"Duration.Type" = Duration.Type,
                    #"Logical.Type" = Logical.Type,
                    #"Currency.Type" = Currency.Type,
                    #"Decimal.Type" = Decimal.Type,
                    #"Double.Type" = Double.Type,
                    #"Int16.Type" = Int16.Type,
                    #"Int32.Type" = Int32.Type,
                    #"Int64.Type" = Int64.Type,
                    #"Int8.Type" = Int8.Type,
                    #"Number.Type" = Number.Type,
                    #"Percentage.Type" = Percentage.Type,
                    #"Single.Type" = Single.Type,
                    #"Text.Type" = Text.Type,
                    #"Time.Type" = Time.Type
                ] )}
            ),

 

I hope this is helpful

 

Helpful resources

Announcements
April Power BI Update Carousel

Power BI Monthly Update - April 2026

Check out the April 2026 Power BI update to learn about new features.

New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

FabCon and SQLCon Highlights Carousel

FabCon &SQLCon Highlights

Experience the highlights from FabCon & SQLCon, available live and on-demand starting April 14th.