Forum Discussion
DAX coding
Hi All,
I have been tryiong to develop a measure that will calculate the avgFTE over a selected period of time, but have not been able to get it to calculate correctly. The code I have works for months Feb-Dec and Q2-Q4, but does not work for Jan, Q1 or Full year averages where the calculation has to span to the prior year. The logic for calculating avg FTE for a specific month is the average of the ending FTE for that month and the prior month. For the qtr it is the average of the ending FTEs for the 3 months of the selected qtr and the last month of the prior qtr. And for full year it is the average of the ending FTE for each month of the selected year and Dec of the prior year.
Below is the code I have written as well as a snippet from my dashboard. Can anyone help in fixing this code or writing a new code that would work for this calculation?
Avg_FTE =
Hello !
Thank you for posting on Microsoft Fabric community.
You don’t need all those year specific hacks.
You can make one measure that always pulls the last N month-ends (2 for Month, 4 for Quarter, 13 for Year) which naturally crosses into the prior year for Jan/Q1/Full Year.
-- FTE at month-end for the current filter context FTE EOM = VAR EOMD = EOMONTH ( MAX ( Periods[Period (dt)] ), 0 ) RETURN CALCULATE ( [FTEs], KEEPFILTERS ( Periods[Period (dt)] = EOMD ) ) -- Main: Avg FTE for Month / Quarter / Year (includes prior month/quarter-end/Dec PY) Avg FTE = VAR IsMonth = ISINSCOPE ( Periods[Month Name] ) VAR IsQuarter = ISINSCOPE ( Periods[Qtr] ) VAR IsYear = ISINSCOPE ( Periods[Year] ) && NOT IsQuarter && NOT IsMonth -- Use the last visible month-end in the current context as the anchor VAR AnchorEOM = EOMONTH ( MAX ( Periods[Period (dt)] ), 0 ) -- How many months back (besides the anchor month) do we include? -- Month: 1 prior (=> 2 months total) -- Quarter: 3 prior (=> 4 months total; adds last month of previous quarter) -- Year: 12 prior (=> 13 months total; adds Dec of prior year) VAR MonthsBack = SWITCH ( TRUE (), IsMonth, 1, IsQuarter, 3, IsYear, 12, /* default */ 1 ) -- Build the list of month-end dates to average over VAR MonthEndsTableRaw = SELECTCOLUMNS ( DATESINPERIOD ( Periods[Period (dt)], AnchorEOM, -MonthsBack, MONTH ), "EOM", EOMONTH ( Periods[Period (dt)], 0 ) ) VAR MonthEnds = DISTINCT ( MonthEndsTableRaw ) -- one row per month-end -- Average the FTE month-ends across the window RETURN AVERAGEX ( MonthEnds, VAR d = [EOM] RETURN CALCULATE ( [FTEs], KEEPFILTERS ( Periods[Period (dt)] = d ) ) )If you must force prior-year month-ends to actuals only you can wrap the inner CALCULATE with a conditional filter.
RETURN AVERAGEX ( MonthEnds, VAR d = [EOM] VAR anchorYear = YEAR ( AnchorEOM ) VAR y = YEAR ( d ) RETURN IF ( y < anchorYear, CALCULATE ( [FTEs], KEEPFILTERS ( Periods[Period (dt)] = d ), KEEPFILTERS ( Master_Data[Submission] IN { "PY_ACTUAL" } || Master_Data[SCENARIO] IN { "ACTUAL" } ) ), CALCULATE ( [FTEs], KEEPFILTERS ( Periods[Period (dt)] = d ) ) ) )
6 Replies
- AmiraBedhSuper User
Hello !
Thank you for posting on Microsoft Fabric community.
You don’t need all those year specific hacks.
You can make one measure that always pulls the last N month-ends (2 for Month, 4 for Quarter, 13 for Year) which naturally crosses into the prior year for Jan/Q1/Full Year.
-- FTE at month-end for the current filter context FTE EOM = VAR EOMD = EOMONTH ( MAX ( Periods[Period (dt)] ), 0 ) RETURN CALCULATE ( [FTEs], KEEPFILTERS ( Periods[Period (dt)] = EOMD ) ) -- Main: Avg FTE for Month / Quarter / Year (includes prior month/quarter-end/Dec PY) Avg FTE = VAR IsMonth = ISINSCOPE ( Periods[Month Name] ) VAR IsQuarter = ISINSCOPE ( Periods[Qtr] ) VAR IsYear = ISINSCOPE ( Periods[Year] ) && NOT IsQuarter && NOT IsMonth -- Use the last visible month-end in the current context as the anchor VAR AnchorEOM = EOMONTH ( MAX ( Periods[Period (dt)] ), 0 ) -- How many months back (besides the anchor month) do we include? -- Month: 1 prior (=> 2 months total) -- Quarter: 3 prior (=> 4 months total; adds last month of previous quarter) -- Year: 12 prior (=> 13 months total; adds Dec of prior year) VAR MonthsBack = SWITCH ( TRUE (), IsMonth, 1, IsQuarter, 3, IsYear, 12, /* default */ 1 ) -- Build the list of month-end dates to average over VAR MonthEndsTableRaw = SELECTCOLUMNS ( DATESINPERIOD ( Periods[Period (dt)], AnchorEOM, -MonthsBack, MONTH ), "EOM", EOMONTH ( Periods[Period (dt)], 0 ) ) VAR MonthEnds = DISTINCT ( MonthEndsTableRaw ) -- one row per month-end -- Average the FTE month-ends across the window RETURN AVERAGEX ( MonthEnds, VAR d = [EOM] RETURN CALCULATE ( [FTEs], KEEPFILTERS ( Periods[Period (dt)] = d ) ) )If you must force prior-year month-ends to actuals only you can wrap the inner CALCULATE with a conditional filter.
RETURN AVERAGEX ( MonthEnds, VAR d = [EOM] VAR anchorYear = YEAR ( AnchorEOM ) VAR y = YEAR ( d ) RETURN IF ( y < anchorYear, CALCULATE ( [FTEs], KEEPFILTERS ( Periods[Period (dt)] = d ), KEEPFILTERS ( Master_Data[Submission] IN { "PY_ACTUAL" } || Master_Data[SCENARIO] IN { "ACTUAL" } ) ), CALCULATE ( [FTEs], KEEPFILTERS ( Periods[Period (dt)] = d ) ) ) ) - Shahid12523Community Champion
Use this
Avg_FTE =
VAR sDate = MAX(Periods[Period (dt)])
VAR sMonth = SELECTEDVALUE(Periods[Month Name])
VAR sQtr = SELECTEDVALUE(Periods[Qtr])
VAR sYear = SELECTEDVALUE(Periods[Year])
VAR EndOfMonth = EOMONTH(sDate, 0)-- Prior Month
VAR PriorMonthDate = EOMONTH(EndOfMonth, -1)
VAR PriorMonthFTE =
CALCULATE(
[FTEs],
Periods[Period (dt)] = PriorMonthDate
)-- Current Month
VAR CurrentMonthFTE =
CALCULATE(
[FTEs],
Periods[Period (dt)] = EndOfMonth
)-- Quarter Dates
VAR QuarterDates =
DATESINPERIOD(Periods[Period (dt)], EndOfMonth, -3, MONTH)
VAR PriorQtrMonthDate = EOMONTH(EndOfMonth, -3)
VAR QtrFTE =
CALCULATE(
[FTEs],
Periods[Period (dt)] IN QuarterDates
)
VAR PriorQtrFTE =
CALCULATE(
[FTEs],
Periods[Period (dt)] = PriorQtrMonthDate
)-- Year Dates
VAR YearDates =
DATESINPERIOD(Periods[Period (dt)], EndOfMonth, -12, MONTH)
VAR PriorYearDecDate = DATE(sYear - 1, 12, 31)
VAR YearFTE =
CALCULATE(
[FTEs],
Periods[Period (dt)] IN YearDates
)
VAR PriorYearDecFTE =
CALCULATE(
[FTEs],
Periods[Period (dt)] = PriorYearDecDate
)RETURN
SWITCH(
TRUE(),
-- Full Year
ISBLANK(sMonth) && ISBLANK(sQtr),
DIVIDE(YearFTE + PriorYearDecFTE, 13),-- Q1
ISBLANK(sMonth) && sQtr = "Q1",
DIVIDE(QtrFTE + PriorQtrFTE, 4),-- Other Quarters
ISBLANK(sMonth),
DIVIDE(QtrFTE + PriorQtrFTE, 4),-- January
sMonth = "Jan",
DIVIDE(CurrentMonthFTE + PriorMonthFTE, 2),-- Other Months
DIVIDE(CurrentMonthFTE + PriorMonthFTE, 2)
) - Ashish_MathurSuper User
Hi,
Share the download link of the PBI file.
- v-hashadapuCommunity Support
Hi psstack , hope you are doing great. May we know if your issue is solved or if you are still experiencing difficulties. Please share the details as it will help the community, especially others with similar issues.
- v-hashadapuCommunity Support
Hi psstack , Hope you're doing okay! May we know if it worked for you, or are you still experiencing difficulties? Let us know — your feedback can really help others in the same situation.
- v-hashadapuCommunity Support