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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
PoweredOut
Resolver I
Resolver I

Average of a running total

I have a Headcount Running Total Measure. Which is getting it's data from an employee history table. Meaning there could be more than one entry for an employee. This table has a relationship with the date table on the Employee Start Date.

 

 

Employee Running Total = 
VAR MaxDate = MAX('DIM Date'[Date])
VAR MinDate = MIN('DIM Date'[Date])
RETURN
CALCULATE (
    [Employee Base],
    KEEPFILTERS('Employee'[ValidFromNew] <= MaxDate &&
                'Employee'[ValidTo] >= MaxDate),
                ALL('DIM Date'))

 

  I have the requirement to find the Average of this number on speacific date. I need to have the result as below in red. Jan is divided by 1, Feb by 2 and so on. 

 

Month/YearHC RunningHC Running Avg.
Jan/2023100100
Feb/2023120110
Mar/2023115111,6


I have tried, but the result isn't what I want

 

Avg. Running Total per Month
AVERAGEX( 
       VALUES(
             'DIM DATE'[Month/Year]),
                [Employee Running Total])

 

 Any ideas?

1 ACCEPTED SOLUTION
PoweredOut
Resolver I
Resolver I

I have solved it with help from https://www.sqlbi.com/articles/rolling-12-months-average-in-dax/ @marcorusso 

i adapted it to fit my needs

PoweredOut_0-1683727066354.png

 

 

Avg. Headcount = 
VAR NumOfMonths = Max('DIM Date'[Month])
VAR LastCurrentDate =
    MAX ( 'DIM Date'[Date] )
VAR Period =
    DATESINPERIOD ( 'DIM Date'[Date], LastCurrentDate, - NumOfMonths, MONTH )
VAR Result =
    CALCULATE (
        AVERAGEX (
            VALUES ( 'DIM Date'[Month-Year] ),
            [Employee Running Total]
        ),
        Period
    )
VAR FirstDateInPeriod = MINX ( Period, 'DIM Date'[Date] )
VAR LastDate = MAX ( 'Employee'[ValidFromNew])
RETURN
    IF ( FirstDateInPeriod <= LastDate, Result )

 

.  

View solution in original post

5 REPLIES 5
PoweredOut
Resolver I
Resolver I

I have solved it with help from https://www.sqlbi.com/articles/rolling-12-months-average-in-dax/ @marcorusso 

i adapted it to fit my needs

PoweredOut_0-1683727066354.png

 

 

Avg. Headcount = 
VAR NumOfMonths = Max('DIM Date'[Month])
VAR LastCurrentDate =
    MAX ( 'DIM Date'[Date] )
VAR Period =
    DATESINPERIOD ( 'DIM Date'[Date], LastCurrentDate, - NumOfMonths, MONTH )
VAR Result =
    CALCULATE (
        AVERAGEX (
            VALUES ( 'DIM Date'[Month-Year] ),
            [Employee Running Total]
        ),
        Period
    )
VAR FirstDateInPeriod = MINX ( Period, 'DIM Date'[Date] )
VAR LastDate = MAX ( 'Employee'[ValidFromNew])
RETURN
    IF ( FirstDateInPeriod <= LastDate, Result )

 

.  

Dhairya
Super User
Super User

 'YourTable'[DateColumn] & 'YourTable'[MonthColumn] implies Your Date dimension table.

 'YourTable'[ValueColumn] implies your Fact Table from where numeric values you want to calculate.

Dhairya
Super User
Super User

Average Of Running Total =
DIVIDE (
    SUMX (
        ADDCOLUMNS (
            VALUES ( 'YourTable'[DateColumn] ),
            "RunningTotal",
            CALCULATE (
            SUM ( 'YourTable'[ValueColumn] ),
            FILTER (
                ALLSELECTED ( 'YourTable'[DateColumn] ),
                'YourTable'[DateColumn] <= EARLIER ( 'YourTable'[DateColumn] )
       )
    )
),
[RunningTotal]
),
COUNTROWS ( VALUES ( 'YourTable'[MonthColumn] ) )
)



Please try this code.

Unfortunaetly it doesn't work. Context is Year = 2023. I want to be able to Show in March the average of Jan, Feb & Mar. 

 

PoweredOut_0-1683635199875.png

Measure I used.

Headcount  Avg. = 
DIVIDE (
    SUMX (
        ADDCOLUMNS (
            VALUES ( 'DIM Date'[Date]),
            "RunningTotal",
            CALCULATE (
            DISTINCTCOUNT('Employee'[EmployeeID]),
            FILTER (
                ALLSELECTED ( 'DIM Date' ),
                'DIM Date'[Date] <= EARLIER ('DIM Date'[Date])
       )
    )
),
[RunningTotal]
),
COUNTROWS ( VALUES ('DIM Date'[Period]  ) )
)

 

Thanks for replying

 

I have tried replacing "YourTable", but I am not getting the results I want. 

 

When you mention Your Table, do you mean DIM Date or Employee?

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

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

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

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