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

A new Data Days event is coming soon! This time we’re going bigger than ever. Fabric, Power BI, SQL, AI and more. Don't miss out.

Reply
Kbdm
New Member

DirectQuery filter table based on condition

I want to be able to create a filter on this table to get all rows with Type Z with no entry Type X for the same location within 1 hour before the Datetime of a Type Z.

 

So after filtering below table the result should only show Row 4(there is no entry with Type x within 1 hour before the Datetime)

 

Row #DatetimeLocationType
112-11-2020 13:15AX
212-11-2020 11:15AX
312-11-2020 12:00AZ
412-11-2020 11:00AZ
1 ACCEPTED SOLUTION
v-kelly-msft
Community Support
Community Support

Hi  @Kbdm ,

 

Create a measure as below:

_Datetime = 
var _previousX=CALCULATE(MAX('Table'[Datetime]),FILTER(ALL('Table'),'Table'[Datetime]<MAX('Table'[Datetime])&&'Table'[Type]="X"&&'Table'[Location]=MAX('Table'[Location])))+0
Return
IF(MAX('Table'[Type])="Z"&&DATEDIFF(_previousX,MAX('Table'[Datetime]),HOUR)>1,MAX('Table'[Datetime]),BLANK())

And you will see:

Screenshot 2020-11-18 152351.png

For the related .pbix file,pls see attached.

 

Best Regards,
Kelly

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

View solution in original post

3 REPLIES 3
v-kelly-msft
Community Support
Community Support

Hi  @Kbdm ,

 

Create a measure as below:

_Datetime = 
var _previousX=CALCULATE(MAX('Table'[Datetime]),FILTER(ALL('Table'),'Table'[Datetime]<MAX('Table'[Datetime])&&'Table'[Type]="X"&&'Table'[Location]=MAX('Table'[Location])))+0
Return
IF(MAX('Table'[Type])="Z"&&DATEDIFF(_previousX,MAX('Table'[Datetime]),HOUR)>1,MAX('Table'[Datetime]),BLANK())

And you will see:

Screenshot 2020-11-18 152351.png

For the related .pbix file,pls see attached.

 

Best Regards,
Kelly

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

amitchandak
Super User
Super User

@Kbdm , Try a measure like

measure =
if(isblank(calculate(countrows(Table), filter(All(Table), table[Datetime] < max(table[Datetime]) && table[Datetime] >= max(table[Datetime])-time(1,0,0)))),1,0)

Share with Power BI Enthusiasts: Full Power BI Video (20 Hours) YouTube
Microsoft Fabric Series 60+ Videos YouTube
Microsoft Fabric Hindi End to End YouTube

@amitchandak Thank you for the answer so far, this covers the time within 1 hour requirement. I also need to be able to make sure that the locations in the resulting rows are the same and check the type combination Type Z should not have a Type X that has taken place earlier , however I am unable to use the EARLIER function for this. Any suggestion How I would go about this?

Helpful resources

Announcements
May Power BI Update Carousel

Power BI Monthly Update - May 2026

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

Fabric SQL PBI Data Days

Data Days 2026 coming soon!

Sign up to receive a private message when registration opens and key events begin.

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.