I have a lot of calculation in a report which calculate stats for the "last [period of time]". Now that we have started a new year, a lot of these calculations seem broken.
I thought that these calcs would work in the new year because Dax's date/calendar math appears capable of handling math that spans different years and months. For instance, if you ask it for a date - 1 month and it the input as january, it correctly returns December. But this does not appear to be the case here.
Below you see a table detailing the numbers for certain periods of time. X1 is the average size of the population, X2 is the size of the losses in that period of time, and Ann Rate is the annualized rate of loss in that period. The periods in question are for the selected period (PRD) via a slicer, Month to Date, Year to Date, Last Month, Last Year, End of Month, and End of Year.
Here is the code for x1, the population of a given data set:
Population_LM = -- STATIC CALC VAR LMDs = [LM_Days] -- Correctly retruns the number of days in the last month VAR DN = [DNOW] -- DNOW being the current date RETURN ROUND(CALCULATE(SUM('Population[COUNT]) , CALCULATETABLE('Population' , MONTH('BI-DATE_DIM'[DIM_DATE]) = (MONTH(DN) - 1) && YEAR('BI-DATE_DIM'[DIM_DATE]) = YEAR(DN) ) ) / LMDs, 0)
x2, the count of losses:
Loss_LM = -- STATIC CALC VAR DN = [DNOW] RETURN CALCULATE(COUNT('Losses'[ID]) , CALCULATETABLE('Losses' , (MONTH('BI-DATE_DIM'[DIM_DATE]) = MONTH(DN) - 1) && (YEAR('BI-DATE_DIM'[DIM_DATE]) = YEAR(DN)) ) )
And Ann Rate is the annualized rate of loss, based on the calendar year:
TO_LM_ANN = ([Loss_LM]/[Population_LM]) * ([EOYrDay_LM] / [LM_Days]) -- STATIC calc. Works for all other metrics but this one. -- [EOYrDay_LM] correcly returns the number of days for the year of which last month was a part.
In other words, the rate for the given period of time * the number of days in the year / the length of the period is the annualized rate. This calculation works for all other metrics, so the problem must be that the code doesn't know how to calculate stats for last month when the last month was in another year.
+Sample data for @v-yuezhe-msft
Population is a table which for simplicity's sake has counts contributing to a 'census', if you will.
As you can see, the total population for December was 50, and the population this far in January is 60.
Losses is just a table detailing events related to that population.
As you can see, there were 10 in December and 6 so far in January.
BI-DATE_DIM is simple a calendar table. It has 1:* relationships with the other tables with dates. You don't need to know why this is, but it is necesary.
Given these numbers, we would expect to get:
LM = 10 / 50 * 365 / 31 = 235%
Why not 20%? because I am required to use annualized numbers.
Solved! Go to Solution.
Ok I figured it out. It's actually pretty simple; you just explicitely tell PBI what to do in January:
Population_LM = VAR LMDs = [LM_Days] VAR DN = [DNOW]
VAR MDN = MONTH(DN)
VAR YDN = YEAR(DN) RETURN ROUND( DIVIDE( CALCULATE( SUM('Population'[COUNT]) , CALCULATETABLE('Population' , MONTH('BI-DATE_DIM'[DIM_DATE]) = IF(MDN = 1 , 12 , MDN ) && YEAR('BI-DATE_DIM'[DIM_DATE]) = IF(MONTH(DN) = 1 , YDN - 1 , YDN ) ) ) , LMDs ) , 0 )
Loss_LM = -- STATIC CALC VAR DN = [DNOW]
VAR MDN = MONTH(DN)
VAR YDN = YEAR(DN) RETURN CALCULATE( COUNT('Losses'[ID]) , CALCULATETABLE(Losses' , MONTH('BI-DATE_DIM'[DIM_DATE]) = IF(MDN = 1 , 12 , MDN ) && YEAR('BI-DATE_DIM'[DIM_DATE]) = IF(MDN = 1 , YDN - 1 , YDN ) ) )
I still think those PREVIOUS...() functions don't really work as envisioned.
Could you please share sample data of your original table and post expected result here?
You can follow the guide in the blog below to share data.
I create the following measures in your date table.
LASTCOUNT = CALCULATE(COUNT(Losses[ID]),PREVIOUSMONTH(DIM_DATE[DIM_DATE]))
LASTSUM = CALCULATE(SUM(Population[Count]),PREVIOUSMONTH(DIM_DATE[DIM_DATE]))
LM = [LASTCOUNT]/[LASTSUM]*365/31
And create the following column in the date table.
YearMonth = FORMAT(DIM_DATE[DIM_DATE],"YYYY-MM")
@v-yuezhe-msft I tried modifying my report to use these calculations, and not only do they still yield (Blank), they actually they break my existing calculations which worked using my code.
Seems like PREVIOUSMONTH(), PREVIOUSQUARTER(), and PREVIOUSYEAR() don't work either.
What is the point of YearMonth? What do you do with that? Oh, that's because it's the determining row in the matrix? Unfortunately, we're not displaying the data in that way.
The PREVIOUSMONTH(), PREVIOUSQUARTER(), and PREVIOUSYEAR() functions will return correct result with date context. If you don't want to put yearmonth column in the Matrix, you can create a slicer using the yearmonth column and select value in the slicer.
@v-yuezhe-msft I don't understand, are those functions somehow dependent on YearMonth? And when I modify my report to include PreviousMonth() and PreviousYear(), the measures return (BLANK).
Yes, these functions will return blank value if you don't use date type fields(Date, Year, YearMonth,etc) to filter them. You can include yearmonth slicer in your report.
@v-yuezhe-msft Literally all I did was swap in those functions. For example:
Population_LM = VAR LMDs = [LM_Days] --VAR DN = [DNOW] RETURN ROUND(CALCULATE(SUM('Population[COUNT]) , PREVIOUSMONTH('BI-DATE_DIM'[DIM_DATE]) /* CALCULATETABLE('BI-DATE_DIM' , MONTH('BI-DATE_DIM'[DIM_DATE]) = (MONTH(DN) - 1) && YEAR('BI-DATE_DIM'[DIM_DATE]) = YEAR(DN) ) */ ) / LMDs, 0)