-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDynamicAddColumnAndMove
More file actions
51 lines (42 loc) · 2.79 KB
/
DynamicAddColumnAndMove
File metadata and controls
51 lines (42 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
Adding a new column in Power Query will usually add the column to the end of the dataset and while that does not typically cause an error,
moving the column to a different position in a dynamic dataset will cause an error if the column names aren't exactly the same as the
names it started with. To circumvent the error:
1. Add column
2. Get all of the columns within the power query dataset and put into a list container
3. Reorder column in power query AHEAD of the entire list (if your volatile columns are at the back end of the data)
Website references:
https://community.powerbi.com/t5/Desktop/Query-Add-column-at-the-beginning/td-p/516184
To move Custom column to the first column:
let
...
TableData = Table.AddColumn(TransformColumnTypes, "Half Year", each if [Month] < 7 then 1 else 2),
ColumnNames = Table.ColumnNames(TableData),
ReorderedList = List.Combine({{"Half Year"},List.FirstN(ColumnNames,List.Count(ColumnNames)-1)}),
Result = Table.ReorderColumns(TableData,ReorderedList)
in
Result
where
- TableData is the last step of your data, in my case, it is a step where I've added a new custom column
- Half Year is my custom column which I want to be first each time
- ColumnNames is a step which contains a list of columns from TableData
- ReorderedList is a step which represents column names where my custom column comes first and all other column next (does not matter how many)
- Result is a step represents sorted TableData based on ReorderedList
Alternatively, if you need to put the custom column somewhere in the middle of your data, this method splits the column headers into
two halves and lets you sandwich the custom column in between the two.
let
...
#"Added Custom" = Table.AddColumn(Source, "Custom", each ...),
// Get a list of all of the columns in the dataset after adding Custom column without the custom column
//(to allow rearrangement of custom column later)
ColumnNames = List.FirstN(Table.ColumnNames(#"Added Custom"),List.Count(Table.ColumnNames(#"Added Custom"))-1),
//Split the column names into a first half (split via column header keyword), the column headers you want BEFORE your custom column
ColumnNameFirstHalf = List.FirstN(ColumnNames,List.PositionOf(ColumnNames, "ColumnHeaderToSplitAt") +1),
//Split the column names into a second half, the column headers you want AFTER your custom column
ColumnNameSecondHalf = List.LastN(ColumnNames,List.Count(ColumnNames)-List.Count(ColumnNameFirstHalf)),
//Reorder the list to sandwich custom column between first and second halves
ReorderedList = List.Combine({ColumnNameFirstHalf,{"Custom"},ColumnNameSecondHalf}),
// Reorder the columns to follow the reordered list order
#"Reordered Columns" = Table.ReorderColumns(#"Added Custom",ReorderedList),
...
in
...