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

July 28 - August 9 | Final Round of the Power BI Dataviz World Championships. This is your chance. 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
Fabric Community Sticker Design Challenge Barcelona Carousel

Fabric Community Sticker Challenge - Barcelona 2026

If you love stickers, then you will definitely want to check out our community sticker challenge, Barcelona edition!

July Power BI Update Carousel

Power BI Monthly Update - July 2026

Check out the July 2026 Power BI update to learn about new features.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.