Transformations are schema objects, and a very powerful tool that MicroStrategy offers to modify the results of a metric, think about the classic "this month versus previous month" report. But it's not only that. Normally, when the SQL statement is generated, the fact table is joined to the dimension tables using the ID forms of the attributes on the grid.
The transformation allows altering the join path between fact and dimension by introducing an extra step. This extra step could be a helper table, or could be a calculation. Think about the SalesAmount
in the FactResellerSales
table: when we join the fact to the DimDate
table on the OrderDateKey
, we get the amount of sales for that particular day. By introducing a helper table we can alter the date so that we get the same number but with a different date, for example one month earlier.
We will see an example in a moment.
We need to create a helper table to calculate the previous month for every month in the DimDate
table. Bring up the sqlcmd
window and type this command (find it in the companion code file):
select distinct CalendarYearMonth, CONVERT(VARCHAR(7), DATEADD(month, -1, FullDAteAlternateKey), 111) PreviousCalendarYearMonth into DimDateMonthTransform from DimDate go
This statement creates a new table called DimDateMonthTransform
with two columns: CalendarYearMonth
and PreviousCalendarYearMonth
.
Now we add the new table to the project schema and create the transformation:
Previous Month
. Update the schema.Sum SalesAmount from FactResellerSales (Previous Month)
and click on Save.16 FactResellerSales SalesAmount Previous Month
.Congratulations, you have completed this chapter!
If you look at the SQL view, you will see that the first column is selected joining FactResellerSales to DimDate directly on (a11.OrderDateKey = a12.DateKey) and grouping on CalendarYearMonth. While the second metric is selected joining FactResellerSales on (a11.OrderDateKey = a12.DateKey) to DimDate first and next to DimDateMonthTransform on (a12.CalendarYearMonth = a13.PreviousCalendarYearMonth); the number is then aggregated grouping on CalendarYearMonth that comes from the helper table, returning in fact the following CalendarYearMonth.
When the two datasets are merged on the grid based on the value of the month, the first column shows the current month, while the second column is shifted one row forward.
The interesting thing is that you can cascade transformations. If you add an extra Previous month transformation to the metric, you get the result of two months ago, and so on. Isn't this exciting? (Not really…)
Creating a Previous Year transformation with the Year attribute it is even easier. Since year is just a number we don't need a helper table, we simply specify that the transformation is (Year -1)
.
Try it yourself:
CalendarYear – 1
.Last Year
and create a new SalesAmount from FactResellerSales (Previous Year) metric with this transformation, using the original SalesAmount from FactResellerSales.