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

Get certified in Microsoft Fabric—for free! For a limited time, get a free DP-600 exam voucher to use by the end of 2024. Register now

Reply
Anonymous
Not applicable

how to write dax measure expression that depends on filters of related tables

I am trying to write a DAX measure expression to summarize a fact table value based on complex conditions in related dimension tables. The SQL I am trying to emulate looks like this:

 

SELECT SUM(WOF.EstimatedHours)/100
FROM WorkOrdersFact WO
inner join DimWorkOrderOverrides WOR on WO.DimWorkOrderOverridesID = WOR.DimWorkOrderOverridesID
inner join DimWorkOrderDocType WDT on WO.DimWorkOrderDocTypeID = WDT.DimWorkOrderDocTypeID
WHERE (WOR.Description2 IN ('307','308','302','311','390')) OR WDT.WorkOrderDocType = 'SV' and WO.WorkOrderType = 'X'

 

The following code represents the best I could come up with but it seems to me that there must be a better way. I am applying the OR conditions by creating two separate CALCULATE functions then adding the results before dividing by 100.

(
CALCULATE(SUM('Work Orders'[Estimated Hours]),
FILTER('Work Order Doc Type', 'Work Order Doc Type'[Work Order Doc Type] = "SV"),
FILTER('Work Orders', 'Work Orders'[Work Order Type] = "X"))
+
CALCULATE(SUM('Work Orders'[Estimated Hours]),
FILTER('Work Order Overrides', 'Work Order Overrides'[Work Order GL Description2] in {"307","308","302","311","390"}))
)
/100

 

note that the "IN" operator is now allowed but is not recognized by intellisense. Also note that the referenced tables are already joined in the PBI Relationships view.

 

Please give me your ideas on the optimal way to accomplish this.

1 ACCEPTED SOLUTION
MattAllington
Community Champion
Community Champion

Edit. Sorry, I just realise the or logic won't work So I deleted my suggestion. I think yours is pretty good



* Matt is an 8 times Microsoft MVP (Power BI) and author of the Power BI Book Supercharge Power BI.
I will not give you bad advice, even if you unknowingly ask for it.

View solution in original post

3 REPLIES 3
dmcguinn
Frequent Visitor

I have been search for ages to find the correct syntax to apply a dimension table filter to a fact table value during measure calculation...this just saved my weekend!!! Love this community

MattAllington
Community Champion
Community Champion

Edit. Sorry, I just realise the or logic won't work So I deleted my suggestion. I think yours is pretty good



* Matt is an 8 times Microsoft MVP (Power BI) and author of the Power BI Book Supercharge Power BI.
I will not give you bad advice, even if you unknowingly ask for it.

Hi @Anonymous,

Your DAX is good. The filter "WHERE (WOR.Description2 IN ('307','308','302','311','390'))" is for special values, so I am unable to formut it more intelligently after research. We must enumerate the values one by one.

 

Best Regards,
Angelia

Helpful resources

Announcements
November Carousel

Fabric Community Update - November 2024

Find out what's new and trending in the Fabric Community.

Live Sessions with Fabric DB

Be one of the first to start using Fabric Databases

Starting December 3, join live sessions with database experts and the Fabric product team to learn just how easy it is to get started.

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! Early Bird pricing ends December 9th.

Nov PBI Update Carousel

Power BI Monthly Update - November 2024

Check out the November 2024 Power BI update to learn about new features.