Matrix Filtered by Distinct Counts Exports No Data

Greetings,

I have attached a simplified PBIX which demonstrates an issue I have run into. The goal is to have a matrix which lists products worked on by more than one employee, “overlap”. I was able to achieve this in the blue matrix (closest to the bottom). In order to achieve this I used this measure:

Overlap =
CALCULATE( DISTINCTCOUNT( ‘Example Table’[Employee] ) ,
FILTER( ‘Example Table’ ,
[Σ Hours] <> 0 ) )

and filtered the matrix on the measure having a value greater than 1.

The issue is when I go to export the blue matrix to excel, the export contains no data. This issue is encountered in both desktop and service. I believe the root of the issue is that the filter is only applied to the rows (products) in the matrix but is applied to both the rows and columns (employees) upon export. With each employee having a DISTINCTCOUNT( employee) of 1 they are all excluded!

I also tried to create a calculated column but that removed the context of the month slicer.

Any help or direction would be greatly appreciated as I have spent nearly half a day working this problem.

Thanks
Malcolm

Filter by Distinct Count Issue.pbix (59.6 KB)

@MalcolmJ Something is wrong with that visual, it is unable to show anything when Eployee field is on rows and Product field is on columns. A same visual shown on the right hand side works fine.

The visual you are showing on the right does not achieve the goal as it is showing Banana, Lime, and Pear which have only been worked by one employee in Jan 21.

The goal is to have a matrix which lists products worked on by more than one employee, “overlap”.

I have the matrix but it will not export to excel.

Hello @MalcolmJ,

Thank You for posting your query onto the Forum.

Well, by far this is the trickiest question I’ve ever faced onto the Forum. Let’s understand why the results are evaluated correctly in Power BI but when exported to Excel everything get’s blanked out. Let’s see this step-by-step to understand in a better manner -

1). This is the formula you’ve written for “Overlap” -

Overlap = 
CALCULATE( DISTINCTCOUNT( 'Example Table'[Employee] ) ,
    FILTER( 'Example Table' ,
        [Σ Hours] <> 0 ) ) 

And based on that formula, this is the result you get. Below is the screenshot of the result provided for the reference -

Overlap Result

Now, based on this result you substituted the measure into the “FILTER PANE” stating the condition as “Overlap > 1”. And this is the final result you got. Below is the screenshot provided for the reference -

Result Of More Than 1 Employee Worked

Up till now, everything was going absolutely great. And in normal circumstances, this is the correct approach I would say (since you harvested the measure into the FILTER PANE to get the results) but when you tried to export the data to the Excel file that’s where things started to go downhill.

The reason why it gave blank is because at the back-end the context was evaluated like this. Below is the screenshot provided for the reference -

Blank Result

Now, you must be wondering how did this happened actually since at each and every step in Power BI you see the correct results. So now, if you remember, you inserted the condition under the FILTER PANE as “Overlap > 1”. This condition actually worked at a “Total Level” and not at a “Individual Item Level”. See how it actually worked and what it actually ignored. Below is the screenshot provided for the reference -

Conditions Evaluated For Overlap

If you observe the above picture, Power BI at back-end was considering the “Total Level” and when we export the data into the Excel we don’t see any “Totals” over there rather we see individual line item results. So therefore, now you need to create a measure that actually considers individual line item context and not “Total Level” context.

2). Creating “Overlap” measure that works at a “Individual Line Item” context -

Overlap - Harsh = 
DIVIDE( 
    COUNTROWS( 'Example Table' ) , 
    CALCULATE( COUNTROWS( 'Example Table' ) , ALLSELECTED( 'Example Table'[Employee] ) ) , 
    0 )

Revised Overlap Results

Now, if you see the results above. The “Figure of Every Row is divided by the Total Of That Row”. So for example, Apple has 3 observations so each observation is divided by the total i.e. 1/3 = 0.33. It actually a “Percentage” so overall result is 1.00 i.e. 100% and Apple is 0.33 each i.e. 0.33% each.

But now, the problem is you cannot put these directly into the “FILTER PANE” stating the condition as “Overlap < 1”. It will simply give you the results as “Blank”. Since technique only works at a total level. So now, you’ll be required to create a formula that actually filters out the employees who’ve actually worked on more than 1 product.

3). Create a formula to Find Out More Than 1 Employee Working onto the same Product -

Overlap - Harsh = 
VAR _Hours_Calculation = 
SUMX( 
    'Example Table' , 
    'Example Table'[Hours] )

VAR _Distinct_Employees_Calculation = 
DIVIDE( 
    COUNTROWS( 'Example Table' ) , 
    CALCULATE( COUNTROWS( 'Example Table' ) , 
        ALLSELECTED( 'Example Table'[Employee] ) ) , 
    0 )

VAR _Results = 
IF( _Distinct_Employees_Calculation < 1 , 
    _Hours_Calculation ,
    BLANK() )

RETURN
_Results

Now, if you see the results it actually filters out the records of only those employees who’ve worked on more than 1 product. Below is the screenshot provided for the reference -

Filtered Employee Results Who Worked On More Than 1 Product

But the grand total is incorrect. Now, we might create a measure to fix the grand total results based on the post which @Greg had already created onto the Forum. But due to the condition specified into the measure as “BLANK()” the measure will only evaluate the results at a total level and will also skip the “Employees”. Overall see how the results will look like. Below is the screenshot provided for the reference -

Totals = 
SUMX(
    SUMMARIZE(
        'Example Table' , 
        'Example Table'[Product] , 
        'Example Table'[Employee] , 
        "@Totals" , 
        [Overlap - Harsh] ) , 
    [@Totals]
)

Total Results

So the reason why it’s ignoring “Employees” is because in a row there’re some blanks and some are not and due to “BLANK()” specified as a condition in the formula it ignoring entire row and therefore affecting the totals in it’s entirety.

So now here’s the “Trick”, just simply overlay the new table visual over the old one by using the following techniques.

4). Technique to Overlay New Table over Old One -

A). Select the New Table Visual, go to the “View” tab and select the “Selection” option.

B). Under the Layer Order menu, set the new visual as first priority and the old visual as second one and than group them together.

C). Turn-off the Background of the New Table visual.

D). Turn-off the Row and Column sub-totals of the New Table visual.

E). Finally, overlay the new one over the old.

This is how the final result will look like. Below is the screenshot provided for the reference -

Final Results

You’ll see that correct individual as well as grand totals are visible.

5). Export Data to Excel -

Now, let’s try and see whether the the table visual exports the data correctly or not. Since this is what we’ve actually worked for. Below is the screenshot provided for the reference -

Data Exported To Excel

Phew, finally, it does!!!

I’m also attaching the working of the PBIX file for the reference so that you can have a close look at it.

Hoping you find this useful and meets your requirements that you’ve been looking for. :slightly_smiling_face:

Thanks and Warm Regards,
Harsh

Filter by Distinct Count Issue - Harsh.pbix (63.6 KB)

4 Likes

Hi @MalcolmJ, did the response provided by @Harsh help you solve your query? If not, how far did you get and what kind of help you need further? If yes, kindly mark as solution the answer that solved your query. Thanks!

@EnterpriseDNA @Harsh

Thank you very much Harsh! The solution appears to be perfect.

I have tried dozens of different methods to attempt fixing this issue. Prior to posting (and after) I had spent hours searching every Power BI resource I could think of. @Harsh’s remark, “Well, by far this is the trickiest question I’ve ever faced onto the Forum.” was very appreciated as I was quite frustrated by this one.

Apologies for the delay in response, I have been quite busy and wanted to follow the steps of implementation prior to confirming. All seems great and I will be sure to mark the post as the solution.

Awesome job and many thanks,
Malcolm

@Harsh Great solution! I totally missed to check the Filters pane, my bad.

Hello @MalcolmJ,

You’re Welcome. :slightly_smiling_face:

I’m glad that I was able to assist you and you found the solution to be helpful. :+1:

Thank You @AntrikshSharma. :slightly_smiling_face:

Have a great weekend ahead.

Thanks and Warm Regards,
Harsh