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
Anonymous
Not applicable

Help to Calculate Distinct Count for each Store

Hello Guys,

Could you please help me to create measure for the below requirement:

CustomerShopping DateStore
Aiden1-OctStore_1
Aiden2-OctStore_2
Zachary3-OctStore_1
Zachary5-OctStore_2
Christian1-OctStore_1
Christian2-OctStore_2
Christian3-OctStore_3
Amey10-OctStore_1

 

I have one fact table, 3 dimensions ( Date, Customer and Store). and below is expected result.

Filter : 1st Oct to 5th Oct
StoreDistinct Customer Count
Store_10
Store_22
Store_31

Basically I want to calculate Distinct Customer for Store . Customer visits different-2 stores over the time, requirement is to count Customer once where he/she has visited in last. For example, Aiden visited two different stores (Store 1 and Store 2 ) on 1st and 2nd October so here Customer should not be calculated for Store_1 as Customer has visited Store_2 also on next day means Customer should be calculated for the store where Customer has visited in last only.

Please note, solution should respect filter context as well. Like, in expected result Amey Customer is not calculated anywhere as it is not within the date range selected on report.

 

Thanks

4 REPLIES 4
Anonymous
Not applicable

Please note that the model is not correct. A good, professional model is always dimensional (star-schema). However, here is a formula in this bad model that should work. If not, then please adjust it accordingly.

 

 

[# Distinct Customers] =
// For each customer
// find the last store
// they shopped in. If
// in more than 1 store
// on the same day,
// assume the store
// that's last alphabetically.
var __visibleStores =
    ALLSELECTED( T[Store] )
var __custsWithLastStore =
    addcolumns(
        distinct( T[Customer] ),
        "@LastStore",
            maxx(
                topn(1,
                    CALCULATETABLE(
                        T,
                        __visibleStores
                    ),
                    T[Shopping Date],
                    desc
                ),
                T[Store]
            )
    )
var __count =
    COUNTROWS( 
        filter(
            __custsWithLastStore,
            [@LastStore] in distinct( T[Store] )
        )
    )
return
    __count

 

Anonymous
Not applicable

@Anonymous : Thank you for quick reply. My model is correct and in star scheme.

Could you please provide DAX again as per model below.

 

fact_shopping   
customer_iddate_idstore_idamount
500120201001125
500120201002240
500220201005112
500220201002296
500320201001178
50032020100228
500320201003371
500420201010169

 

dim_store 
store_idstore_name
1Store_1
2Store_2
3Store_3

 

dim_customer 
customer_idcustomer_name
5001Aiden
5002Zachary
5003Christian
5004Amey
Anonymous
Not applicable

Please, next time when you post a question, do give people the data and model you really have and not some versions that then will require people to change their formulas. It's a waste of everybody's time. Thanks.

 

[# Distinct Customers] =
var __visibleStoresIds =
    CALCULATETABLE(
        distinct( dim_store[store_id] ),
        ALLSELECTED( dim_store )
    )
var __custsWithLastStore =
    addcolumns(
        distinct( dim_customer[customer_id] ),
        "@LastStoreId",
            // if there is more than 1 store
            // on the particular date, the
            // store with the highest store_id
            // is selected
            maxx(
                topn(1,
                    CALCULATETABLE(
                        fact_shopping,
                        __visibleStoresIds,
                        all( dim_store )
                    ),
                    fact_shopping[date_id],
                    desc
                ),
                fact_shopping[store_id]
            )
    )
var __count =
    COUNTROWS(
        filter(
            __custsWithLastStore,
            [@LastStoreId] in distinct( dim_store[store_id] )
        )
    )
return
    __count

 

amitchandak
Super User
Super User

@Anonymous , You can create a rank column and count only rank 1

rankx(filter(table,[Customer] =earlier([Customer])),[Shopping],,asc,dense)

 

For Rank Refer these links
https://radacad.com/how-to-use-rankx-in-dax-part-1-of-3-calculated-columns
https://radacad.com/how-to-use-rankx-in-dax-part-2-of-3-calculated-measures

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.