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

Calling all Data Engineers! Fabric Data Engineer (Exam DP-700) live sessions are back! Starting October 16th. Sign up.

Reply
cwollett
Advocate II
Advocate II

DAX - How to change CALCULATE to a better performing calculation?

My understanding is that, while powerful, CALCULATE can perform poorly compared to other options. I am struggling to understand how to transform one of my measures to not use CALCULATE, but still return the same result.

 

Background Info

I am making a series of reports that showcase course evaluation survey data. I have a data model that looks something like the image below. In words:

  • Dimension tables for Courses, Responses (text and normalized values), Terms, Students, Instructors, and Questions (question text as well as whether the question is for the course or the instructor).
  • We have a factless fact table that just acts as the hub for all these tables to join to

cwollett_1-1739381825095.png

Performance Issue

All calculations are done as measures. Two calculations in particular are:

  • Department Course Average (performance is slow - looking to improve)

 

Department Course Average = 

var _selectedDepartment = SELECTEDVALUE('Dim Courses'[Department])
var _selectedTerm = SELECTEDVALUE('Dim Terms'[TERM_ID])

var _table = 
SUMMARIZE(
    ALLEXCEPT('Fact', 'Fact'[TERM_ID]),
    'DimTerms'[TERM_ID],
    'DimCourses'[Department],
    "DEPT_AVG",
    CALCULATE(
        [Course Average],
        'DimCourses'[Department] = _selectedDepartment,
        'Fact'[TERM_ID] = _selectedTerm
    )
)

RETURN
AVERAGEX(_table,[DEPT_MEAN])​

 

  • Course Average (performance is fine - included as the measure above uses this one)

 

Course Average =

var _responses = 
CALCULATETABLE(
    SUMMARIZE(
        ADDCOLUMNS(
            'Fact',
            "RESPONSE_VALUE", RELATED('DimResponses'[RESPONSE_VALUE])
        ),
        'Fact'[COURSE_ID],
        'Fact'[QUESTION_ID],
        'Fact'[STUDENT_ID],
        [RESPONSE_VALUE]
    ),
    'DimQuestions'[QUESTION_TYPE] = "COURSE"
)
RETURN 
AVERAGEX(_responses, [RESPONSE_VALUE])​

 

Adding Department Course Average to visuals causes the spinning wheel in the corner and it takes awhile to load, though it is correct when it does finally load. Is there a more efficient way to handle this calculation?

1 ACCEPTED SOLUTION
AlexisOlson
Super User
Super User

My understanding is that, while powerful, CALCULATE can perform poorly compared to other options.

 

This is a pretty misleading oversimplification but it sounds like @GregDeckler's propaganda is working.

 

I don't think your issue is CALCULATE or no CALCULATE but rather than your [Department Course Average] measure is a mess that could probably be rewritten much more simply. Maybe like this?

Department Course Average =
CALCULATE (
    [Course Average],
    REMOVEFILTERS (),
    VALUES ( 'DimCourses'[Department] ),
    VALUES ( 'Fact'[TERM_ID] )
)

View solution in original post

4 REPLIES 4
cwollett
Advocate II
Advocate II

Apologies for not sharing actual data. I personally find the way tables look in the posts hard to discern and can make posts quite lengthy. But, I didn't think about folks using copy/paste for quick creation in Power BI itself. That's good feedback.

I've made a PBIX file (dropbox download link) with a very small set of data to give an example of how we're joining data and the solution that we've tried as well as the measure @AlexisOlson suggested. Disclaimer - this is miniscule compared to our data. Our fact table is more like 1 million rows per term, so we'll likely be doing calculations on 15-20 million rows soon.

 

@AlexisOlson's measure (DEPT_INSTR_MEAN_2) returns the correct values when I do some filtered tests, but over the whole dataset, we get an error about exceeding available resources. We have added a survey stats dimension, where we can roll up some values (we do not want to roll up all responses, because we want to be able to let users filter some things based on student demographic groups like year and major). That should help alleviate the resources needed in the calculation, but I am not sure how to fix DEPT_INSTR_MEAN_1 to make it only take into consideration Department & TERM_ID.

Anonymous
Not applicable

Hi, @cwollett 

The SUMMARIZE function combined with CALCULATE inside it can be computationally expensive, especially when iterating over large datasets.
The Department Course Average measure is recalculating the Course Average for every row in the summarized table, which is inefficient.

We can rewrite the Department Course Average measure to avoid CALCULATE and SUMMARIZE where possible. Instead, we’ll use FILTER and AVERAGEX to achieve the same result more efficiently.

Department Course Average (Optimized) = 
VAR _selectedDepartment = SELECTEDVALUE('Dim Courses'[Department])
VAR _selectedTerm = SELECTEDVALUE('Dim Terms'[TERM_ID])
VAR _filteredData = 
    FILTER(
        ADDCOLUMNS(
            'Fact',
            "RESPONSE_VALUE", RELATED('Dim Responses'[RESPONSE_VALUE]),
            "DEPARTMENT", RELATED('Dim Courses'[Department]),
            "QUESTION_TYPE",RELATED('Dim Questions'[QUESTION_TYPE])
        ),
       [QUESTION_TYPE] = "COURSE" &&
        [DEPARTMENT] = _selectedDepartment &&
        'Fact'[TERM_ID] = _selectedTerm
    )
RETURN 
AVERAGEX(_filteredData, [RESPONSE_VALUE])

vjianpengmsft_0-1740031148422.png

 

 

Best Regards

Jianpeng Li

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

AlexisOlson
Super User
Super User

My understanding is that, while powerful, CALCULATE can perform poorly compared to other options.

 

This is a pretty misleading oversimplification but it sounds like @GregDeckler's propaganda is working.

 

I don't think your issue is CALCULATE or no CALCULATE but rather than your [Department Course Average] measure is a mess that could probably be rewritten much more simply. Maybe like this?

Department Course Average =
CALCULATE (
    [Course Average],
    REMOVEFILTERS (),
    VALUES ( 'DimCourses'[Department] ),
    VALUES ( 'Fact'[TERM_ID] )
)
speedramps
Super User
Super User

Please dont provide your slow buggy DAX and expect us to fathom what you want.

Please do just give a simple non technical functional description of what you want, then let us suggest the solution. Thank you.

Provide example input data as table text (not a screen print) so we can import the data to build a solution for you.

Remove any unneeded columns which may cause confusion.

Rename columns to user friendly names. Avoid jargon.
Also provide the example desired output, with a clear step-by-step description of calculations the process flow.
Remember not to share private data ... we don't want you to get into trouble. 😧
Take time and care to use the same table and field names in the input, output and description so we can understand your problem and help you.
Try keep it simple and ask one question per ticket.
You will get a quicker and better response if you put time, care and effort into writing clear problem descriptions.

Rather than expect the helper to unpick your DAX 

Thanks

Helpful resources

Announcements
FabCon Global Hackathon Carousel

FabCon Global Hackathon

Join the Fabric FabCon Global Hackathon—running virtually through Nov 3. Open to all skill levels. $10,000 in prizes!

September Power BI Update Carousel

Power BI Monthly Update - September 2025

Check out the September 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors
Top Kudoed Authors