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

Power BI is turning 10! Let’s celebrate together with dataviz contests, interactive sessions, and giveaways. Register now.

Reply
jony_gloeckner
Frequent Visitor

Count of distinct products on max date per location

Hello Community,

 

I need help with DAX measure to calculate distinct products on last date for each location.

This is fact table:

jony_gloeckner_0-1678699555468.png

and here is where I got with current measure:

jony_gloeckner_1-1678699615733.png

Thing that bothers me is that I need to show total of 4 ( 1 product for location B and 3 products for location B).

My current dax measure is:

VAR LastDateForCustomer=
    ADDCOLUMNS(
        DISTINCT( Fact[Location]),
        "@LastDate", MAX(Fact[Date])
    )
VAR FilterCustomerDate=
    TREATAS(
        LastDateForCustomer,
        Locations[Location],
        Calendar[Date]
    )
Return
    CALCULATE(
        DISTINCTCOUNT(Fact[Product]),
        FilterCustomerDate
    )
 
Fact table:
LocationDateProduct
A24.11.20222
A24.11.202210
A24.11.202211
B3.11.20225
B20.10.20228
A22.9.20222
B22.9.20225
B15.9.20225
B8.9.20225
B1.9.20226
A30.6.20222
A5.5.20224
A28.4.20222
A14.4.20223
A17.3.20221
A10.2.20227
A10.2.20229
A27.1.20224
Thank you all! 
1 ACCEPTED SOLUTION

@jony_gloeckner 
Please refer to attached

1.png

Product Count 3 = 
COUNTROWS (
    DISTINCT (
        SELECTCOLUMNS ( 
            GENERATE ( 
                VALUES ( Locations[Location] ),
                TOPN ( 
                    1,
                    CALCULATETABLE ( 'Fact' ),
                    'Fact'[Date]
                )
            ),
            "@Product", [Product]
        )
    )
)

View solution in original post

17 REPLIES 17
tamerj1
Super User
Super User

Hi @jony_gloeckner 
Please refer to attached sample file with the proposed solution

1.png

Product Count 2 = 
SUMX ( 
    VALUES ( Locations[Location] ),
    MAXX ( 
        VALUES ( 'Calendar'[Date] ),
        CALCULATE ( DISTINCTCOUNT ( 'Fact'[Product] ) )
    )
)

Hi @tamerj1 ,

thank you for your suggestion, but dax measure doesn't count distinct products.
I added to example location C that has same product as location B on max date, and it adds that product to total sum.

jony_gloeckner_0-1678791355203.png

I need to get 4 as count of products.

Pbix file is below:

Community 567.pbix 

@jony_gloeckner 
Please refer to attached

1.png

Product Count 3 = 
COUNTROWS (
    DISTINCT (
        SELECTCOLUMNS ( 
            GENERATE ( 
                VALUES ( Locations[Location] ),
                TOPN ( 
                    1,
                    CALCULATETABLE ( 'Fact' ),
                    'Fact'[Date]
                )
            ),
            "@Product", [Product]
        )
    )
)

That's it! Great! Thank you a lot!

jony_gloeckner
Frequent Visitor

@andhiii079845 @Greg_Deckler  thank you guys, I was unsure if I did something wrong, but it seems like total is the issue.
So what you say basically is that I can't get this figure as a total or a KPI value?
Because when I put measure in KPI, it also doesnit show correct count.

@Greg_DecklerI also voted for your idea.

@jony_gloeckner You can, but at that point it is kind of a different problem (actually a bit easier to solve than measure totals. It's a measure aggregation issue at that point. This looks like a measure aggregation problem. See my blog article about that here: https://community.powerbi.com/t5/Community-Blog/Design-Pattern-Groups-and-Super-Groups/ba-p/138149

The pattern is:
MinScoreMeasure = MINX ( SUMMARIZE ( Table, Table[Group] , "Measure",[YourMeasure] ), [Measure])
MaxScoreMeasure = MAXX ( SUMMARIZE ( Table, Table[Group] , "Measure",[YourMeasure] ), [Measure])
AvgScoreMeasure = AVERAGEX ( SUMMARIZE ( Table, Table[Group] , "Measure",[YourMeasure] ), [Measure])
etc.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

@Greg_DecklerI am using this measure:
CALCULATE(
DISTINCTCOUNT(Fact[Product]),
FilterCustomerDate
)
Isn't that same as the one in your pattern? I don't see what am I missing..

@jony_gloeckner I still don't understand where you are getting your desired numbers from. In the sample data provided, there are 8 distinct products for Location A and 3 distinct products for Location B. There is no overlap between Locations in terms of products. To me, that would mean that the total should be 11. But you are saying 4 and I don't know where that is coming from. Unless you are saying 3 for Location A on November 24th and 1 for Location B on November 3rd. If that is the case, then it is this for a Card visual (or KPI). And it also works in a Matrix visual as well. My PBIX file is attached below signature.

Distinct Products = 
    VAR __Table = 
        ADDCOLUMNS(
            SUMMARIZE('Table',[Location],"__MaxDate", MAX('Table'[Date])),
            "__Distinct", 
                    VAR __Date = [__MaxDate]
                    VAR __Location = [Location]
                    VAR __Result = 
                        COUNTROWS(
                            DISTINCT(
                                SELECTCOLUMNS(
                                    FILTER(ALL('Table'),[Location] = __Location && [Date] = [__MaxDate]),
                                    "__Products",[Product]
                                )
                            )
                        )
                RETURN
                    __Result
        )
    VAR __Result = SUMX(__Table,[__Distinct])
RETURN
    __Result

 

 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler, I thought that this formula is correct one, but if we add location C and we have same product on  maximum date - formula doesn't recognize that given product is on location A.
Here is example:

jony_gloeckner_0-1678781996808.png

I need to get total of 4 distinct products.
Here is .pbix:
Distinct Products

@jony_gloeckner The link doesn't work for me.



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Add it via ondrive or dropbox or google drive and share the link. 🙂





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

Proud to be a Super User!




@andhiii079845 thx for the tip 🙂

You have to use a HASONEFILTER:

IF(HASONEFILTER("Location"),"Your count measure","Other calcuation to get total")





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

Proud to be a Super User!




andhiii079845
Solution Sage
Solution Sage

If only total is the probem, we discuss it some hours ago. Its a typical "total" problem.

https://community.powerbi.com/t5/Desktop/Measures-are-getting-mapped-correctly-But-Grand-Total-is-sh...





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

Proud to be a Super User!




jony_gloeckner
Frequent Visitor

@Greg_Deckler, yes, but in total I need to show number of distinct products on all locations (4). So products 2,10 and 11 for location B and product 5 for location A.

@jony_gloeckner First, please vote for this idea: https://ideas.powerbi.com/ideas/idea/?ideaid=082203f1-594f-4ba7-ac87-bb91096c742e

This looks like a measure totals problem. Very common. See my post about it here: https://community.powerbi.com/t5/DAX-Commands-and-Tips/Dealing-with-Measure-Totals/td-p/63376

Also, this Quick Measure, Measure Totals, The Final Word should get you what you need:
https://community.powerbi.com/t5/Quick-Measures-Gallery/Measure-Totals-The-Final-Word/m-p/547907

Also: https://youtu.be/uXRriTN0cfY
And: https://youtu.be/n4TYhF2ARe8



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...
Greg_Deckler
Super User
Super User

@jony_gloeckner So, your sample data ony has 3 distinct values for Product for Location B (5, 8 and 6). Unless I am not understanding the requirement.

 



Follow on LinkedIn
@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
Power BI Cookbook Third Edition (Color)

DAX is easy, CALCULATE makes DAX hard...

Helpful resources

Announcements
Join our Fabric User Panel

Join our Fabric User Panel

This is your chance to engage directly with the engineering team behind Fabric and Power BI. Share your experiences and shape the future.

June 2025 Power BI Update Carousel

Power BI Monthly Update - June 2025

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

June 2025 community update carousel

Fabric Community Update - June 2025

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

Top Solution Authors