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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
johnsmith92
Regular Visitor

Translate SQL WHERE clauses to a query

Hi all! I have a very simple model with 2 tables - one fact with cases and history and one date dimension in SSAS. I need a measure to count open cases at the start of each contextualized period. So for example:

DateOpen cases start of day
01-01-202113400
02-02-202113230
03-02-202113300

 

Now, I can achieve this PER day in MSSQL with the following query:

 

 

DECLARE @FilterDate AS DATE;
SET @FilterDate = '2021-01-01'

Select COUNT(*)
FROM FactTable
WHERE TaskCompleted IS NULL
AND IsDeleted = 0
AND DW_ValidFrom < @FilterDate
AND DW_ValidTo >= @FilterDate

 

In SQL I can create a function that reiterates all of the dates needed for my date variable so I would get the table above. In my tabular model I have a 1:* relationship from DimDate to FactCases on DateKey to DW_ValidFrom

 

Could someone help me translate this seemingly easy query to a DAX statement?

1 ACCEPTED SOLUTION

@johnsmith92 , if you do not have an active join between two tables then you do not need a crossfilter. Make both join inactive

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

View solution in original post

8 REPLIES 8
v-kelly-msft
Community Support
Community Support

Hi  @johnsmith92 ,

 

Try:

Count=
var _date=IF(SELECTEDVALUE('Date'[date])=BLANK(),DATE(2021,1,1),SELECTEDVALUE('Date'[date]))
Return
CALCULATE(COUNTROWS('FactTable'),FILTER(ALL('FactTable'),'FactTable'[TaskCompleted]=BLANK()&&'FactTable'[IsDeleted]=0&& 'FactTable'[DW_ValidFrom]<_date && 'FactTable'[DW_ValidTo]>=_date))

 

Best Regards,
Kelly

Did I answer your question? Mark my reply as a solution!

amitchandak
Super User
Super User

@johnsmith92 , check if the current employee measure of HR blog can help

https://community.powerbi.com/t5/Community-Blog/HR-Analytics-Active-Employee-Hire-and-Termination-tr...

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

Hi @amitchandak. Thanks for your prompt reply! Unfortunately we are running SQL Server 2014, so I cannot use the CROSSFILTER function. Is there another way to write it?

@johnsmith92 , if you do not have an active join between two tables then you do not need a crossfilter. Make both join inactive

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

I did as you suggested, but now I'm getting some wild results that are not expected.

Hi  @johnsmith92 ,

 

Is your issue solved now?

 

Best Regards,
Kelly

Did I answer your question? Mark my reply as a solution!

Yes, using inactive relations between tables does the trick. 🙂

Hi @johnsmith92 ,

 

Great!

 

Best Regards,
Kelly

Did I answer your question? Mark my reply as a solution!

 

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!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

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