Forum Discussion
dim_Range? One set of time ranges that classify multiple different columns
You can use a custom function in Power Query to apply this logic to multiple columns at once. Here's how you can do it:
In the Power Query Editor, click on the "View" tab and select "Advanced Editor".
In the Advanced Editor, paste the following code:
let
Source = <Your_Source_Table>,
durationColumns = List.Select(Table.ColumnNames(Source), each Text.StartsWith(_, "xduration")),
toRange = (duration as number) =>
if duration <= 0.25 then "00:01-00:15"
else if duration <= 0.5 then "00:16-00:30"
else if duration <= 0.75 then "00:31-00:45"
else if duration <= 1 then "00:46-01:00"
else "Other",
toRangeTable = (column as text) =>
let
columnRange = Table.AddColumn(Source, column & "_Range", each toRange([column])),
columnSelected = Table.SelectColumns(columnRange, column, column & "_Range")
in
columnSelected,
outputTable = List.Accumulate(durationColumns, Source, (t, c) => Table.Combine({t, toRangeTable(c)}))
in
outputTableReplace <Your_Source_Table> in the code above with the name of your source table.
Click on the "Done" button to close the Advanced Editor.
The code above will create a custom function called "toRange" that maps the decimal number durations to the desired ranges. It also creates another custom function called "toRangeTable" that adds a new column to the source table with the range values.
Finally, the code uses the "List.Accumulate" function to apply the "toRangeTable" function to all columns that start with "xduration", and then combine the resulting tables into a single output table.
By using this custom function, you can easily apply the range logic to multiple duration columns at once, and the refresh performance should still be efficient.
What's the easiest way to use custom column names instead? They don't all start with xduration, that was just written as an example.
Edit : Did a test on a few columns by changing xduration to bin and get this error :
"Expression.Error: We cannot convert the value "bin_on_scene_duratio..." to type Number.
Details:
Value=bin_on_scene_duration_Range
Type=[Type]"
Adamboer