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

Join us at FabCon Atlanta from March 16 - 20, 2026, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM. Register now.

Reply
ssingh33
New Member

How to dynamically count unique values in a table visual

Hi,

We are trying to create a power bi report which counts unique products based on account within a dynamic date range.

account date productlocation unique_product_count
12310/1/20201 
12310/1/20202 
12310/1/20203 
12311/1/2020b2 
12311/1/2020b3 
12311/1/2020b4 
12312/1/2020b1 
12312/1/2020b2 
12312/1/2020b4 

 

We want to identify for a given date range (based on a date slicer in the report) which account(s) have more than one unique product
In the above example, if the date slicer is between 10/1/2020 and 12/31/2020 the unique_product_count should be 2 for all rows.
If the slicer is between 11/1/2020 and 12/31/2020 the unique_product_count should be 1 for all rows.
if the slicer is between 10/1/2020 and 11/15/2020 the unique_product_count should be 2 for all rows.

 

Thanks in advance.

1 ACCEPTED SOLUTION

Hey @ssingh33 ,

 

I think that description helped me to understand your result better 😊

Try that version:

Unique Products =
CALCULATE(
    DISTINCTCOUNT( mytable[product] ),
    ALLEXCEPT(
        myTable,
        myTable[Account],
        myTable[Date]
    ),
    ALLSELECTED( myTable[Date] )
)

 

If you need any help please let me know.
If I answered your question I would be happy if you could mark my post as a solution ✔️ and give it a thumbs up 👍
 
Best regards
Denis
 

View solution in original post

7 REPLIES 7
ssingh33
New Member

Hi @selimovd 

 

The solution works for Import mode, but not when we use it in a report that has Direct query. When we use Direct Query, the value for distinct product is always 1.

Is this an issue with the behavior of the ALLEXCEPT and/or ALLSELECTED functions?

Hello @ssingh33 ,

 

yes, both ALLEXCEPT and ALLSELECTED won't work with DirectQuery.

Is there a specific reason you are using DirectQuery? It has a lot of disadvantages and is most of the times not needed.

 

Best regards

Denis

selimovd
Super User
Super User

Hey @ssingh33 ,

 

if I understood the requirements right, the following measure should give you the desired result:

Unique Products per =
VAR vFilterTable =
    ADDCOLUMNS (
        SUMMARIZE ( myTable, myTable[account], myTable[product] ),
        "@AmountProducts", CALCULATE ( DISTINCTCOUNT ( mytable[product] ) )
    )
RETURN
    SUMX ( vFilterTable, [@AmountProducts] )

 

If you need any help please let me know.

If I answered your question I would be happy if you could mark my post as a solution ✔️ and give it a thumbs up 👍

 

Best regards

Denis

 

Blog: WhatTheFact.bi

Follow me: twitter.com/DenSelimovic

 

Hi,

 

Thank you for the quick response. We tried the measure but it returns a value of 1 when there are 2 unique products. The measure you give makes sense, but the way PowerBI calculates the unique products doesn't seem to take into account all the valuse of the dataset. 

 

 

ssingh33_0-1624392798740.png

From the above screenshot, the Unique Products per column should have a value of 2 for all the rows.

 

Thanks

Hey @ssingh33 ,

 

I think that description helped me to understand your result better 😊

Try that version:

Unique Products =
CALCULATE(
    DISTINCTCOUNT( mytable[product] ),
    ALLEXCEPT(
        myTable,
        myTable[Account],
        myTable[Date]
    ),
    ALLSELECTED( myTable[Date] )
)

 

If you need any help please let me know.
If I answered your question I would be happy if you could mark my post as a solution ✔️ and give it a thumbs up 👍
 
Best regards
Denis
 
Anonymous
Not applicable

Hi @selimovd 
This works for me, except that distinctcount does not count the 'object' with an empty value.

In other words: A location has 100 objects and every object has a value column. I want to know how many objects I have, even when this value column is empty. But I want to have it dynamic, because some objects are irrelevant based on a filter (like when an object only shows midnight values, it does not belong in the midday objects). 
The concrete issue is that I wanted to compare corresponding objects from different locations, but 1 small location did not show that object (the values show how many issues that object had in the past year).

 

I might decide to use the non dynamic count if this does not work.

 

Here is my Measure (TagCode being all the unique objects from all locations):

Count_Object_All =
CALCULATE(
    DISTINCTCOUNT(L_PD_Totaal[TagCode]),
    ALLSELECTED(L_PD_Totaal[TagCode])
)
 
Thanks, Gert

That works. 

Thanks for your help! 

Helpful resources

Announcements
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!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

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

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.