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

Change column types based on rows and function created

Hi Guys,

 

I have 2 tables, one with ColumnTypes:

TableNameColumnTypes

TableTest

{"Col1",type text}, {"Col2",Int64.Type}, {"Col3",type logical}

Table3

{"Col1",type text}, {"Col2",type Int64.Type}, {"Col3", type number}, {"Col4", type logical}

 

And SourceTable - "TestTable":

Col1Col2Col3

55

006

TRUE

66

SomeString

FALSE

 

and now i have 3 functions.

First one is to "getListOfOccurences" to get specific types for each column


Second one "fnTypes" to replace string to specific Type value. 

It is just specyfing what column Type we have - converting String to Type. 

And last one : fnGetListOfTypes it is just a filter to get proper table:

 

and  the result of all these functions i am getting table :

TableNameColumnTypesCustom

TableTest

{"Col1",type text}, {"Col2",Int64.Type}, {"Col3",type logical}

[List]

Table3

{"Col1",type text}, {"Col2",type Int64.Type}, {"Col3", type number}, {"Col4", type logical}

[List]

 

where i have all types within list for each table. 


And now as a result query where i am using dynamic Table.TransformColumnTypes function:

 

 

let Source = Excel.CurrentWorkbook(){[Name="TableTest"]}[Content], #"Changed Type" = Table.TransformColumnTypes(Source,fnGetListOfTypes("TableTest")) in #"Changed Type"

 

I am getting:

jaryszek_0-1633684684443.png

 

error in second line because i am trying to change String to Number! 

I tried to use pattern:

 

= Table.TransformColumnTypes(Source,each try Int64.From(_) otherwise fnGetListOfTypes("TableTest"))

 

 

Maybe i am making this too complicated. I should just replace "006" to 6 as use it as any. But still i would have to know which column i should apply change. Like check each column if Type = number then remove leading zeros. 

 

but it is failing.Please help,
Best Wishes,
Jacek

1 ACCEPTED SOLUTION

thank you for taking the topic. 

I managed to solve the issue using

 

 

let
InputTable = "TableTest",
Source = Excel.CurrentWorkbook(){[Name=InputTable]}[Content],
TableType = Table.SelectRows(Excel.CurrentWorkbook(){[Name="TableType"]}[Content], each [TableName] = InputTable),
TableTypeList = Table.AddColumn(TableType, "Custom", each fnGetListOfOccurences([ColumnTypes],"{","}")),
Transformation = List.Accumulate(List.Numbers(0,Table.ColumnCount(Source)), Source, (state, index) => Table.TransformColumns(state,{{Table.ColumnNames(Source){index}, each if TableTypeList[Custom]{0}{index}{1}="Int64.Type" or TableTypeList[Custom]{0}{index}{1}="type number" then try Int64.From(_) otherwise _ else _}})),

in 

Transformation

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

Hi @jaryszek ,

 

Sorry, I don't understand much. Is your sample data the data in the screenshot below? What is the expected result, please provide.

vstephenmsft_0-1633936110044.png

 

 

You can detect data type in power query:

https://docs.microsoft.com/en-us/power-query/data-types#data-type-detection

 

Or define a column data type:

https://docs.microsoft.com/en-us/power-query/data-types#how-to-define-a-column-data-type

 

 

 

 

Best Regards,

Stephen Tao

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

thank you for taking the topic. 

I managed to solve the issue using

 

 

let
InputTable = "TableTest",
Source = Excel.CurrentWorkbook(){[Name=InputTable]}[Content],
TableType = Table.SelectRows(Excel.CurrentWorkbook(){[Name="TableType"]}[Content], each [TableName] = InputTable),
TableTypeList = Table.AddColumn(TableType, "Custom", each fnGetListOfOccurences([ColumnTypes],"{","}")),
Transformation = List.Accumulate(List.Numbers(0,Table.ColumnCount(Source)), Source, (state, index) => Table.TransformColumns(state,{{Table.ColumnNames(Source){index}, each if TableTypeList[Custom]{0}{index}{1}="Int64.Type" or TableTypeList[Custom]{0}{index}{1}="type number" then try Int64.From(_) otherwise _ else _}})),

in 

Transformation
jaryszek
Resident Rockstar
Resident Rockstar

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.