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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
spradhan_pm
Regular Visitor

i need to optimize my dax query .. any help is appreciated

i have 
a distinct user table 
5 diff (many side relationship table to user ) - Jobs table, dealer table, hire table, termination table.. etc

i have about 55000+ distinct users and a total of 2.6M rows of data in total 

User_shadow table: ( externalcode is used as id )

ExternalCodename
123james
124joe
125jill
126jay



job_shadow table:

ExternalCodeprimary_job_cddate
123101/02/2020
123202/01/2020
124 2 01/01/2020
1243 03/01/2020



Dealer_shadow table:

ExternalCodedlr_cddate
12311201/02/2020
12321103/01/2020
124122 01/01/2020
124112 04/01/2020


calculation max in job (same logic to get each max dealer, hiredate, terminated date as well):

latestJob_max = 

    var SelectedMaxDate = 
    MAX('Calendar'[Date])
    //[relative_date_max]

VAR MaxDateBefore = 
    CALCULATE(
        MAX(Job_Shadow[umd_date]), 
        ALL(Job_Shadow[umd_date],Job_Shadow[PRIMARY_JOB_CD]), // This removes filters only from umd_date and ExternalCode
        // Job_Shadow[ExternalCode] = "D00031301",
        Job_Shadow[umd_date] < SelectedMaxDate,
        REMOVEFILTERS(Job_Lookup)
    )

VAR Result = 
    CALCULATE(
        MAX(Job_Shadow[PRIMARY_JOB_CD]),
        Job_Shadow[PRIMARY_JOB_CD] in VALUES(Job_Lookup[JobID]),
        Job_Shadow[umd_date] = MaxDateBefore
    )

RETURN
    Result
   



ending count dax:

 

 

count_max = 


    var SelectedMaxDate = 
	 Max('Calendar'[Date])
        //[relative_date_max]
    var SelectedMinDate = 
        //[relative_date_min]
	Min('Calendar'[Date])
		//  DATE(2024,2,31)

		
	var selectedDealer = 
        Values('Dealer/District/Zone/Region_Lookup'[DealerID])
//    SELECTEDVALUE('Dealer/District/Zone/Region_Lookup'[DealerID])
    //  "10130"

    
	var selectedJob = 
        Values(Job_Lookup[JobID])
//    SELECTEDVALUE(Job_Lookup[JobID])
		// "D102"
		

        
		
	var selectedTenure = 
        Values(TenureSort_Reference[TenureSort])

    
	var selectedDriveR = 
        Values(DriveParticipation_Lookup[Column1])
        
return
Calculate(
    Count(User_Shadow[ExternalCode]),
    FILTER(
        User_Shadow,
        Calculation_shadow[latestDealer_max] in selectedDealer
//        && Calculation_shadow[latestJob_max] in selectedJob
//        && Calculation_shadow[latestTenure_max] in selectedTenure
//        && Calculation_shadow[latestDriveR_max] in selectedDriveR
        && NOT(Calculation_shadow[latestJob_max]  IN {"D345", "D346", "D347", "D365", "D366", "D367"})
        && LEFT(Calculation_shadow[latestJob_max], 1) = "D"
        
        &&
         Calculation_shadow[latestHire_max] <= SelectedMaxDate
        && (
            ISBLANK(Calculation_shadow[latestFire_max])
            || ( 
                not(DATEVALUE(Calculation_shadow[latestFire_max]) > DATEVALUE(Calculation_shadow[latestHire_max]))
                )
        )
    )
        
    ,CROSSFILTER(
        'Job_Lookup'[JobID],
        Job_Shadow[PRIMARY_JOB_CD]
        ,None
    )
    ,CROSSFILTER(
        'Dealer/District/Zone/Region_Lookup'[DealerID],
        Dealer_Shadow[DLR_CD]
        ,None
    )
)+0



    

 

 


startin count dax:

 

 

count_min = 


    var SelectedMaxDate = 
	Min('Calendar'[Date])
        //[relative_date_min]
		//  DATE(2024,2,31)

		
	var selectedDealer = 
        Values('Dealer/District/Zone/Region_Lookup'[DealerID])
//    SELECTEDVALUE('Dealer/District/Zone/Region_Lookup'[DealerID])
    //  "10130"

    
	var selectedJob = 
        Values(Job_Lookup[JobID])
//    SELECTEDVALUE(Job_Lookup[JobID])
		// "D102"
		
	var selectedTenure = 
        Values(TenureSort_Reference[TenureSort])

    
	var selectedDriveR = 
        Values(DriveReimbursement_Shadow[DriveParticipation])

RETURN
CALCULATE (
    COUNT(User_Shadow[ExternalCode]),
    FILTER(
        User_Shadow,
        Calculation_shadow[latestDealer_min] in selectedDealer
        && Calculation_shadow[latestJob_min] in selectedJob
        && Calculation_shadow[latestTenure_min] in selectedTenure
        && Calculation_shadow[latestDriveR_min] in selectedDriveR
        
        &&
            NOT(Calculation_shadow[latestJob_min]  IN {"D345", "D346", "D347", "D365", "D366", "D367"})
        && LEFT(Calculation_shadow[latestJob_min], 1) = "D"
        
        


        &&
        Calculation_shadow[latestHire_min]  <= SelectedMaxDate
        && (
            ISBLANK(Calculation_shadow[latestFire_min])
            || ( 
                    Not(DATEVALUE(Calculation_shadow[latestFire_min]) > DATEVALUE(Calculation_shadow[latestHire_min] ))

                )
                
        )
    )
    
    ,CROSSFILTER(
        'Job_Lookup'[JobID],
        Job_Shadow[PRIMARY_JOB_CD]
        ,None
    )
    ,CROSSFILTER(
        'Dealer/District/Zone/Region_Lookup'[DealerID],
        Dealer_Shadow[DLR_CD]
        ,None
    )
)+0

 

 

 

average count dax:

 

 

count_avgEmp = DIVIDE([count_min] + [count_max],2,0)

 

 


terminated count dax:

 

 

count_terminated_range = 


    var SelectedMaxDate = 
	Max('Calendar'[Date])
        //[relative_date_max]
    var SelectedMinDate = 
        //[relative_date_min]
	Min('Calendar'[Date])
		//  DATE(2024,2,31)

		
	var selectedDealer = 
        Values('Dealer/District/Zone/Region_Lookup'[DealerID])
//    SELECTEDVALUE('Dealer/District/Zone/Region_Lookup'[DealerID])
    //  "10130"

    
	var selectedJob = 
        Values(Job_Lookup[JobID])
//    SELECTEDVALUE(Job_Lookup[JobID])
		// "D102"
		

        
		
	var selectedTenure = 
        Values(TenureSort_Reference[TenureSort])

    
	var selectedDriveR = 
        Values(DriveParticipation_Lookup[Column1])
        
return
CALCULATE (
    COUNT(User_Shadow[ExternalCode]),
    FILTER(
        User_Shadow,
        

            [latestJob_max] in selectedJob
        && [latestTenure_max] in selectedTenure
        && [latestDriveR_max] in selectedDriveR
        
        && NOT(Calculation_shadow[latestJob_max]  IN {"D345", "D346", "D347", "D365", "D366", "D367"})
        && LEFT(Calculation_shadow[latestJob_max], 1) = "D"
        
        &&

        
        (
            (
                Not(ISBLANK([latestFire_max]))
                &&  (

                    [latestDealer_max] in selectedDealer 
                    && SelectedMinDate <= Calculation_shadow[latestFire_max] 
                    &&  Calculation_shadow[latestFire_max] < SelectedMaxDate
                    && 
                        


                        format(Calculation_shadow[latestFire_max] , "YYYYMMDD") > format(Calculation_shadow[latestHire_max] , "YYYYMMDD")

                )
                
                
                
            )
                || 
            (
                    SelectedValue('Dealer/District/Zone/Region_Lookup'[DealerID])
                && [latestDealer_min] in selectedDealer
                && Not( [latestDealer_max] = [latestDealer_min] )
                
            )
        )

        
        
        // 'Hire_Shadow'[TRMNTN_DT] >= SelectedMinDate
        // && 'Hire_Shadow'[TRMNTN_DT] <= SelectedMaxDate
    )
    
    ,CROSSFILTER(
        'Job_Lookup'[JobID],
        Job_Shadow[PRIMARY_JOB_CD]
        ,None
    )
    ,CROSSFILTER(
        'Dealer/District/Zone/Region_Lookup'[DealerID],
        Dealer_Shadow[DLR_CD]
        ,None
    )
)+0



    

 

 


turnover percentage dax:

 

 

turnover_master = 
    DIVIDE([count_terminated_range], [count_avgEmp], 0)

 

 



it takes atleast a minute to generate a monthly line chart for turnover for a single years timeline.
how do i reduce the load time and optimize this query?

any help is appreciated. thanks



 

1 REPLY 1
Sahir_Maharaj
Super User
Super User

Hello @spradhan_pm,

 

Can you please try the following:

 

1. Optimize latestJob_max Calculation

latestJob_max = 
VAR SelectedMaxDate = MAX('Calendar'[Date])
VAR MaxDateBefore = CALCULATE(MAX(Job_Shadow[umd_date]), Job_Shadow[umd_date] < SelectedMaxDate)
RETURN CALCULATE(MAX(Job_Shadow[PRIMARY_JOB_CD]), Job_Shadow[umd_date] = MaxDateBefore)

2. Simplify the count_max and count_min Calculations

count_max = 
VAR SelectedMaxDate = MAX('Calendar'[Date])
VAR SelectedMinDate = MIN('Calendar'[Date])
VAR ValidJobIDs = EXCEPT(VALUES(Job_Lookup[JobID]), {"D345", "D346", "D347", "D365", "D366", "D367"})
RETURN
CALCULATE(
    COUNTROWS(User_Shadow),
    FILTER(
        User_Shadow,
        User_Shadow[latestJob_max] IN ValidJobIDs &&
        LEFT(User_Shadow[latestJob_max], 1) = "D" &&
        User_Shadow[latestHire_max] <= SelectedMaxDate &&
        (ISBLANK(User_Shadow[latestFire_max]) || User_Shadow[latestFire_max] > User_Shadow[latestHire_max])
    )
)

3. Revise count_terminated_range for Better Performance

count_terminated_range = 
VAR SelectedMaxDate = MAX('Calendar'[Date])
VAR SelectedMinDate = MIN('Calendar'[Date])
VAR ValidJobIDs = EXCEPT(VALUES(Job_Lookup[JobID]), {"D345", "D346", "D347", "D365", "D366", "D367"})
RETURN
CALCULATE(
    COUNTROWS(User_Shadow),
    FILTER(
        User_Shadow,
        User_Shadow[latestJob_max] IN ValidJobIDs &&
        LEFT(User_Shadow[latestJob_max], 1) = "D" &&
        NOT(ISBLANK(User_Shadow[latestFire_max])) &&
        User_Shadow[latestFire_max] >= SelectedMinDate &&
        User_Shadow[latestFire_max] < SelectedMaxDate &&
        User_Shadow[latestFire_max] > User_Shadow[latestHire_max]
    )
)

4. Reduce turnover_master Complexity

turnover_master = DIVIDE([count_terminated_range], [count_avgEmp], 0)

Hope this helps!


Did I answer your question? Mark my post as a solution, this will help others!

If my response(s) assisted you in any way, don't forget to drop me a "Kudos" 🙂

Kind Regards,
Sahir Maharaj
Data Scientist | Data Engineer | Data Analyst | AI Engineer
P.S. Want me to build your Power BI solution? (Yes, its FREE!)
➤ Lets connect on LinkedIn: Join my network of 15K+ professionals
➤ Join my free newsletter: Data Driven: From 0 to 100
➤ Website: https://sahirmaharaj.com
➤ About: https://sahirmaharaj.com/about.html
➤ Email: sahir@sahirmaharaj.com
➤ Want me to build your Power BI solution? Lets chat about how I can assist!
➤ Join my Medium community of 30K readers! Sharing my knowledge about data science and artificial intelligence
➤ Explore my latest project (350K+ views): Wordlit.net
➤ 100+ FREE Power BI Themes: Download Now
LinkedIn Top Voice in Artificial Intelligence, Data Science and Machine Learning

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

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

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

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.