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
RegionH
Helper I
Helper I

Slow DAX formula to calculate # Orders Delayed with adjustment

Hi

I have a datamodel with orderlines and goods receipt. I have written a formula to calculate number of unique orders delayed (in Danish # Ordre Forsinket) based on two what if parameters.

The parameter 'Leveringstid Regionslager rabat'[Leveringstid Regionslager rabat] is used to adjust the delay with X number of days on those orders there are marked as indkøbsbilagsart ZCD or ZLM.

 

The parameter 'Leveringstid Øvrige rabat'[Leveringstid Øvrige rabat] is used to adjust the delay with X number of days on those orders there are different from indkøbsbilagsart ZCD or ZLM.

 

The data model looks like this and there is 1.4 million rows in the table 'Ordrelinjer' and 2.775 suppliers in the table 'Leverandør':

RegionH_1-1720705589360.png


Here is the DAX formula:

 

# Ordre Forsinket = 
VAR _LeveringstidRegionslagerRabat = SELECTEDVALUE( 'Leveringstid Regionslager rabat'[Leveringstid Regionslager rabat], 0 )
VAR _LeveringstidOevrigeRabat = SELECTEDVALUE( 'Leveringstid Øvrige rabat'[Leveringstid Øvrige rabat], 0 )

   
RETURN
CALCULATE(
    DISTINCTCOUNT('Ordrelinjer'[Indkøbsordrenummer]),
    FILTER(
        ADDCOLUMNS(
            FILTER(
                'Ordrelinjer',
                'Ordrelinjer'[Varemodtagelse] = 1 && 'Ordrelinjer'[Afvigelse i dage] > 0
            ),
            "JusteretAfvigelse", 
            'Ordrelinjer'[Afvigelse i dage] -
            IF(
                RELATED('Indkøbsbilagsart'[Indkøbsbilagsart]) IN {"ZCD", "ZLM"},
                _LeveringstidRegionslagerRabat,
                _LeveringstidOevrigeRabat
            )
        ),
        [JusteretAfvigelse] > 0
    )
)

 

The measure is used in a table visual, but it takes around 33 seconds to refresh the table visual. I need a few more measures in the table, so it will just take longer and longer time to refresh the table. 

RegionH_2-1720706042873.png

 

Can I write the DAX formula in a more efficient, clean and performance optimized way?

 

Best regards 

Morten

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

Run your formula through DAX Studio. Enable Query Plan display. Inspect the query plan and look for excessive cartesians (high number of records). Refactor your formula and repeat.

View solution in original post

3 REPLIES 3
RegionH
Helper I
Helper I

Just in case som of you run in to the same performance issue. Then I have written a more optimized code, which now only takes 216 ms to execute. I use COUNTROWS and SUMMARIZE to do the distinctcount. Here is the code:

# Ordre Forsinket Countrows + Summarize 280 ms = 

VAR _LeveringstidRegionslagerRabat = SELECTEDVALUE ( 'Leveringstid Regionslager rabat'[Leveringstid Regionslager rabat] )
VAR _LeveringstidOevrigeRabat = SELECTEDVALUE ( 'Leveringstid Øvrige rabat'[Leveringstid Øvrige rabat] )

   
RETURN
CALCULATE (
    COUNTROWS (
        SUMMARIZE (
            FILTER (
                ADDCOLUMNS (
                    'Ordrelinjer',
                    "JusteretAfvigelse", 
                    'Ordrelinjer'[Afvigelse i dage] -
                        IF (
                            RELATED ( 'Indkøbsbilagsart'[Indkøbsbilagsart] ) IN {"ZCD", "ZLM"},
                            _LeveringstidRegionslagerRabat,
                            _LeveringstidOevrigeRabat
                        )
                ),
                'Ordrelinjer'[Varemodtagelse] = 1 && [JusteretAfvigelse] > 0
            ),
            'Ordrelinjer'[Indkøbsordrenummer]
        )
    )
)

 

RegionH
Helper I
Helper I

Thank you for pointing me in the direction of DAX Studio. I haven’t used DAX Studio to evaluate a measure and its performance before, so I had to watch a Guy in a Cube video first. The link to it is here: https://www.youtube.com/watch?v=eABg872TAJU&t=617s.

I made a small change to the code and how the distinctcount is calculated. Instead of using distinctcount, then I use SUMX and VALUES. Here is the updated code:

 

# Ordre Forsinket = 
VAR _LeveringstidRegionslagerRabat = SELECTEDVALUE( 'Leveringstid Regionslager rabat'[Leveringstid Regionslager rabat])
VAR _LeveringstidOevrigeRabat = SELECTEDVALUE( 'Leveringstid Øvrige rabat'[Leveringstid Øvrige rabat])

   
RETURN
CALCULATE(
    // DISTINCTCOUNT('Ordrelinjer'[Indkøbsordrenummer]), -- Before
    SUMX(VALUES('Ordrelinjer'[Indkøbsordrenummer]),1), -- After
    FILTER(
        ADDCOLUMNS(
            FILTER(
                'Ordrelinjer',
                'Ordrelinjer'[Varemodtagelse] = 1 && 'Ordrelinjer'[Afvigelse i dage] > 0
            ),
            "JusteretAfvigelse", 
            'Ordrelinjer'[Afvigelse i dage] -
            IF(
                RELATED('Indkøbsbilagsart'[Indkøbsbilagsart]) IN {"ZCD", "ZLM"},
                _LeveringstidRegionslagerRabat,
                _LeveringstidOevrigeRabat
            )
        ),
        [JusteretAfvigelse] > 0
    )
)

 


Now I see these numbers in Power BI Performance Analyzer and DAX Studio under Server Timings, which is more acceptable:

Before change:

RegionH_0-1720766796797.png

RegionH_3-1720767137656.png



After change:

RegionH_1-1720766820621.png
RegionH_2-1720766912400.png

 

Best regards
Morten

lbendlin
Super User
Super User

Run your formula through DAX Studio. Enable Query Plan display. Inspect the query plan and look for excessive cartesians (high number of records). Refactor your formula and repeat.

Helpful resources

Announcements
July 2025 community update carousel

Fabric Community Update - July 2025

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

July PBI25 Carousel

Power BI Monthly Update - July 2025

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