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

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a $100 discount. Register Now

Reply
Syndicate_Admin
Administrator
Administrator

insert multiple columns that subtract previous columns

Hello - I have 50+ columns that I need to calculate the difference after every 2 columns. Currently I appended my tables in Power Query and it looks like this:

 DRAFT  ACTUAL  DRAFT  ACTUAL 
                  10                  15                  20                  10
                     5                     4                  30                100
                     1                     2                  40                  50

I need it to do this:

 DRAFT  ACTUAL  DIFF  DRAFT  ACTUAL  DIFF 
                  10                  15                   (5)                  20                  10                  10
                     5                     4                     1                  30                100                 (70)
                     1                     2                   (1)                  40                  50                 (10)

 

I can add a custom column in Power query but it shows up at the end of the table and then I have to move it. Also, it is very manual and I have to do this for 50+ columns on 50+ files.

1 ACCEPTED SOLUTION

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

vcgaomsft_0-1696219874158.png

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

View solution in original post

2 REPLIES 2
Syndicate_Admin
Administrator
Administrator

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

vcgaomsft_0-1696219874158.png

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

Helpful resources

Announcements
Fabric Community Conference

Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

February 2024 Update Carousel

Power BI Monthly Update - February 2024

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

Fabric Career Hub

Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

Fabric Partner Community

Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors
Top Kudoed Authors