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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
rpiboy_1
Helper V
Helper V

Is this bad DAX, even though it works

I know that I always read about how tricky calculate can be and avoiding 'nested' calculates, so I'm looking for feedback. I have a measure that works, but there are nested Calculates, so I want to know if there is a better way.

Model set-up
table: Dates (well formed via Bravo)
table: Projects

Projects has a column 'Created Date' which is related to 'Date'[Date] via an inactive relationship.

I'm returning the average number of projects created per month for the current year, so for 2025 that would be 'Count Projects' / 'Count Months' (2) (January & February).

Here is the measure that works, but importantly you'll note that I'm nesting another measure [Projects Created] which itself is wrapped in a calculate. If I try to remove the calculate that wraps _result I get an OLE DB / ODBC error which cites [Projects Created] so I assume it has to do with the in-active relationship. If I try to direclty write the contents of the [Projects Created] measure into this measure it also fails.

Main Measure:

Avg. Proj Created by Mo current YR = 

VAR _currentYear = YEAR(TODAY())
VAR _Months =
    CALCULATETABLE(
        VALUES('Date'[Year Month]),
        FILTER('Date', YEAR('Date'[Date]) = _currentYear)
    )

VAR _result =
    CALCULATE(
        AVERAGEX(
            _Months,
            [Projects Created]
        ),
        USERELATIONSHIP('Date'[Date], 'Projects'[Created Date])
    )

RETURN
    _result


Nested Measure [Projects Created]:

Projects Created = 
CALCULATE(
    COUNTROWS('Projects'),
    USERELATIONSHIP('Date'[Date], 'Projects'[Created Date])
)

 

1 ACCEPTED SOLUTION
Anonymous
Not applicable

Hi, @rpiboy_1 

To avoid nested CALCULATE and simplify the measure, we can:
Use a single CALCULATE with USERELATIONSHIP to activate the inactive relationship.
Combine the logic of Avg. Proj Created by Mo current YR and Projects Created into a single measure.

Proj Created by Mo current YR (Optimized) = 
VAR _currentYear = YEAR(TODAY())
VAR _Months =
    CALCULATETABLE(
        VALUES('Date'[Year Month]),
        FILTER('Date', YEAR('Date'[Date]) = _currentYear)
    )
RETURN
    CALCULATE(
        AVERAGEX(
            _Months,
            COUNTROWS('Projects')
        ),
        USERELATIONSHIP('Date'[Date], 'Projects'[Created Date])
    )

vjianpengmsft_0-1740033633570.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.

 

 

 

 

 

View solution in original post

3 REPLIES 3
Anonymous
Not applicable

Hi, @rpiboy_1 

To avoid nested CALCULATE and simplify the measure, we can:
Use a single CALCULATE with USERELATIONSHIP to activate the inactive relationship.
Combine the logic of Avg. Proj Created by Mo current YR and Projects Created into a single measure.

Proj Created by Mo current YR (Optimized) = 
VAR _currentYear = YEAR(TODAY())
VAR _Months =
    CALCULATETABLE(
        VALUES('Date'[Year Month]),
        FILTER('Date', YEAR('Date'[Date]) = _currentYear)
    )
RETURN
    CALCULATE(
        AVERAGEX(
            _Months,
            COUNTROWS('Projects')
        ),
        USERELATIONSHIP('Date'[Date], 'Projects'[Created Date])
    )

vjianpengmsft_0-1740033633570.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.

 

 

 

 

 

sjoerdvn
Super User
Super User

I think the nested measure is mostly OK, but I does seem like the USERELATIONSHIP  in your main measure is redundant. I would rewrite the main measure to a single expression (no need for VARs and RETURN).

I get that the VARs and RETURN are likely uncessary, but my understanding is that should not impose any unccessary overhead, and at least for me it makes it easier to troubleshoot and incrementally build the measure in the first place. That said, when I tried without the USERELATIONSHIP in the Main measure it fails. In both case the calculate is being used to impose the alternate relationship path to be used to resolve the calculations, perhaps it is what it is...? I tried collapsing the [Projects Created] measure into the Main Measure and still ultimatly ran into issues. I was not able to get it down to a single CALCULATE/USRERELATIONSHIP wrapper.

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

Find out what's new and trending in the Fabric community.