Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
440 views
in Technique[技术] by (71.8m points)

powerbi - Calculating Monthly Average over time with only Start and End Dates in Power BI

In order to track our average headcounts and turnover trends, I had created a report in Power BI that essentially expanded a table with employee start/end information to give a record for each associate every day they were employed at the company.

While this solution works, I end up with a table containing millions of rows which does not seem very efficient.

I have looked into other possible solutions in DAX, but have not found anything that will give me the exact same output. It seems like most solutions rely on taking the timing from the start date and end date of a month and then averaging them, which is not quite what I am trying to do.

I have attached a simplified pbix file that shows the desired output of what I am trying to get at. Any advice would be appreciated.

pbix example

question from:https://stackoverflow.com/questions/65648703/calculating-monthly-average-over-time-with-only-start-and-end-dates-in-power-bi

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Final code ended up looking like this:

All Employees = countrows ('Employees')

Terminations = 
Calculate(
    Countrows('Employees'),
    USERELATIONSHIP('Employees'[End Date],'Calendar'[Date])
)


Active Employees = 
AVERAGEX(
    VALUES('Calendar'[Date]),
    VAR CurrentDate = 'Calendar'[Date]
    VAR HiredBeforeCurrentDate =
        Filter(
            ALL('Employees'[Start Date]),
            'Employees'[Start Date] <= CurrentDate
        )
    VAR HiredAfterCurrentDate = 
        FILTER(
            ALL('Employees'[End Date]),
            OR('Employees'[End Date] >= CurrentDate, isblank('Employees'[End Date])
            )
        )
    RETURN
        CALCULATE(
            Countrows('Employees'),
            HiredBeforeCurrentDate,
            HiredAfterCurrentDate,
            ALL('Calendar')
        ))

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...