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

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
Syndicate_Admin
Administrator
Administrator

Inverse values based on condition without adding column

I am trying to inverse the values in ACTUAL, BUDGET, FORECAST columns based on a condition in Account column where if the value = "Discount".

 

Below is the formula I have written.

ConvertDiscount = Table.TransformColumns(ExpandAccountHierarchy, {
{"ACTUAL", each if [Account] = "Discount" then _ *-1 else _, type number},
{"BUDGET", each if [Account] = "Discount" then _ *-1 else _, type number},
{"FORECAST ", each if [Account] = "Discount" then _ *-1 else _, type number}},

 

But, it is giving me an error "Expression.Error: We cannot apply field access to the type Null. Details: Value= Key=Account". I have verified and ensured that the BUD, FCST & ACT columns has the type as number and Account as Text and all these columns doesn't have Null values. Can you please help me if there is something I am missing in this formula

1 ACCEPTED SOLUTION
AlienSx
Super User
Super User

Hi, deepak_shashi  

let
    Source = your_table,
    reversal = 
        Table.FromRecords(
            Table.TransformRows(
                Source, 
                (x) => 
                    [mult = (z) => if x[Account] = "Discount" then -z else z,
                    result = 
                        Record.TransformFields(
                            x, 
                            {{"ACTUAL", mult},
                            {"BUDGET", mult},
                            {"FORECAST", mult}}
                        )][result]
            )
        )
in
    reversal

 

View solution in original post

3 REPLIES 3
Syndicate_Admin
Administrator
Administrator

This is a great function, but since the volume of the data I am working with is huge it would not be ideal. Anyways now I understand that in Transform.Columns we cannot refer to 2 columns, so the ideal and the best solution would be to have separate formulas for each Column. Appreciate your help

AlienSx
Super User
Super User

Hi, deepak_shashi  

let
    Source = your_table,
    reversal = 
        Table.FromRecords(
            Table.TransformRows(
                Source, 
                (x) => 
                    [mult = (z) => if x[Account] = "Discount" then -z else z,
                    result = 
                        Record.TransformFields(
                            x, 
                            {{"ACTUAL", mult},
                            {"BUDGET", mult},
                            {"FORECAST", mult}}
                        )][result]
            )
        )
in
    reversal

 

ImkeF
Community Champion
Community Champion

Hi @Syndicate_Admin ,
you cannot reference a different column from withing the Table.TransformColumns function.
Instead, you have to use Table.ReplaceValue  :
Table.TransformColumns - alternative in PowerBI and PowerQuery in Excel (thebiccountant.com)


And if you want to do it for multiple columns at once, check out this article:
Transforming multiple columns at once with reference to existing column in Power Query (thebiccounta...

 

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

Helpful resources

Announcements
Power BI DataViz World Championships

Power BI Dataviz World Championships

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now!

December 2025 Power BI Update Carousel

Power BI Monthly Update - December 2025

Check out the December 2025 Power BI Holiday Recap!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors