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

Score big with last-minute savings on the final tickets to FabCon Vienna. Secure your discount

Reply
robjw
New Member

Help getting the distinct counts for a customer by category by only the latest transaction date /cat

I have been trying to do the following and it seems like it should be really simple but I can't get it to work. I have a table of data that is millions of rows long but is essentially this: 

 

customerIDCategoryDate
1a25/12/2022
1a26/12/2022
1a27/12/2022
1a28/12/2022
2a25/12/2022
2b26/12/2022
2b27/12/2022
2a28/12/2022
3c25/12/2022
3b26/12/2022
3a27/12/2022
3a28/12/2022

 

I am trying to get four types of counts across it, represented in one table like below. I am trying to use measures rather than calculated columns to get there as the count needs to be dynamic across date range (the user can filter the date range to what they want, so the maximum dates etc need to change):

 

CategoryCountDistinct Customer CountMaxDate Customer CountMaxCategory Customer Count
a8331
b3201
c1101

 

The first two counts are fine, but I need help writing DAX formula for:

 

MaxDate Customer Count: This should count the customer ONLY ONCE in the category that has the max date in the date range selected. As per my example date set, all customers would appear in category A.

 

MaxCategory Customer Count: This should count the customer ONLY ONCE in the category that was the Max they reached in the date range selected (in actual date the category is a number, so max will work, but thought it might confuse things here)

 

All the things I've found and try on internet either only return the latest date for the whole data set, or ignore the requirement to only count the customer once across all categories.

 

Thank you in advance!

1 ACCEPTED SOLUTION

@robjw,

Yes I understand what you are saying. To achieve this we need to process the customer id in the measure. I added the customer id as a row context within the measure:

MaxDate Customer Count =
COUNTROWS (
    FILTER (
        DISTINCT ( 'Table'[customerID] ),
        VAR _customer = 'Table'[customerID]
        VAR _max_date =
            CALCULATE (
                MAX ( 'Table'[Date] ),
                REMOVEFILTERS ( 'Table' ),
                FILTERS ( 'Table'[Date] ),
                'Table'[customerID] = _customer
            )
        VAR _category =
            CALCULATETABLE (
                VALUES ( 'Table'[Category] ),
                REMOVEFILTERS ( 'Table' ),
                'Table'[customerID] = _customer,
                'Table'[Date] = _max_date
            )
        VAR _result =
            SELECTEDVALUE ( 'Table'[Category] ) IN _category
        RETURN
            _result
    )
)

Any date selection of a user is still taken into account.

The MaxCategory Customer Count works in a comparative manner, but then based on the maximum category. Here, too, any date selections are taken into account.

MaxCategory Customer Count =
COUNTROWS (
    FILTER (
        DISTINCT ( 'Table'[customerID] ),
        VAR _customer = 'Table'[customerID]
        VAR _max_category =
            CALCULATE (
                MAX ( 'Table'[Category] ),
                REMOVEFILTERS ( 'Table' ),
                FILTERS ( 'Table'[Date] ),
                'Table'[customerID] = _customer
            )
        VAR _result =
            SELECTEDVALUE ( 'Table'[Category] ) = _max_category
        RETURN
            _result
    )
)

View solution in original post

5 REPLIES 5
Barthel
Solution Sage
Solution Sage

Hey @robjw,

You can use the code below to calculate the MaxDate Customer Count.

 

MaxCategory Customer Count =
VAR _max_date =
    CALCULATE (
        MAX ( 'Table'[Date] ),
        REMOVEFILTERS ( 'Table' ),
        FILTERS ( 'Table'[Date] )
    )
VAR _result =
    CALCULATE ( DISTINCTCOUNT ( 'Table'[customerID] ), 'Table'[Date] = _max_date )
RETURN
    _result

 

First you calculate the max date taking into account any user date filters. You then calculate the number of unique customers per category based on this.

I don't quite understand the definition of the second measure yet (MaxCategory Customer Count). Could you explain a bit more?

Hi @Barthel thank you very much for coming back to me.

 

The issue with the maxdate calculation is I think it is only taking the max date across the table, rather than per customer. I have tried to make this more obvious with the below addition to the sample data - customer 4 doesn't have a transaction on the max date overall, but I still want them counted under category B:

 

customerIDCategoryDate
1a25/12/2022
1a26/12/2022
1a27/12/2022
1a28/12/2022
2a25/12/2022
2b26/12/2022
2b27/12/2022
2a28/12/2022
3c25/12/2022
3b26/12/2022
3a27/12/2022
3a28/12/2022
4a25/12/2022
4b26/12/2022
4b27/12/2022

 

Should give: 

 

CategoryCountDistinct Customer CountMaxDate Customer CountMaxCategory Customer Count
a9431
b5312
c1101

 

With the formula you provided, the count is 0/blank in category B, rather than showing 1 - if that makes sense?

 

Regarding MaxCategory Customer count - over time customers can move between category a,b and c. I want to find the max category they were in during the selected time period, counting them only once in that category.  E.g. if they have one transaction in category C, I count them there as the 'max' category they hit during the timeframe.

 

Hope that makes sense.

@robjw,

Yes I understand what you are saying. To achieve this we need to process the customer id in the measure. I added the customer id as a row context within the measure:

MaxDate Customer Count =
COUNTROWS (
    FILTER (
        DISTINCT ( 'Table'[customerID] ),
        VAR _customer = 'Table'[customerID]
        VAR _max_date =
            CALCULATE (
                MAX ( 'Table'[Date] ),
                REMOVEFILTERS ( 'Table' ),
                FILTERS ( 'Table'[Date] ),
                'Table'[customerID] = _customer
            )
        VAR _category =
            CALCULATETABLE (
                VALUES ( 'Table'[Category] ),
                REMOVEFILTERS ( 'Table' ),
                'Table'[customerID] = _customer,
                'Table'[Date] = _max_date
            )
        VAR _result =
            SELECTEDVALUE ( 'Table'[Category] ) IN _category
        RETURN
            _result
    )
)

Any date selection of a user is still taken into account.

The MaxCategory Customer Count works in a comparative manner, but then based on the maximum category. Here, too, any date selections are taken into account.

MaxCategory Customer Count =
COUNTROWS (
    FILTER (
        DISTINCT ( 'Table'[customerID] ),
        VAR _customer = 'Table'[customerID]
        VAR _max_category =
            CALCULATE (
                MAX ( 'Table'[Category] ),
                REMOVEFILTERS ( 'Table' ),
                FILTERS ( 'Table'[Date] ),
                'Table'[customerID] = _customer
            )
        VAR _result =
            SELECTEDVALUE ( 'Table'[Category] ) = _max_category
        RETURN
            _result
    )
)

@Barthel You are officially a genius! This has worked perfectly and I don't think I would have ever got to that answer myself so thank you very much.

amitchandak
Super User
Super User

@robjw , Based on what I got, see if this can help

https://amitchandak.medium.com/power-bi-get-the-sum-of-the-last-latest-value-of-a-category-f1c839ee8...

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

Helpful resources

Announcements
August Power BI Update Carousel

Power BI Monthly Update - August 2025

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

August 2025 community update carousel

Fabric Community Update - August 2025

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