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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
dmned-ph
New Member

changing column data type based on the suffix in the column name

I have a query template that returns results from sources wich can differ in the number and names of the columns. For example,, the columns may be thus:

 

time

value.1.a

value.1.b

value.2.a

value.2.b

value.3.on

...

 

The results always return a column named 'time' that I can easily convert to a Date/Time data type. The query results always produce some quantity of values with the 'a', 'b' or 'on' suffix, but the number and prefix names can vary. I am looking for a method to change the data type of all the columns with the 'a' or 'b' suffix to a decimal number and the columns with the 'on' suffix to a True/False (boolean) type.

 

Is there a method to select the columns by their suffix and then apply a transformation based on the selection?

1 ACCEPTED SOLUTION
ronrsnfld
Super User
Super User

Not entirely foolproof, but more checking can be added if necessary:

 

#"Changed Type" = Table.TransformColumnTypes(Source,
        List.Transform(Table.ColumnNames(Source), each 
            let 
                split = Text.Split(_,"."),
                types = if _ = "time" then type time
                    else if List.Last(split) = "a" then type number  
                    else if List.Last(split) = "b" then type number 
                    else if List.Last(split) = "on" then type logical else type any
                in 
                    {_, types}))

View solution in original post

2 REPLIES 2
ronrsnfld
Super User
Super User

Not entirely foolproof, but more checking can be added if necessary:

 

#"Changed Type" = Table.TransformColumnTypes(Source,
        List.Transform(Table.ColumnNames(Source), each 
            let 
                split = Text.Split(_,"."),
                types = if _ = "time" then type time
                    else if List.Last(split) = "a" then type number  
                    else if List.Last(split) = "b" then type number 
                    else if List.Last(split) = "on" then type logical else type any
                in 
                    {_, types}))

Thank you...this worked exactly as expected for my query, and it allowed me to learn how to use a nested LET statement. The only tweak I made was my time column includes the date, so I changed the type to datetime.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors
Top Kudoed Authors