Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Rolling total last 12 months grand total

Hello All,

 

Need some help please. I have the below sample data with 5 columns - Year, Month, Department, Employee, Leavers. I need to calculate the % turnover (Leavers/Employees) rolling previous 12 month from the month selected in a slicer. So, if May 2019 is chosen then I need to get the % turnover from June 2018 all the way to May 2019 as a total. The problem is that employee cannot summed up by month because the data is a monthly snapshot.

 

On the report I need to have Year, Month and Department as slicers.

 

This is what I have come up with so far but the grand total isn't correct if this measure is used in a table

Rolling 12 month turnover =
VAR _StartDate =
    DATE ( YEAR ( MAX ( 'dDate'[Date] ) ) - 1, MONTH ( MAX ( 'dDate'[Date] ) ), DAY ( MAX ( 'dDate'[Date] ) ) )
VAR _EndDate =
    MAX ( 'dDate'[Date] )
VAR DateFilter =
    FILTER ( ALL ( dDate ), dDate[Date] >= _StartDate && dDate[Date] <= _EndDate )
RETURN
    CALCULATE (
        DIVIDE (
            SUM ( 'fact'[Leavers] ),
            AVERAGEX (
                'fact',
                SUM ( 'Employee] )
            )
        ),
        DateFilter
    )

 

 This is what I'm getting. the grand total should be 11.16% but it is .93%. 

 

Rolling 12 months.PNG

1 ACCEPTED SOLUTION

Hi again.

I think you may be overthinking this problem.

Try creating a measure for the Turnover by utilizing the SUMMARIZE function, and then create a rolling 12 months measure based on this Turnover-measure.

Create a Turnover-measure like this:

Turnover = SUMX(
SUMMARIZE('fact', //Summarizes the fact table
dDate[year-month], //Grouping on the [year-month] column
"Turnover", //Naming and creating the turnover-measure caluclation for each row (group)
DIVIDE(
SUM('fact'[Leavers]),
SUM('fact'[Employee])
)
),
[Turnover]) //Sums up the values given the evaluation context.

This would result in a measure evaluating to the correct Turnover for each year-month, and the correct total turnover for the Grand Total row.

Now you could use time intelligence functions like TOTALYTD, TOTALQTD, to craete new measures based on your turnover-measure. And you could create a measure to calculate the Rolling12M Turnover like this:

Rolling12M Turnover = CALCULATE (
    [Turnover],
    DATESBETWEEN (
        dDate[Date],
        NEXTDAY(SAMEPERIODLASTYEAR(LASTDATE(dDate[Date]))),
        LASTDATE(dDate[Date])
    )
)   

When you create DAX measures it is often usefull to break down your process in several smaller steps, and then build more on top of them.

View solution in original post

6 REPLIES 6
Anonymous
Not applicable

Anyone who can shaed some light on this please? @Zubair_Muhammad @Greg_Deckler @parry2k 

Hi again.

I think you may be overthinking this problem.

Try creating a measure for the Turnover by utilizing the SUMMARIZE function, and then create a rolling 12 months measure based on this Turnover-measure.

Create a Turnover-measure like this:

Turnover = SUMX(
SUMMARIZE('fact', //Summarizes the fact table
dDate[year-month], //Grouping on the [year-month] column
"Turnover", //Naming and creating the turnover-measure caluclation for each row (group)
DIVIDE(
SUM('fact'[Leavers]),
SUM('fact'[Employee])
)
),
[Turnover]) //Sums up the values given the evaluation context.

This would result in a measure evaluating to the correct Turnover for each year-month, and the correct total turnover for the Grand Total row.

Now you could use time intelligence functions like TOTALYTD, TOTALQTD, to craete new measures based on your turnover-measure. And you could create a measure to calculate the Rolling12M Turnover like this:

Rolling12M Turnover = CALCULATE (
    [Turnover],
    DATESBETWEEN (
        dDate[Date],
        NEXTDAY(SAMEPERIODLASTYEAR(LASTDATE(dDate[Date]))),
        LASTDATE(dDate[Date])
    )
)   

When you create DAX measures it is often usefull to break down your process in several smaller steps, and then build more on top of them.

Anonymous
Not applicable

This solution works for me, thank you very much for your help. I was indeed overthinking it!

Toerstad
Advocate I
Advocate I

Hi.

You can solve this by utilizing the function HASONEFILTER. This can be used to change the formula used for a measure given the evaluation context.

Put the name of your yaer-month column in the HASONEFILTER function.

Rolling 12 month turnover =
IF(HASONEFILTER('dDate'[year-month]), // <-- put your year-month column here. 

// if HASONEFILTER is true it will use the following 1 month window
VAR _StartDate = DATE ( YEAR ( MAX ( 'dDate'[Date] ) ) - 1, MONTH ( MAX ( 'dDate'[Date] ) ), DAY ( MAX ( 'dDate'[Date] ) ) ) VAR _EndDate = MAX ( 'dDate'[Date] ) VAR DateFilter = FILTER ( ALL ( dDate ), dDate[Date] >= _StartDate && dDate[Date] <= _EndDate ) RETURN CALCULATE ( DIVIDE ( SUM ( 'fact'[Leavers] ), AVERAGEX ( 'fact', SUM ( 'Employee] ) ) ), DateFilter ),

// if HASONEFILTER is false (as it woult be for the Total) it will use the following 12 month window VAR _StartDate = DATE ( YEAR ( MAX ( 'dDate'[Date] ) ) - 12, MONTH ( MAX ( 'dDate'[Date] ) ), DAY ( MAX ( 'dDate'[Date] ) ) ) VAR _EndDate = MAX ( 'dDate'[Date] ) VAR DateFilter = FILTER ( ALL ( dDate ), dDate[Date] >= _StartDate && dDate[Date] <= _EndDate ) RETURN CALCULATE ( DIVIDE ( SUM ( 'fact'[Leavers] ), AVERAGEX ( 'fact', SUM ( 'Employee] ) ) ), DateFilter ) )

You can probably find a more efficient way of wrinting the measure utilizing the HASONEFILTER function yourself. You can find the reference for the function here: https://docs.microsoft.com/en-us/dax/hasonefilter-function-dax 

 

Anonymous
Not applicable

Hi Toerstad,

 

Thank you but this does not seem to work as expected. 

Rolling 12 months.PNG

Sample data 

20193A37    
20193B187    
20193C391   
20193D661   
20193E10    
20193F10    
20193G5    
20193H10    
20193I37    
20193J9    
20193K42    
20193L4    
20193M14    
20193N41   
20193AN3    
20193O13    
20193P8    
20193Q92   
20193R1    
20193S10    
20193T7    
20193U9    
20193V17    
20193W48    
20193Y2    
20193Z19    
20193AA21    
20193AB40    
20193AC9    
20193AO2    
20193AD29    
20193AE13    
20193AF31    
20193AG1    
20193AH7    
20193AI6    
20193AJ50    
20193AK32    
20193AL6    
20193AM3 87050.57%
20194A37    
20194B1882   
20194C38    
20194D66    
20194E9    
20194F10    
20194G5    
20194H10    
20194I37    
20194J9    
20194K42    
20194L5    
20194M14    
20194N4    
20194AN2    
20194O121   
20194P81   
20194Q71   
20194R1    
20194S10    
20194T7    
20194U9    
20194V17    
20194W481   
20194Y3    
20194Z18    
20194AA21    
20194AB39    
20194AC9    
20194AO2    
20194AD292   
20194AE13    
20194AF311   
20194AG1    
20194AH7    
20194AI6    
20194AJ50    
20194AK32    
20194AL6    
20194AM3 86591.04%
20195A37    
20195B1932   
20195C382   
20195D66    
20195E9    
20195F10    
20195G5    
20195H10    
20195I37    
20195J9    
20195K41    
20195L4    
20195M14    
20195N4    
20195AN2    
20195O10    
20195P7    
20195Q6    
20195R1    
20195S9    
20195T7    
20195U9    
20195V171   
20195W472   
20195Y3    
20195Z181   
20195AA22    
20195AB39    
20195AC9    
20195AO2    
20195AD27    
20195AE111   
20195AF301   
20195AG1    
20195AH7    
20195AI6    
20195AJ501   
20195AK33    
20195AL5    
20195AM3 858111.28%

Hi.

Since you have a snapshot table you could rewrite the entire last part, where HASONEFILTER = False.
A calculation dividing the sum of all the "leavers" for the last 12 months and utilizing the LASTNONBLANK function to get the values for current employees should work. 

See documentation on the LASTNONBLANK function here: https://docs.microsoft.com/en-us/dax/lastnonblank-function-dax

And common patterns and scenarios for semi-additive measures here: https://www.sqlbi.com/articles/semi-additive-measures-in-dax-for-power-pivot/

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.

Top Solution Authors