Forum Discussion
insert multiple columns that subtract previous columns
- Anonymous2 years ago
Hi ,
Please refer to this:let Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("JYqxDQAwCMN+ydyhQOkziP/fKEkXK5FdBdtYsBy4lmCDCPQq0Bzeb8lU7dIsnYkEcVjfi+4H", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [DRAFT = _t, ACTUAL = _t, DRAFT.1 = _t, ACTUAL.1 = _t, DRAFT.2 = _t, ACTUAL.2 = _t]), ColumnNames = Table.ColumnNames(Source), ChangeType = Table.TransformColumnTypes(Source, List.Transform(ColumnNames, each {_, type number})), AddDiffColumns = List.Accumulate( List.Zip({{0..List.Count(ColumnNames)-1}, ColumnNames}), ChangeType, (state, current) => if Number.Mod(current{0}, 2) = 1 then let diffColumnName = "DIFF" & Text.From(Number.IntegerDivide(current{0}, 2) + 1), draftColumnName = ColumnNames{current{0}-1}, actualColumnName = current{1}, addedDiffColumn = Table.AddColumn(state, diffColumnName, each Record.Field(_, draftColumnName) - Record.Field(_, actualColumnName), type number) in addedDiffColumn else state ) in AddDiffColumnsBest Regards,
Gao
Community Support TeamIf there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!How to get your questions answered quickly -- How to provide sample data in the Power BI Forum
I should add that each of my files has a different number of columns as well. So if I can automate to add this difference column for any number of iterations as there is data, that is what I want to do.
Hi ,
Please refer to this:
let
Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("JYqxDQAwCMN+ydyhQOkziP/fKEkXK5FdBdtYsBy4lmCDCPQq0Bzeb8lU7dIsnYkEcVjfi+4H", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [DRAFT = _t, ACTUAL = _t, DRAFT.1 = _t, ACTUAL.1 = _t, DRAFT.2 = _t, ACTUAL.2 = _t]),
ColumnNames = Table.ColumnNames(Source),
ChangeType = Table.TransformColumnTypes(Source, List.Transform(ColumnNames, each {_, type number})),
AddDiffColumns = List.Accumulate(
List.Zip({{0..List.Count(ColumnNames)-1}, ColumnNames}),
ChangeType,
(state, current) =>
if Number.Mod(current{0}, 2) = 1 then
let
diffColumnName = "DIFF" & Text.From(Number.IntegerDivide(current{0}, 2) + 1),
draftColumnName = ColumnNames{current{0}-1},
actualColumnName = current{1},
addedDiffColumn = Table.AddColumn(state, diffColumnName, each Record.Field(_, draftColumnName) - Record.Field(_, actualColumnName), type number)
in
addedDiffColumn
else
state
)
in
AddDiffColumns
Best Regards,
Gao
Community Support Team
If there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!
How to get your questions answered quickly -- How to provide sample data in the Power BI Forum