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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
ryan25r9
Helper I
Helper I

Measure Efficiency -- Get distinct values, average per single date, then sum the averages

Hey everyone,

 

I'm trying to find a way to refactor this measure to make it more efficient, since it's currently pretty slow. I've been spinning my wheels, so any help is greatly appreciated.

 

We need to get distinct values per Employee & Date because the TLG table also has a more granular line level, and Workday Days is just per Employee & Date Worked.

 

The general idea here is that two different employees can have a different workday per date, because one might be in a country that has different holidays or weekends from the other. So in order to get the real total workdays for "Hours per Day" denominators etc, I need to find average workdays for each date.

 

EVALUATE

VAR DistinctEmpDays = 

		SUMMARIZE(
        		TLG
                , TLG[Date Worked], TLG[Employee ID], TLG[Workday Days]
        )
        
VAR AvgDays = /* Average workdays per single day */

        GROUPBY(
        		DistinctEmpDays
                , [Date Worked]
                , "AvgWorkdays"
                , AVERAGEX( CURRENTGROUP(), [Workday Days] ) 
        )
        
VAR Total = /* Sum daily avg for total workdays */

		SUMX( AvgDays, [AvgWorkdays] )
                
RETURN  
		ROW("Test", Total)

 

8 REPLIES 8
v-lionel-msft
Community Support
Community Support

Hi @ryan25r9 ,

 

Or like this?

Measure = 
VAR x = 
AVERAGEX(
    FILTER(
        TLG,
        TLG[Date Worked] = SELECTEDVALUE(TLG[Date Worked])
    ),
    TLG[Workday Days]
)
RETURN
ROW(
    "Test",
    SUMX(
        TLG,
        x
    )
)

 

Best regards,
Lionel Chen

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

Sorry, this doesn't quite get there either. It doesn't account for there being duplicate TLG[Workday Days] per Date Worked & Employee.

 

Probably helps to give an example. Starting dataset:

Employee IDDate WorkedWorkday DaysTLP ID
49101/05/20010
49101/06/200.54
49101/06/200.5941
49101/07/201331
49101/08/201596
49101/08/2012537
1140201/06/201319
1140201/06/201782
1140201/07/201331
1140201/08/201841
9982101/05/20010
9982101/06/201319
9982101/07/2012537
9982101/07/201596
9982101/07/201941
272901/05/201331
272901/05/2011931
272901/06/200.5841

 

Distinct values of Employee ID, Date Worked, & Workday Days:

Employee IDDate WorkedWorkday Days
49101/05/200
49101/06/200.5
49101/07/201
49101/08/201
1140201/06/201
1140201/07/201
1140201/08/201
9982101/05/200
9982101/06/201
9982101/07/201
272901/05/201
272901/06/200.5

 

AVERAGE Workday Days by day:

Date WorkedAverage Workday Days
01/05/200.333
01/06/200.750
01/07/201.000
01/08/201.000

 

Lastly, the SUM of averages, making the expected result of the measure here:

3.083

Hi,

You may download my PBI file from here.

Hope this helps.

Untitled.png


Regards,
Ashish Mathur
http://www.ashishmathur.com
https://www.linkedin.com/in/excelenthusiasts/

Greatly appreciate the Power BI file, thanks so much. This actually returns the correct results, but running on the entire model drops the efficiency from 25 seconds to 171 seconds. The core table (TLG) is 700M rows, so that certainly isn't helping with the context transition. For reference, this is the old code simplified and your recommended code altered to run in DAX Studio:

 

EVALUATE

VAR Old = 	
		SUMX(
				GROUPBY(
						SUMMARIZE(TLG, TLG[Date Worked], TLG[Employee ID], TLG[Workday Days])
						, [Date Worked]
						, "AvgWorkdays"
						, AVERAGEX( CURRENTGROUP(), [Workday Days] ) 
				)
				, [AvgWorkdays]
		)

VAR New =
		SUMX (
			    VALUES ( 'TLG'[Date Worked] ),
			    AVERAGEX (
				        SUMMARIZE (
					            VALUES ( TLG[Employee ID] ),
					            [Employee ID],
					            "AvgWorkdays", CALCULATE(AVERAGE(TLG[Workday Days]))
				        ),
			        [AvgWorkdays]
			    )
		)

RETURN
	ROW("test", New)

 

Thanks for providing sample data and output.  This measure should work for you in a table with Dates on the rows.

 

NewMeasure =
VAR __summary =
ADDCOLUMNS (
SUMMARIZE ( TLG, TLG[Employee ID], TLG[Date Worked] ),
"AvgHrs", CALCULATE ( AVERAGE ( TLG[Workday Days] ) )
)
RETURN
AVERAGEX ( __summary, [AvgHrs] )

 

If this works for you, please mark it as solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Greatly appreciate the reply! Unfortunately this measure is in an SSAS tabular model, so I need it to work in all contexts and can't limit to a table with dates on the rows.

mahoneypat
Employee
Employee

Please try this measure instead.

 

NewMeasure = Sumx(Values(TLG[Date]), calculate(average(TLG[Workday Days])))

 

If this works for you, please mark it as the solution.  Kudos are appreciated too.  Please let me know if not.

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Thank you, but this won't work. It doesn't change the granularity to Employee & Date Worked, so it's aggregating millions of duplicates.

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

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