cancel
Showing results for
Did you mean:

The ultimate Microsoft Fabric, Power BI, Azure AI & SQL learning event! Join us in Las Vegas from March 26-28, 2024. Use code MSCUST for a \$100 discount. Register Now

Anonymous
Not applicable

## Calculate Average with Filter in DAX not working

I have a table with a Working Days column, which is integer type, and a Latest Action Date column with date (01/01/2020). I want a DAX measure to calculate the average but only for 2020 dates.

 Latest Submission Date Latest Action Date Working Days 4/1/2020 4/4/2020 4 4/3/2020 4/5/2020 2 1/4/2021 1/5/2021 1

I tried the following measures I wrote:

2020 Avg SLA =
CALCULATE ( AVERAGE ( 'Pivoted Audit Log'[Working Days] ), 'Pivoted Audit Log'[Latest TM Action Date] < 01/01/2021 )

2020 Avg SLA Fixed = CALCULATE(
AVERAGE('Pivoted Audit Log'[Working Days]),
FILTER('Pivoted Audit Log', 'Pivoted Audit Log'[Latest TM Action Date] < 01/01/2021))

Got this error though both times:

Error Message:
MdxScript(Model) (35, 60) Calculation error in measure 'Pivoted Audit Log'[2020 Avg SLA Fixed]: DAX comparison operations do not support comparing values of type Text with values of type Number. Consider using the VALUE or FORMAT function to convert one of the values.

My objective:

A measure that calculates the average in a column, but only for year 2020, and then, another measure that calculates the average in a column but only for year 2021

1 ACCEPTED SOLUTION
Super User

@Anonymous , you can create a new column like

Work Day =
Var _end = if([Latest Action Date] > Date(2020,12,31),Date(2020,12,31), [Latest Action Date])
return
COUNTROWS(FILTER(ADDCOLUMNS(CALENDAR(Table[Latest Submission Date])_end),"WorkDay", if(WEEKDAY([Date],2) <6,1,0)),[WorkDay] =1))

or measure with some group by row id

Work Day =
Var _end = if(max([Latest Action Date]) > Date(2020,12,31),Date(2020,12,31), max[Latest Action Date]))
return
AverageX(Values(Table[RowID]), COUNTROWS(FILTER(ADDCOLUMNS(CALENDAR(max(Table[Latest Submission Date]),_end),"WorkDay", if(WEEKDAY([Date],2) <6,1,0)),[WorkDay] =1)))

Super User

@Anonymous , you can create a new column like

Work Day =
Var _end = if([Latest Action Date] > Date(2020,12,31),Date(2020,12,31), [Latest Action Date])
return
COUNTROWS(FILTER(ADDCOLUMNS(CALENDAR(Table[Latest Submission Date])_end),"WorkDay", if(WEEKDAY([Date],2) <6,1,0)),[WorkDay] =1))

or measure with some group by row id

Work Day =
Var _end = if(max([Latest Action Date]) > Date(2020,12,31),Date(2020,12,31), max[Latest Action Date]))
return
AverageX(Values(Table[RowID]), COUNTROWS(FILTER(ADDCOLUMNS(CALENDAR(max(Table[Latest Submission Date]),_end),"WorkDay", if(WEEKDAY([Date],2) <6,1,0)),[WorkDay] =1)))

Announcements

#### Microsoft Fabric Community Conference

Join us at our first-ever Microsoft Fabric Community Conference, March 26-28, 2024 in Las Vegas with 100+ sessions by community experts and Microsoft engineering.

#### Power BI Monthly Update - February 2024

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

#### Microsoft Fabric Career Hub

Explore career paths and learn resources in Fabric.

#### Microsoft Fabric Partner Community

Engage with the Fabric engineering team, hear of product updates, business opportunities, and resources in the Fabric Partner Community.

Top Solution Authors
Top Kudoed Authors