cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
ConwayA
Frequent Visitor

Optimize Dax

I have a measure which counts the number of days which are applicable toward goals my team has.  it exlcudes holidays and weekends and changes the start date of the slicer to the training date if that date is larger than the start date.  It also provides a zero day count if the training date is larger than the slicers end date.  It works when I have the data extremely filtered to one month of training dates, but runs slow and fails when i have the training date filter off.  Please advise how I could improve the measure's performance. 

 

NBR Days = --this excludes holidays and weekend--
var Startdate = MAX(MIN('Safety Contact Training'[Date Attended]), MIN(DimDate[Cal_Date_DT]))
var enddate = MAX(DimDate[Cal_Date_DT])
var Trainingdate = IF( MIN('Safety Contact Training'[Date Attended])< enddate, 1,0)
RETURN

IF(Trainingdate = 1,
    CALCULATE(
        COUNTROWS(DimDate),
        FILTER(DimDate,DimDate[SA_Workday] = TRUE()),
   
        DATESBETWEEN(DimDate[Cal_Date_DT],Startdate,enddate)
        ),
 "0"    
   
 
)
2 ACCEPTED SOLUTIONS
Mikelytics
Resident Rockstar
Resident Rockstar

Hi @ConwayA 

 

Did you check the following function?

NETWORKDAYS function (DAX) - DAX | Microsoft Learn

 

Best regards

Michael

-----------------------------------------------------

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

@ me in replies or I'll lose your thread.

-----------------------------------------------------

LinkedIn

------------------------------------------------------------------
Visit my blog datenhungrig which I recently started with content about business intelligence and Power BI in German and English or follow me on LinkedIn!

View solution in original post

Hi @ConwayA 

 

Is there a reason why you use the CalculateTable statement in the measure? Because this means in eahc measure calculation you calculate the same thing which increases the amount of preformance required. If it sis so slow can you not pre-calculate the table in Power Query or Dax so that you have a separate physical table you only refer to?

 

I also see that you have many other values in your visual. Is mabe one of the other measures the performance killer?

 

Best regards

Michael

-----------------------------------------------------

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

@ me in replies or I'll lose your thread.

-----------------------------------------------------

LinkedIn

------------------------------------------------------------------
Visit my blog datenhungrig which I recently started with content about business intelligence and Power BI in German and English or follow me on LinkedIn!

View solution in original post

5 REPLIES 5
Mikelytics
Resident Rockstar
Resident Rockstar

Hi @ConwayA 

 

Did you check the following function?

NETWORKDAYS function (DAX) - DAX | Microsoft Learn

 

Best regards

Michael

-----------------------------------------------------

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

@ me in replies or I'll lose your thread.

-----------------------------------------------------

LinkedIn

------------------------------------------------------------------
Visit my blog datenhungrig which I recently started with content about business intelligence and Power BI in German and English or follow me on LinkedIn!

@Mikelytics , 

 I updated the formula to use Networkdays but it is not resolving the issue, this is still running super slow when I remove the filter of only those trained in October.  it works well when I remove the If statement but

it runs fine but it gives me negative days for those trained in November. 

new formula

NBR Days = --this excludes holidays and weekend--
var Startdate = max(MIN('Training'[Date Attended]), min(DimDate[Cal_Date_DT]))
var enddate = MAX(DimDate[Cal_Date_DT])
var Trainingdate = IF( MIN(' Training'[Date Attended])< enddate, 1,0)
RETURN
   
        NETWORKDAYS(Startdate, enddate, 1,
             CALCULATETABLE(
                 SELECTCOLUMNS(DimDate,"Holidays", DimDate[Cal_Date_DT]),
                 FILTER(DimDate,DimDate[Holiday_FLG] = 1)
            )
        )          
   
Results date slicer October 1, 2022 - October 31, 2022
ConwayA_0-1669912335363.png

 

 

Hi @ConwayA 

 

Is there a reason why you use the CalculateTable statement in the measure? Because this means in eahc measure calculation you calculate the same thing which increases the amount of preformance required. If it sis so slow can you not pre-calculate the table in Power Query or Dax so that you have a separate physical table you only refer to?

 

I also see that you have many other values in your visual. Is mabe one of the other measures the performance killer?

 

Best regards

Michael

-----------------------------------------------------

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

@ me in replies or I'll lose your thread.

-----------------------------------------------------

LinkedIn

------------------------------------------------------------------
Visit my blog datenhungrig which I recently started with content about business intelligence and Power BI in German and English or follow me on LinkedIn!

@Mikelytics , 

I changed holiday table back to a table to point to.  no change in the failure rate. 

I do not think it is a problem with the other items as it has run fine for a while and runs quick enough until i add this if statement. to get the zero day count when the training date is greater than the enddate. 

@Mikelytics,

You were right  i had to change another measure I had using this measure.  Thanks for your help. 

Helpful resources

Announcements
Carousel_PBI_Wave1

2023 Release Wave 1 Plans

Power BI release plans for 2023 release wave 1 describes all new features releasing from April 2023 through September 2023.

Power BI Summit Carousel 2

Global Power BI Training

Make sure you register today for the Power BI Summit 2023. Don't miss all of the great sessions and speakers!

Thank you 2022 Review

2022 Monthly Feature Releases

We had a great 2022 with a ton of feature releases to help you drive a data culture.