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

Don't miss out! 2025 Microsoft Fabric Community Conference, March 31 - April 2, Las Vegas, Nevada. Use code MSCUST for a $150 discount. Prices go up February 11th. Register now.

Reply
criztina14
Frequent Visitor

Distinct count on dynamic query using unrelated dimension tables

I was asked to compare the number of distinct clients in the current year vs lifetime.
We're still discussing if this is really needed but I'm not sure if this would be actually possible to get.

 

Main Table - Current year data

StateIDCityIDProductIDStoreIDDateCustomerIDAmount
       


Lifetime Table

StateIDDistinctCountCustomers
  

 

For example, getting the #distinct customers by State, having products A, G, Z and stores 1, 2 selected in slicers is easy to get for the current year, but for the lifetime distinct count, I was just thinking it would have to be a direct query as there may be different combinations of values selected in the slicers, but I can't see how to pass the slicer values as parameters for the dinamyc query if this table won't have a relationship with those dimensions?

 

Any help would be appreciated. Thank you

1 ACCEPTED SOLUTION
darkniqht
Advocate I
Advocate I

You can easily get the distinct count of customers for the current year using a measure like this:

 

DistinctCustomersCurrentYear =
DISTINCTCOUNT(MainTable[CustomerID])

 

For the lifetime distinct count, since you’re dealing with unrelated dimension tables, you can use a combination of DAX and context. Try creating a measure that calculates the distinct count while ignoring the slicers for the lifetime table:

 

DistinctCustomersLifetime =
CALCULATE(
    DISTINCTCOUNT(LifetimeTable[CustomerID]),
    ALL(MainTable)  // Ignores current year filters
)

 

To filter the lifetime data based on slicers, you can create a measure that checks for the selected products or stores from the slicers and then uses that context to filter the lifetime data:

 

DistinctCustomersLifetimeFiltered =
CALCULATE(
    DISTINCTCOUNT(LifetimeTable[CustomerID]),
    FILTER(
        LifetimeTable,
        LifetimeTable[StateID] IN VALUES(MainTable[StateID]) // Adjust as necessary
    )
)

 

If you’re using DirectQuery, you may be limited in how you pass slicer values. You might need to look into using stored procedures or creating a SQL view that combines these tables dynamically.

 

Finally, create visuals that show both measures side by side for comparison. This will help illustrate the differences between the distinct customer counts.

View solution in original post

2 REPLIES 2
darkniqht
Advocate I
Advocate I

You can easily get the distinct count of customers for the current year using a measure like this:

 

DistinctCustomersCurrentYear =
DISTINCTCOUNT(MainTable[CustomerID])

 

For the lifetime distinct count, since you’re dealing with unrelated dimension tables, you can use a combination of DAX and context. Try creating a measure that calculates the distinct count while ignoring the slicers for the lifetime table:

 

DistinctCustomersLifetime =
CALCULATE(
    DISTINCTCOUNT(LifetimeTable[CustomerID]),
    ALL(MainTable)  // Ignores current year filters
)

 

To filter the lifetime data based on slicers, you can create a measure that checks for the selected products or stores from the slicers and then uses that context to filter the lifetime data:

 

DistinctCustomersLifetimeFiltered =
CALCULATE(
    DISTINCTCOUNT(LifetimeTable[CustomerID]),
    FILTER(
        LifetimeTable,
        LifetimeTable[StateID] IN VALUES(MainTable[StateID]) // Adjust as necessary
    )
)

 

If you’re using DirectQuery, you may be limited in how you pass slicer values. You might need to look into using stored procedures or creating a SQL view that combines these tables dynamically.

 

Finally, create visuals that show both measures side by side for comparison. This will help illustrate the differences between the distinct customer counts.

rajendraongole1
Super User
Super User

Hi @criztina14 - To achieve the comparison of distinct clients between the current year and lifetime, you can use a combination of measures and DAX formulas in Power BI.

Create a measure that counts distinct customers in the current year based on slicer selections.

DistinctCurrentYearCustomers =
CALCULATE(
DISTINCTCOUNT(MainTable[CustomerID]),
FILTER(
MainTable,
YEAR(MainTable[Date]) = YEAR(TODAY())
)
)

 

Create a measure for counting distinct customers from the lifetime table.

DistinctLifetimeCustomers =
DISTINCTCOUNT(LifetimeTable[DistinctCountCustomers])

 

Now. , cate a new table that contains the unique values for slicers (e.g., State, City, Product, etc.).

For example, if you're creating a table for states

StateSlicer = VALUES(MainTable[StateID])

 

calculates the distinct count of lifetime customers while considering the slicer selections.

DistinctLifetimeCustomersFiltered =
CALCULATE(
DISTINCTCOUNT(LifetimeTable[DistinctCountCustomers]),
LifetimeTable[StateID] IN VALUES(StateSlicer[StateID])
)

 

You can now create a card visual or a table visual to display both measures:

Current Year Distinct Customers: Use the DistinctCurrentYearCustomers measure.
Lifetime Distinct Customers: Use the DistinctLifetimeCustomersFiltered measure.

 

Hope the above measure helps in your scenerio.





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

Proud to be a Super User!





Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Jan25PBI_Carousel

Power BI Monthly Update - January 2025

Check out the January 2025 Power BI update to learn about new features in Reporting, Modeling, and Data Connectivity.

Jan NL Carousel

Fabric Community Update - January 2025

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