DAX formula for SUM of previous value in column

yurykrav

New Member
Joined
Mar 29, 2015
Messages
2
Hi!
I have a very simple (and in the same time very complicated for me) task.
I have this initial data (about 100 000 rows):

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Obervation[/TD]
[TD]Customer[/TD]
[TD]Location[/TD]
[TD]Product[/TD]
[TD]Sales[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]381101[/TD]
[TD]NY[/TD]
[TD]34221[/TD]
[TD]43241[/TD]
[/TR]
[TR]
[TD]5[/TD]
[TD]381101[/TD]
[TD]NY[/TD]
[TD]14321[/TD]
[TD]12321[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]381101[/TD]
[TD]NY[/TD]
[TD]34223[/TD]
[TD]13413[/TD]
[/TR]
[TR]
[TD]6[/TD]
[TD]381101[/TD]
[TD]NY[/TD]
[TD]65446[/TD]
[TD]13124[/TD]
[/TR]
</tbody>[/TABLE]

Very simple - observations (it can be some periods, but no link to calendar dates), Customers, Locations, Products and Sales amount

All I want is to see in calculated field sum of Sales for previous observation - like this:

oTtOTxkoc.jpg


So I need to create somehow a reference to Observation value in query context and calculate Sum of Sales
But I can only write a formula for Calculated Field using EARLIER function:

"=calculate(sum([Sales]);filter('Table';[Observation]+1=earlier([Observation])&&[Customer]=earlier([Customer])&&[Location]=earlier([Location])&&[Product]=earlier([Product])))"

It works but it works row by row and if I miss some Customer in Observation 5, I get in total less Sales in my field

Im sure that there is some very simple decision for this case

Thank!
 

Excel Facts

Format cells as date
Select range and press Ctrl+Shift+3 to format cells as date. (Shift 3 is the # sign which sort of looks like a small calendar).
Total Sales := SUM(MyTable[Sales])
Prev Sales := CALCULATE([Total Sales], FILTER(ALL(MyTable[Observation]), MyTable[Observation] = MAX(MyTable[Observation]) - 1))

May need something special for the grand total, but this should get you close.
 
Upvote 0
Total Sales := SUM(MyTable[Sales])
Prev Sales := CALCULATE([Total Sales], FILTER(ALL(MyTable[Observation]), MyTable[Observation] = MAX(MyTable[Observation]) - 1))

May need something special for the grand total, but this should get you close.

Amazing! Its very simple, thanks
 
Upvote 0

Forum statistics

Threads
1,224,084
Messages
6,176,270
Members
452,718
Latest member
Nyxs_Inquisitor

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top