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

Compete to become Power BI Data Viz World Champion! First round ends August 18th. Get started.

Reply
AhsenMajid
Frequent Visitor

Distribution of one measure by another - pbix and dax.do code provided

I am using the Contoso db (dax.do).


There is a sales table, which is a fact table. 

There are also customer, product, and order dimension tables. 

I want to see the distribution of the count of distinct product keys by order number. Do most orders have just one product key? 5? etc. (sharing PBIX https://we.tl/t-ynvzHDkXIv and code at https://dax.do/yFMaeAbs5BRRWr/ - data is slightly different, problem and result is the same) 

 

This is the solution in SQL (copied the DB over using DAX Studio). 

AhsenMajid_0-1728798274168.png

This is what I get with DAX:

AhsenMajid_1-1728798875044.png

You can see the intermediate table is actually fine, and the number of orders with 7 distinct product keys is 45 (see row num on tabled, ordered dec.). This is exactly the same as what SQL give us. 

AhsenMajid_2-1728799298448.png

Question:

  1. How can I get the "no of sales orders" column correct, given I have the intermediate table correct?
  2. Once I've got the the correct result in a table expression, how do I get one in a visual?

Thanks!

 

DEFINE
MEASURE 'Sales'[Distinct Product Key Count] = DISTINCTCOUNT( 'Sales'[ProductKey] )

EVALUATE

VAR __tbl_count_product_keys_by_order_number = 
ADDCOLUMNS (
	DISTINCT ( 'Sales'[Order Number] ),
	"@Distinct Product Keys", [Distinct Product Key Count]
)

VAR __tbl_distinct_column_number_of_product_keys = 
	DISTINCT ( 
		SELECTCOLUMNS ( 
			__tbl_count_product_keys_by_order_number,
			"@No of Distinct Product Keys", [@Distinct Product Keys]
		)
	)
	
VAR result = 
ADDCOLUMNS(
	SUMMARIZE(
		__tbl_count_product_keys_by_order_number,
		[@Distinct Product Keys]
	),
	"no of sales orders", 
	CALCULATE( 
		COUNTROWS( __tbl_distinct_column_number_of_product_keys )
	)
)

// RETURN result ORDER BY [@Distinct Product Keys]

RETURN __tbl_count_product_keys_by_order_number ORDER BY [@Distinct Product Keys] DESC
1 ACCEPTED SOLUTION
ThxAlot
Super User
Super User

You're looking to calculate distribution of _count_product_keys_by_order_number, that's to say, _count_product_keys_by_order_number is a dimension; therefore it's no long a calculation on the fly, but a tangible (calculated) column.

 

First create a calculated table in table view as what you did in __tbl_count_product_keys_by_order_number; then put necessary column in a viz.

ThxAlot_0-1728842313183.png

 

ThxAlot_1-1728842446793.png



Expertise = List.Accumulate(


        {Days as from Today},


        {Skills and Knowledge},


        (Current, Everyday) => Current & Day.LearnAndPractise(Everyday)


)



View solution in original post

1 REPLY 1
ThxAlot
Super User
Super User

You're looking to calculate distribution of _count_product_keys_by_order_number, that's to say, _count_product_keys_by_order_number is a dimension; therefore it's no long a calculation on the fly, but a tangible (calculated) column.

 

First create a calculated table in table view as what you did in __tbl_count_product_keys_by_order_number; then put necessary column in a viz.

ThxAlot_0-1728842313183.png

 

ThxAlot_1-1728842446793.png



Expertise = List.Accumulate(


        {Days as from Today},


        {Skills and Knowledge},


        (Current, Everyday) => Current & Day.LearnAndPractise(Everyday)


)



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.