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

Next up in the FabCon + SQLCon recap series: The roadmap for Microsoft SQL and Maximizing Developer experiences in Fabric. All sessions are available on-demand after the live show. Register now

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
New to Fabric survey Carousel

New to Fabric Survey

If you have recently started exploring Fabric, we'd love to hear how it's going. Your feedback can help with product improvements.

Power BI DataViz World Championships carousel

Power BI DataViz World Championships - June 2026

A new Power BI DataViz World Championship is coming this June! Don't miss out on submitting your entry.

Join our Fabric User Panel

Join our Fabric User Panel

Share feedback directly with Fabric product managers, participate in targeted research studies and influence the Fabric roadmap.

March Power BI Update Carousel

Power BI Community Update - March 2026

Check out the March 2026 Power BI update to learn about new features.