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

Calculate using OR from Dimensions

I have a fairly simple model with 1 Fact table and 2 Dimensions. 

Fact Table Sales:

       SalesID, ProductID, StoreID

ProductDim

       ProductID, ProductName

StoreDim

       StoreID, StoreName

 

What I need to do is write a dax that will calculate the number of distinct SaleIDs, when either dimension is selected. e.g. If I seled ProductName="Gym Paints" and StoreName="Walmart", I'll get the count of SalesIDs that contain either Gym Paints ProductName or Walmart StoreName. 

 

I have so far tried:

SalesTotal := 

     CALCULATE(DISTINCTCOUNT(Sales[SalesId])

      -- various forms of FILTER, but none have worked. 

)

 

I know this is not ideal solution, we are forced into this because other solutions have been performing slowly. 

 

Thanks!

1 ACCEPTED SOLUTION

Sorry, I missunderstood. You can’t do what you want if you have 2 active relationships. Relationships from 2 dim tables are logical AND. So either remove the relationships or make them inactive. Then write something like

 

measure =
VAR prod =
    MAX ( prod[id] )
VAR Store =
    MAX ( store[ID] )
VAR prodsales =
    FILTER ( sales, sales[prod id] = prod )
VAR Storesales =
    FILTER ( sales, sales[store id] = store )
VAR allsales =
    UNION ( prodsales, storesales ) // includes 2 copies of rows that are both 
VAR alldistinctsales =
    DISTINCT ( allsales ) // removes double count of rows that are both
RETURN
    CALCULATE ( [total sales], Alldistinctsales)

I think this will work. I haven’t tested it.  



* Matt is an 8 times Microsoft MVP (Power BI) and author of the Power BI Book Supercharge Power BI.

View solution in original post

3 REPLIES 3

You don’t need calculate. Just write this

 

DISTINCTCOUNT(Sales[SalesId])



* Matt is an 8 times Microsoft MVP (Power BI) and author of the Power BI Book Supercharge Power BI.

Thanks @MattAllington , 

Note I want to apply two dimensions to the calculation though.  If I simply do: 

 

measure := distinctcount(Sales[SaleId]) and apply two dimension filters, it will count only rows where the two dimension rows are applied. 

 

I want to acheive an OR condition where either of the two dimensions are applied. I.e. distinctcount of salesid when ProductA OR storeB is selected in the dimensions. 

Thanks! 

Sorry, I missunderstood. You can’t do what you want if you have 2 active relationships. Relationships from 2 dim tables are logical AND. So either remove the relationships or make them inactive. Then write something like

 

measure =
VAR prod =
    MAX ( prod[id] )
VAR Store =
    MAX ( store[ID] )
VAR prodsales =
    FILTER ( sales, sales[prod id] = prod )
VAR Storesales =
    FILTER ( sales, sales[store id] = store )
VAR allsales =
    UNION ( prodsales, storesales ) // includes 2 copies of rows that are both 
VAR alldistinctsales =
    DISTINCT ( allsales ) // removes double count of rows that are both
RETURN
    CALCULATE ( [total sales], Alldistinctsales)

I think this will work. I haven’t tested it.  



* Matt is an 8 times Microsoft MVP (Power BI) and author of the Power BI Book Supercharge Power BI.

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.