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

We've captured the moments from FabCon & SQLCon that everyone is talking about, and we are bringing them to the community, live and on-demand. Starts on April 14th. Register now

Reply
ChemEnger
Advocate V
Advocate V

Dynamically apply column transformations

I have a 'data' table where I'd like to dynamically change the number of decimal places.

I use a 'dictionary' table that holds the names of the columns in 'data' along with the desired number of decimal places, e.g.

ColumnNameDecimal
Input2
Temperature1
Pressure0
Weight3

 

I would like to be able to perform a TransformColumns operation on 'data', such as

Table.TransformColumns(data, {{"Temperature", each Number.Round(_, 1), type number}})
But driven dynamically from the 'dictionary' table
I am thinking that looping through the number of decimal places might be a start and can easily generate a list like:
  MinDecimal = List.Min(dictionary[Decimal]),
  MaxDecimal = List.Max(dictionary[Decimal]),
  Range = List.Generate(() => MinDecimal, each _ <= MaxDecimal, each _ + 1)

It feels like I could then use a recursive function (with or without the Range list) to select one by one the columns that need each transform, something like the answer from @ImkeF  in Table Transformations for Dynamic Columns :

fnTransform = (InputTable as table, ListOfColNames, TransformFunction as function) =>
let
    TransformFunctionList = List.Transform(ListOfColNames, (x) => {x, TransformFunction}),
    Transform = Table.TransformColumns(InputTable, TransformFunctionList)
in
    Transform
// Somehow loop n = from MinDecimal to MaxDecimal
ColumnsToTransform = Table.SelectRows(dictionary, each [Decimal] = n)[ColumnName],
Transform = fnTransform(data, ColumnsToTransform, each Number.Round(_, n), type number)

but I am stumped as to the way to use a loop to make this work...

1 ACCEPTED SOLUTION
ImkeF
Community Champion
Community Champion

Hi @ChemEnger ,
it could work like so:

(InputTable as table, dictionary as table) =>
  let
    TransformFunctionList = List.Transform(
      Table.ToRecords(dictionary), 
      (record) => {record[ColumnName], each Number.Round(_, record[Decimal]), type number}
    ), 
    Transform = Table.TransformColumns(InputTable, TransformFunctionList)
  in
    Transform

 No real recursion needed. Just transform your dictionary table into a list of records and create the transformation list elements out of it.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

View solution in original post

2 REPLIES 2
ImkeF
Community Champion
Community Champion

Hi @ChemEnger ,
it could work like so:

(InputTable as table, dictionary as table) =>
  let
    TransformFunctionList = List.Transform(
      Table.ToRecords(dictionary), 
      (record) => {record[ColumnName], each Number.Round(_, record[Decimal]), type number}
    ), 
    Transform = Table.TransformColumns(InputTable, TransformFunctionList)
  in
    Transform

 No real recursion needed. Just transform your dictionary table into a list of records and create the transformation list elements out of it.

Imke Feldmann (The BIccountant)

If you liked my solution, please give it a thumbs up. And if I did answer your question, please mark this post as a solution. Thanks!

How to integrate M-code into your solution -- How to get your questions answered quickly -- How to provide sample data -- Check out more PBI- learning resources here -- Performance Tipps for M-queries

Spot on, thank you @ImkeF.

 

I was looking at something similar on Medium - Transform Multiple Columns Based On Other Columns In Power Query but your solution worked first time 🙂

Helpful resources

Announcements
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.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

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