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
frw
Frequent Visitor

Counting Distinct IDs with Non-Blank/Zero Values Elsewhere

Hello everyone,

I've been banging my head against the wall for the last few days trying to solve something that I feel like should be easy.

Goal: Measure that will return the number of products per category that had any sales in the selected time period (date slicer)

 

Tables: Summary table with condensed daily sales for each item (example for one day shown). As well, there is a date table with 1:* relationship to the Date column of the below table.

DateItem IDClassificationSold
1/1/20231CSR5
1/1/20232CSR7
1/1/20233CSR0
1/1/20234BCSR3
1/1/20235BCSR4
1/1/20236BCSR1
1/1/20237BUS24
1/1/20238BUS7
1/1/20239BUS0
1/1/202310RES3
1/1/202311RES4
1/1/202312RES1

 

The goal is to have a matrix/table visual that, in each row, will display the total number of products that had any sales in the time period specified by the date slicer. For example, the measure for the above data would show:
CSR: 2, BCSR: 3, BUS: 2, RES: 3

 

After playing around for a while and exhausting my Google-fu, I've got two measures that handle half of the problem but don't get me where I need.

 

 

Products Sold 1 =
CALCULATE (
    DISTINCTCOUNT ( 'Summary'[Item ID] ),
    ALLEXCEPT ( 'Summary', 'Summary'[Classification] ),
    ALLEXCEPT ( 'Date', 'Date'[Date] ),
    FILTER ( 'Summary', SUM ( 'Summary'[Sold] ) > 0 )
)

Products Sold 2 =
VAR tab =
    CALCULATETABLE (
        FILTER ( 'Summary', SUM ( 'Summary'[Sold] ) > 0 ),
        ALLEXCEPT ( 'Summary', 'Summary'[Classification] ),
        ALLEXCEPT ( 'Date', 'Date'[Date] )
    )
VAR result =
    CALCULATE ( DISTINCTCOUNT ( 'Summary'[Item ID] ), tab )
RETURN
    result

 

 

The first measure correctly counts the number of products in a classification with any sales in the time period, but it assigns a value of 1 for each item with a sale. The second, meanwhile, gives the same value for each item in the matrix as desired, but it also counts the items without sales. Preferrably, the below would show a 37 everywhere you see a 1 currently, although the alternative with 37 appearing in every row is also acceptable.

 

Screenshot 2023-02-09 150531.png

I know I must be missing something easy, but I've tried so many permutations of found DAX online that I'm starting to lose my mind.

 

1 ACCEPTED SOLUTION
DataInsights
Super User
Super User

@frw,

 

Try this measure:

 

Products Sold = 
CALCULATE (
    DISTINCTCOUNT ( Summary[Item ID] ),
    ALLSELECTED ( 'Date'[Date] ),
    Summary[Sold] > 0,
    NOT ISBLANK ( Summary[Sold] )
)

 

DataInsights_0-1676050580774.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




View solution in original post

2 REPLIES 2
DataInsights
Super User
Super User

@frw,

 

Try this measure:

 

Products Sold = 
CALCULATE (
    DISTINCTCOUNT ( Summary[Item ID] ),
    ALLSELECTED ( 'Date'[Date] ),
    Summary[Sold] > 0,
    NOT ISBLANK ( Summary[Sold] )
)

 

DataInsights_0-1676050580774.png

 





Did I answer your question? Mark my post as a solution!

Proud to be a Super User!




Hey @DataInsights,

 

Thanks for the reply. You got me almost all the way there; when I broke the category matrix (like your image) down into the member products, it was still showing 1 for each of the individual products with any sales. I added the following to the filter context, and it propogated the "Products Sold" measure through for each item in the category.

REMOVEFILTERS('SUMMARY'[ITEM ID])

 Haven't spent much time playing around with the ALL* functions in filter context, so clearly I've got some more work to do. Thanks for your help!

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.