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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

How to subtract columns values from two different tables using power query?

Hello,

 

I have two tables:

 

| Income |              | Expenses |

  --------                   ---------

|  Date    |               |   Date    |

| Value    |               |   Value   |

 

Using M, I want to create a new table, like this:

 

| Difference |

  -----------  

|     Date      |

|    Value      |

 

Where the Difference's Value is the Income's Value - Expenses's Value where Income's Date is equal to Expenses's Date.

 

One problem is: 

A date may not contain a revenue; A date may not contain a expense.

 

This is something similar to my data:

 

Sem título.png

 

 

 How can I calculate the diference of incomes and expenses even when there is no incomes or expenses in date? In other words, all date's incomes should be added to it and all date's expenses should be subtracted from it.

 

Thanks.

2 ACCEPTED SOLUTIONS
Anonymous
Not applicable

Hi, @Greg_Deckler, I tried your solution, but I realized that I have a bigger problem, so I edited my question with the new problem.

View solution in original post

I believe that you would want to do a group by on Date for both of your Income and Expenses tables and then do your Merge as described.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

5 REPLIES 5
Greg_Deckler
Super User
Super User

You would do a Merge query based on Date columns, create a calculated column to do the subtraction and then remove the Income Values and Expenses Values columns.

 

let
    Source = Table.NestedJoin(Income,{"Date"},Expenses,{"Date"},"NewColumn",JoinKind.LeftOuter),
    #"Expanded NewColumn" = Table.ExpandTableColumn(Source, "NewColumn", {"Value"}, {"NewColumn.Value"}),
    #"Added Custom" = Table.AddColumn(#"Expanded NewColumn", "Net", each [Value] - [NewColumn.Value]),
    #"Removed Columns" = Table.RemoveColumns(#"Added Custom",{"Value", "NewColumn.Value"})
in
    #"Removed Columns"

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

Hi, @Greg_Deckler, I tried your solution, but I realized that I have a bigger problem, so I edited my question with the new problem.

I believe that you would want to do a group by on Date for both of your Income and Expenses tables and then do your Merge as described.


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...
Anonymous
Not applicable

@Greg_Deckler, It worked, I merged it using a full outer join and expanded the new table.

Awesome!


@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.