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

Measure to calculate the distinct count of type from fact table using dim table

Hi all!

 

Can you help me to build a DAX measure, please?

 

So I have two tables.
The first one looks like this, it shows the participating organizations (columns from partner1 to partner5) by location and sales for each row.

yoleksandr_0-1693811512660.png

 

The second table is like this, it shows the type of each Organization. 

yoleksandr_1-1693811548560.png

 

I need to write a DAX measure, that will calculate the distinct count of type of organizations, so that then I can filter it by location.
NB: I need to keep the same structure of the first table, with no changes/pivots in PowerQuery.
 
The correct result that I calculated in Excel looks like this:
yoleksandr_2-1693811621436.png

 

Here I put the sample PBIX file:

I know it's easy if I create a separate calculated table, but I need a measure for this, not a new table.

Would be very grateful for any advice on this!
2 ACCEPTED SOLUTIONS
tamerj1
Super User
Super User

@yoleksandr and @DimaMD 

I don't have access to my laptop right now but you may try

=
COUNTROWS (
INTERSECT (
VALUES ( table2[Organization] ),
DISTINCT (
UNION (
VALUES ( table1[partner1] ),
VALUES ( table1[partner2] ),
VALUES ( table1[partner3] ),
VALUES ( table1[partner4] ),
VALUES ( table1[partner5] )
)
)
)
)

View solution in original post

@tamerj1 Hi, so this measure gave the correct result, @yoleksandr  use the measure provided @tamerj1 

DimaMD_0-1693980358541.png

 

 


__________________________________________

Thank you for your like and decision

__________________________________________

Greetings from Ukraine

To help me grow PayPal: embirddima@gmail.com

View solution in original post

12 REPLIES 12
tamerj1
Super User
Super User

@yoleksandr and @DimaMD 

I don't have access to my laptop right now but you may try

=
COUNTROWS (
INTERSECT (
VALUES ( table2[Organization] ),
DISTINCT (
UNION (
VALUES ( table1[partner1] ),
VALUES ( table1[partner2] ),
VALUES ( table1[partner3] ),
VALUES ( table1[partner4] ),
VALUES ( table1[partner5] )
)
)
)
)

thanks a lot! works great!

@tamerj1 Hi, so this measure gave the correct result, @yoleksandr  use the measure provided @tamerj1 

DimaMD_0-1693980358541.png

 

 


__________________________________________

Thank you for your like and decision

__________________________________________

Greetings from Ukraine

To help me grow PayPal: embirddima@gmail.com
yoleksandr
Frequent Visitor

UPDATE:
So I managed to create the unpivot with DAX with the following code:

sales_unpivot =
NATURALINNERJOIN(
TREATAS(
DISTINCT(FILTER(
UNION(
SELECTCOLUMNS(sales, "partner", sales[partner1], "location", sales[location]),
SELECTCOLUMNS(sales, "partner", sales[partner2], "location", sales[location]),
SELECTCOLUMNS(sales, "partner", sales[partner3], "location", sales[location]),
SELECTCOLUMNS(sales, "partner", sales[partner4], "location", sales[location]),
SELECTCOLUMNS(sales, "partner", sales[partner5], "location", sales[location])),
[partner] <> "")), dim_types[partner], sales[location]),dim_types)

It gives a table of this kind

yoleksandr_0-1693906060810.png

 

Now the question is whether it is possible to create a measure out of the above formula, this measure should calculate the disctinct count of organization by type. But I don't need to create a separate table, only a measure (the full dataset is 200K entries, so additional table would be redundant and slow the performance).

@yoleksandr  yes try it measure, only I see that the total counts incorrectly

 

 

OrganizationsCount = 
VAR VirtualTable =
            NATURALINNERJOIN(
TREATAS(
DISTINCT(FILTER(
UNION(
SELECTCOLUMNS(sales, "partner", sales[partner1], "location", sales[location]),
SELECTCOLUMNS(sales, "partner", sales[partner2], "location", sales[location]),
SELECTCOLUMNS(sales, "partner", sales[partner3], "location", sales[location]),
SELECTCOLUMNS(sales, "partner", sales[partner4], "location", sales[location]),
SELECTCOLUMNS(sales, "partner", sales[partner5], "location", sales[location])),
[partner] <> "")), dim_types[Organization], sales[location]),dim_types)
          
VAR FilteredTable =
    CALCULATE( COUNTROWS(VirtualTable), FILTER(VirtualTable, [Organization] = SELECTEDVALUE(dim_types[Organization])))
RETURN
    FilteredTable
        
    

 

DimaMD_0-1693908341108.png

 


__________________________________________

Thank you for your like and decision

__________________________________________

Greetings from Ukraine

To help me grow PayPal: embirddima@gmail.com

hi @DimaMD ,
thanks a lot for this, I see we're really close to the correct solution, but not there yet.

The issue is that I need to have DISTINCTCOUNT of [partner] column from the virtual table, but the below variant of the FilteredTable is not working:

VAR FilteredTable =
    CALCULATE( DISTINCTCOUNT(VirtualTable[partner]), FILTER(VirtualTable, [Organization] = SELECTEDVALUE(dim_types[Organization])))

Do you know how to make it calculate the correct total?
DimaMD
Solution Sage
Solution Sage

@tamerj1 Hello , maybe you have an idea how to calculate it?


__________________________________________

Thank you for your like and decision

__________________________________________

Greetings from Ukraine

To help me grow PayPal: embirddima@gmail.com
DimaMD
Solution Sage
Solution Sage

@yoleksandr  hi, look at the attached file and answer if this is the desired result

DimaMD_0-1693815510984.png

 


__________________________________________

Thank you for your like and decision

__________________________________________

Greetings from Ukraine

To help me grow PayPal: embirddima@gmail.com

hi @DimaMD ,

Thanks a lot for this, but it's not exactly what I need.
Sorry if it wasn't clear initially, I added one line to the fist message now.

So I need to keep the structure of the table, it should not be changed. You unpivoted the columns with partners, but with this the data in "sales" column is now repeated many times, so it gives the wrong result. Hope what I mean is clear.
in any case, Slava Ukraini!

i think you can add a concatinating column where you concat values and then , see if it contains in the column teh value

eliasayyy
Memorable Member
Memorable Member

hello first pivot your first table for all 5 partners this way , you will have 2 columns , partners and organization column , 

next create 2 dim tabes one with distinct values of organizatio nand one with distinct values of partners

now link the tbales via one to many relationship 

Thanks, but I need to keep the same structure of the table, no unpivots (sales column will give a wrong result in this case).

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.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.