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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
sean_cochran
Resolver I
Resolver I

Measure displays values for excluded categories - what am I misunderstanding?

I have a table of average wage data from the bureau of labor statistics:

sean_cochran_1-1663101130493.png

 

I want to create a measure that shows the weighted average of hourly wages for the "Production" population type only. I added a calculated column ("Weighted Amount" = hourly wage * employee count) to help with the weighted average calculation. I created a measure that divides the sum of weighted amount by the sum of employees:

AVG Production Hourly Wage =

CALCULATE(
    (
     SUM(FactBLSHourly[Weighted Amount]) / SUM(FactBLSHourly[BLS Employees (Thousands)])
     ),
    FactBLSHourly[BLS Data Population Type] = "Production"
)

This works, but values also appear for the "Total" population type as well:

sean_cochran_3-1663101372704.png

I expected there to be no values for the "Total" population type. The CALCULATE statement excludes them. Why are values showing up in the excluded category, and what am I misunderstanding about this measure?

 

Sample file with data available upon request.

1 ACCEPTED SOLUTION
v-yueyunzh-msft
Community Support
Community Support

Hi, @sean_cochran 

According to your description According to your description, you have doubts about the return result of the metric you wrote. Right?

Here is my explanation:

(1) First, based on your measure, we can write an equivalent measure as follows:

AVG Production Hourly Wage =

CALCULATE(

    (

     SUM('Test'[Weighted Amount]) / SUM('Test'[BLS Employees(Thousands)])

     ),

   FILTER(ALL(Test[BLS Data Population Type]), 'Test'[BLS Data Population Type] = "Production")

)

 

(2) We can see that using a boolean expression below the CALCULATE() function will automatically clear the filter of the [BLS Data Population Type] column, thus displaying data in the visual that is inconsistent with the expected result.

vyueyunzhmsft_0-1663138823380.png

 

(3) For the filter of CALCULATE(), we need to keep the filter of the column on the visual object, we can add the KEEPFILTERS() function outside the filter, the result is as follows:

AVG Production Hourly Wage =

CALCULATE(

    (

     SUM('Test'[Weighted Amount]) / SUM('Test'[BLS Employees(Thousands)])

     ),

   KEEPFILTERS('Test'[BLS Data Population Type] = "Production")

)

 

vyueyunzhmsft_1-1663138823384.png

 

Best Regards,

Aniya Zhang

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

5 REPLIES 5
v-yueyunzh-msft
Community Support
Community Support

Hi, @sean_cochran 

According to your description According to your description, you have doubts about the return result of the metric you wrote. Right?

Here is my explanation:

(1) First, based on your measure, we can write an equivalent measure as follows:

AVG Production Hourly Wage =

CALCULATE(

    (

     SUM('Test'[Weighted Amount]) / SUM('Test'[BLS Employees(Thousands)])

     ),

   FILTER(ALL(Test[BLS Data Population Type]), 'Test'[BLS Data Population Type] = "Production")

)

 

(2) We can see that using a boolean expression below the CALCULATE() function will automatically clear the filter of the [BLS Data Population Type] column, thus displaying data in the visual that is inconsistent with the expected result.

vyueyunzhmsft_0-1663138823380.png

 

(3) For the filter of CALCULATE(), we need to keep the filter of the column on the visual object, we can add the KEEPFILTERS() function outside the filter, the result is as follows:

AVG Production Hourly Wage =

CALCULATE(

    (

     SUM('Test'[Weighted Amount]) / SUM('Test'[BLS Employees(Thousands)])

     ),

   KEEPFILTERS('Test'[BLS Data Population Type] = "Production")

)

 

vyueyunzhmsft_1-1663138823384.png

 

Best Regards,

Aniya Zhang

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

Thank you! This is the first time I have encountered the KEEPFILTERS function. I want to make sure I understand. By default, a measure made with the CALCULATE function will disregard any segmenters that are used as filters. Because I used the column [BLS Data Population Type] as a filter in a CALCULATE function, the resulting measure did not respect [BLS Data Population Type] in the filter context of visualizations (such as a table). The solution to this was to wrap the filter in the "KEEPFILTER" function, which forces the measure to respect this column again. Is this correct?

Hi, @sean_cochran 

Your understanding is correct, you can refer to the documentation about KEEPFILTERS(): https://docs.microsoft.com/en-us/dax/keepfilters-function-dax 

Second, if you don't want to use the KEEPFILTERS() function you can also use the following DAX: 

AVG Production Hourly Wage =
CALCULATE (
    ( SUM ( 'Test'[Weighted Amount] ) / SUM ( 'Test'[BLS Employees(Thousands)] ) ),
    FILTER ( 'Test', 'Test'[BLS Data Population Type] = "Production" )
)

 

Best Regards,

Aniya Zhang

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

Thank you!

Thank you! This is the first time I have encountered the KEEPFILTERS function. I want to make sure I understand. By default, a measure made with the CALCULATE function will disregard any segmenters that are used as filters. Because I used the column [BLS Data Population Type] as a filter in a CALCULATE function, the resulting measure did not respect [BLS Data Population Type] in the filter context of visualizations (such as a table). The solution to this was to wrap the filter in the "KEEPFILTER" function, which forces the measure to respect this column again. Is this correct?

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.