Forum Discussion

jdevries192's avatar
jdevries192
Frequent Visitor
3 years ago

AddColumns with Summarize Filter Context

Hi Experts,

 

Need some help. .PBIX and data files attached.

 

I need to calculate hires from 3 months ago & terminations from that group of hires. But my calculation is not returning what I expect. I'm believe I'm missing something in regards to Addcolumns(Summarize and its interaction with outside filter context.

 

The TOTAL for the calculation appears correct. But month by month is not.

 

For April, I expect the following:

  • 5,472 hires (from January)
  • 3,129 of those hires terminated by the end of April

(5,472 - 3,129) / 5472 = 42.8%

 

Data File

Date Table

.PBIX File

 

 

 

Calculation 90 Days = 
//calcs based on x days
VAR __Days = 90
VAR __Periods =
    ROUND ( DIVIDE ( __Days, 30 ), 0 ) //Look back to period x days ago for x day hires
VAR __Max_Date =
    CALCULATE ( MAX ( Data[Record Date] ) )
VAR __Lookback_Month =
    CALCULATE (
        MAX ( Date_Table[Month_Num] ),
        Date_Table[Date] <= __Max_Date,
        Date_Table[Month_Num]
            = MAX ( Date_Table[Month_Num] ) - __Periods
    ) //Look back x periods from most recent date to determine Date Range
VAR __MIN_DATE =
    CALCULATE (
        MIN ( Date_Table[Date] ),
        ALL ( Date_Table ),
        Date_Table[Month_Num] = __Lookback_Month
    ) //Build tables for all Hires and Terms in timeframe
//Find hires up to x days from max date
VAR __Hire_EMPIDs =
    ADDCOLUMNS (
        SUMMARIZE ( Data, Data[Employee ID] ),
        "Hire",
            CALCULATE (
                COUNTROWS ( Data ),
                Data[Proccess] = "Hire",
                ALL ( Date_Table ),
                Date_Table[Month_Num] = __Lookback_Month
            )
    ) //Find all terms for timeframe
VAR __Term_EMPIDs =
    ADDCOLUMNS (
        SUMMARIZE ( Data, Data[Employee ID] ),
        "Term",
            CALCULATE (
                COUNTROWS ( Data ),
                Data[Proccess] = "Termination",
                ALL ( Date_Table ),
                Data[Record Date] >= __MIN_DATE
                    && Data[Record Date] <= __Max_Date
            )
    ) //Combine hire and term tables to get term count for hires in time period
VAR __Join =
    NATURALLEFTOUTERJOIN ( __Hire_EMPIDs, __Term_EMPIDs ) //will not count duplicate EMPIDs
VAR __Count_Hires =
    COUNTX ( FILTER ( __Hire_EMPIDs, [Hire] > 0 ), [Employee ID] )
VAR __Count_Terms =
    COUNTX ( FILTER ( __Join, [Hire] > 0 && [Term] > 0 ), [Employee ID] )
VAR __HC_Continuous_End = __Count_Hires - __Count_Terms
VAR __RESULT =
    DIVIDE ( __HC_Continuous_End, __Count_Hires )
RETURN
    __RESULT

 

 

No RepliesBe the first to reply