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

July 28 - August 9 | Final Round of the Power BI Dataviz World Championships. This is your chance. Learn more

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
FabCon and SQLCon Barcelona 2026

FabCon & SQLCon – Barcelona 2026

Join us in Barcelona for FabCon and SQLCon, the Fabric, Power BI, SQL, and AI community event. Save €200 with code FABCMTY200.

Fabric Community Sticker Design Challenge Barcelona Carousel

Fabric Community Sticker Challenge - Barcelona 2026

If you love stickers, then you will definitely want to check out our community sticker challenge, Barcelona edition!

July Power BI Update Carousel

Power BI Monthly Update - July 2026

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

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.

Top Solution Authors