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
sibis
New Member

DAX help: calculating the ratio of categorial variables

I have trouble counting the ratio of categorial variables in table over a certain time period.

 

Let's say I have sales data in a table called "purchases", and I want to calculate the ratio of returned goods. Each row in the table represents a sales transaction of a certain product to certain customer. There's a column called "status" which has a categorial variable (number) which tells the status of the purchase, a value of "3" meaning a returned purchase. I want to visualize the ratio of returns in all sales transactions over a time period, and be able to slice it by customers, item category, etc.

 

I'm able to count the number of rows with certain status, but not able to calculate the ratio. I tried two different DAX expressions, and they both result in the same incorrect result: all bars in the visualization say that the ratio is 1, but the bars appear otherwise. Any help is appreciated.

 

 

ratio = 
	CALCULATE(
		COUNT(purchases[purchase_id]),
		FILTER(purchases, purchases[status] = 3)
	)
	/
	CALCULATE(
		COUNT(purchases[purchase_id),
		FILTER(purchases, purchases[status] <= 4)
	)

 

 

ratio = 
SUMX(
purchases,
IF(purchases[status] = 3, 1, 0)
) /
SUMX(
purchases,
IF(purchases[status] = 1
|| purchases[status] = 2
|| purchases[status] = 3
|| purchases[status] = 4
, 1, 0
)
)

 

Capture.PNG



 

1 REPLY 1
richbenmintz
Solution Sage
Solution Sage

the following should work

metric=divide(calculate(countrows('table'), 'table'[status] = 3),calculate(countrows('table'), All('table'[status])). if you are able to share your pbix, that would be helpful 



I hope this helps,
Richard

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

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.

December 2024

A Year in Review - December 2024

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