cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
Highlighted
New Member

Difference column from a match lookup?

Hello,

I am not familiar enough with DAX to pull this off.

I have a table:

Date          Product     Item     % Usage of item

1/1/2020Product 1Water60
1/1/2020Product 1Salt40
1/1/2020Product 2Water30
1/1/2020Product 2Salt70
1/2/2020Product 1Water20
1/2/2020Product 1Salt80
1/2/2020Product 2Water45
1/2/2020Product 2Salt

55

 I am adding a column and I need to work out the diffrence between each item used from the previous Date (Water, Salt) . but filted out for each product on the second date entry (1/2/2020) like so:

Difference from previous run
-40
40
-15
-15

So getting the 20 from 1/2/2020, Product 1, Water and then deducting. 60 from 1/1/2020, Product 1 Water. to make -40 and so on

What Forumla would I use in DAX to pull this off when I add Custom Column in the Query Editor?

Appreciate your help in advance

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Highlighted
Community Support
Community Support

Re: Difference column from a match lookup?

hi  @DH102 

You need to knowledge that DAX is used to create a new Calculate Column/Table/Measure.

https://docs.microsoft.com/en-us/power-bi/transform-model/desktop-tutorial-create-calculated-columns

1.JPG

M code is used in power query to create a custom column

2.JPG

 

https://community.powerbi.com/t5/Desktop/Dax-or-M-Language/td-p/136827

 

Then you could use this dax formula to create a new column

 

Difference from previous run = 
VAR previousdate=  CALCULATE(MAX('Table'[Date]),FILTER('Table', [Date]<EARLIER([date]) && [Product]=EARLIER([Product])&& [item]=EARLIER([item]))) return
'Table'[% Usage of item]-CALCULATE(SUM('Table'[% Usage of item]),FILTER('Table',[date]=previousdate && [Product]=EARLIER([Product])&& [item]=EARLIER([item])))

 

or

 

Difference from previous run 2 = 
VAR previousdate=  CALCULATE(MAX('Table'[Date]),FILTER('Table', [Date]<EARLIER([date]) && [Product]=EARLIER([Product])&& [item]=EARLIER([item]))) return
IF(ISBLANK(previousdate),BLANK(),'Table'[% Usage of item]-CALCULATE(SUM('Table'[% Usage of item]),FILTER('Table',[date]=previousdate && [Product]=EARLIER([Product])&& [item]=EARLIER([item]))))

 

 

Regards,

Lin

Community Support Team _ Lin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

3 REPLIES 3
Highlighted
Super User IV
Super User IV

Re: Difference column from a match lookup?

@DH102 , both as new columns

last Date = maxx(filter(table, [date]<earlier([date]) && [Product]=earlier([Product])&& [item]=earlier([item])),[date])
[% Usage of item] - maxx(filter(table, [date] =earlier([last Date]) && [Product]=earlier([Product])&& [item]=earlier([item])),[% Usage of item])



Did I answer your question? Mark my post as a solution! Appreciate your Kudos!!
My Recent Blog -Week is not so Weak Connect on Linkedin

Proud to be a Super User!

Highlighted
New Member

Re: Difference column from a match lookup?

I must be making an elementary error

It gives me 

https://imgur.com/g3Y5Ki0

Tolken Literal

 

Highlighted
Community Support
Community Support

Re: Difference column from a match lookup?

hi  @DH102 

You need to knowledge that DAX is used to create a new Calculate Column/Table/Measure.

https://docs.microsoft.com/en-us/power-bi/transform-model/desktop-tutorial-create-calculated-columns

1.JPG

M code is used in power query to create a custom column

2.JPG

 

https://community.powerbi.com/t5/Desktop/Dax-or-M-Language/td-p/136827

 

Then you could use this dax formula to create a new column

 

Difference from previous run = 
VAR previousdate=  CALCULATE(MAX('Table'[Date]),FILTER('Table', [Date]<EARLIER([date]) && [Product]=EARLIER([Product])&& [item]=EARLIER([item]))) return
'Table'[% Usage of item]-CALCULATE(SUM('Table'[% Usage of item]),FILTER('Table',[date]=previousdate && [Product]=EARLIER([Product])&& [item]=EARLIER([item])))

 

or

 

Difference from previous run 2 = 
VAR previousdate=  CALCULATE(MAX('Table'[Date]),FILTER('Table', [Date]<EARLIER([date]) && [Product]=EARLIER([Product])&& [item]=EARLIER([item]))) return
IF(ISBLANK(previousdate),BLANK(),'Table'[% Usage of item]-CALCULATE(SUM('Table'[% Usage of item]),FILTER('Table',[date]=previousdate && [Product]=EARLIER([Product])&& [item]=EARLIER([item]))))

 

 

Regards,

Lin

Community Support Team _ Lin
If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

View solution in original post

Helpful resources

Announcements
Community Blog

Community Blog

Visit our Community Blog for articles, guides, and information created by fellow community members.

Using the Community

Using the Community

Need help with the Power BI Community? Our 'Using the Community' support articles are a great place to start.

Community Summit North America

Community Summit North America

Innovate, Collaborate, Grow. The top training and networking event across the globe for Microsoft Business Applications

Power Platform 2020 release wave 2 plan

Power Platform 2020 release wave 2 plan

Features releasing from October 2020 through March 2021

Top Solution Authors
Top Kudoed Authors