Last week I wrote about how the Above() function can be used for calculating rolling averages and other accumulations. There is however also an alternative method for doing the same thing:
The As-Of table.
When you use the Above() function, you fetch a number from other rows in a chart or Aggr() table. The As-Of table is slightly different in this respect: It is not a transient table created by an object or an expression – instead it is a real table in the data model.
The idea is to create a secondary month field – the AsOfMonth - that links to multiple real months.
In the example above, you can see that ‘2015 Oct’ links to several preceding months, and each Month in turn links to several rows in a fact table. This means that a specific transaction will be linked to several AsOfMonths.
In the data model, the As-Of table should appear as a separate calendar table that links to the existing primary calendar table:
One way to create this table is the following:
First, make sure that you in your master calendar have a field “Month” that is defined as the first date of the month, e.g.
Date(MonthStart(Date),'YYYY MMM') asMonth,
Then add the following lines at the end of the script:
// ======== Create a list of distinct Months ======== tmpAsOfCalendar: LoaddistinctMonth Resident [Master Calendar] ;
I have made the Set Analysis expressions based on two fields: YearDiff and MonthDiff. However, for clarity it could be a good idea to add flags in the As-Of table, so that the Set Analysis expressions become even simpler, e.g.
if you tell about your specific use case, experts here can guide you more properly.
you can use button with action selection>select in field and search string to fetch all dates less than or equal to max date
As mentioned by Henric, you should see the no of records in the as of table. You can calculate the records that as-of table will have with the sum of arithmetic series
no of records = total no of dates or days * (1+no of last day ) / 2
So if you have four years of data, your as of table will have
The problem of comparing two months is not solved by the As-of table, but it can be solved in a number of other ways. I would use YearMonth as dimension, e.g.
Date(MonthStart(Date),'YYYY-MMM') as YearMonth
and then calculate a change from previous month using
Sum(Sales) - Above(Sum(Sales))
This way you will get the difference between Feb and Jan to show up as the Feb number.
I'm looking to make the calculation work with multiple dimensions in a custom pivot or ClimberCustomReport in Sense where dimensions can be added and removed. I think aggr and above can get the desired result, but I was hoping the expression could be fluid and work with multiple dimensions. Below is what I'm trying to do using PowerBi and the PREVIOUSMONTH function.
Current month versus previous month can be done with an As-Of table. For the current month the value of MonthDiff is 0, for the previous month it will be 1.
Current month: Sum({$<MonthDiff={0}>} Sales)
Previous month:Sum({$<MonthDiff={1}>} Sales)
You could also add flag fields for this in the As-Of table: