In DAX can I filter by the previous item in a list of Strings?

cr731

Well-known Member
Joined
Sep 17, 2010
Messages
611
I have a table with a column representing a period number (formatted as YYYYMM) followed by a date (formatted as MM-DD-YY), which represent the period the data relates to and the date stamp of when that record was created.

Something like,

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Period[/TD]
[TD]Value[/TD]
[/TR]
[TR]
[TD]201701 (01-15-17)[/TD]
[TD]10[/TD]
[/TR]
[TR]
[TD]201701 (01-20-17)[/TD]
[TD]15[/TD]
[/TR]
[TR]
[TD]201702 (02-05-17)[/TD]
[TD]20[/TD]
[/TR]
</tbody>[/TABLE]


I want to create two calculated measures, one as a simple SUM of the Value column, and another that is the sum of the previous item in the Period column (based on an ascending sort of the list).

So based on the above example it would be like this,

[TABLE="class: grid, width: 500"]
<tbody>[TR]
[TD]Period[/TD]
[TD]Sum of Value[/TD]
[TD]Sum of Previous Value[/TD]
[/TR]
[TR]
[TD]201701 (01-15-17)[/TD]
[TD]10[/TD]
[TD][/TD]
[/TR]
[TR]
[TD]201701 (01-20-17)[/TD]
[TD]15[/TD]
[TD]10[/TD]
[/TR]
[TR]
[TD]201702 (02-05-17)[/TD]
[TD]20[/TD]
[TD]15[/TD]
[/TR]
</tbody>[/TABLE]

Can this be done or would I have to create an index on my Period column so the values are numbers?

Thanks
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
convert your data to Table format. menu PowerQuery > click From Table/Range > click menu Add Column in Query Editor > click Index Column drop down menu > select From 0


above will add column Index to your table in Query Editor (syntax in Advanced Editor #"Added Index" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1)


remove the space from #"Added Index" in Advanced Editor
place code #"PrvVal" = Table.AddColumn(#"AddedIndex","PrvValue",each try AddedIndex[Value]{[Index]-1} otherwise 0)


let
Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
#"Changed Type" = Table.TransformColumnTypes(Source,{{"Date", type date}, {"Sales", Int64.Type}}),
#"AddedIndex" = Table.AddIndexColumn(#"Changed Type", "Index", 0, 1),
#"PrvVal" = Table.AddColumn(#"AddedIndex","PrvValue",each try AddedIndex[Sales]{[Index]-1} otherwise 0)
in
#"PrvVal"


i think above solves your problem
 
Upvote 0

Forum statistics

Threads
1,225,738
Messages
6,186,728
Members
453,368
Latest member
positivemind

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