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
moosepng
Helper II
Helper II

How do you FILTER by ID match with a temporary table

Hi,

Running into a bit of trouble with this one. I've posted about a similar issue before, so will link that at the bottom.

I want to create a measure (or column) that calculates the number of people where a condition is met, and where they have previously ever met a seperate condition. I also want to be able to group results by DateTime.

 

I have some dummy data 'Table'

DateTimeIDStatus_AEntryExit
25/10/2020 14:251001A  
29/10/2020 14:251001 10
1/12/2020 15:301002 10
5/02/2021 12:001001 01
15/04/2021 21:451002 01


So, in rough logic/code I want to

CALCULATE the DISTINCTCOUNT of ID where exit = 1 and where 'Table'[ID] = 'temp_table'ID (when temp_table = FILTER('Table', 'Table'[Status_A] = "A"))


I thought the simplest way to do this would be to use a temp_table VAR, similar to a CTE in SQL, adding the clause - if 'Table'[ID] = 'temp_table' ID when temp_table = FILTER('Table', 'Table'[Status_A] = "A")

IDStatus_A
1001A

 

So...

 

 

Measure =
VAR select_id = SELECTEDVALUE('Table'[ID])
VAR temp_table = FILTER('Table', 'Table'[Status_A] = "A")
RETURN
IF(
    CONTAINS(FILTER('Table', 'Table'[Status_A] = "A"), 'Table'[ID], ELECTEDVALUE('Table'[ID])),
    CALCULATE(
        DISTINCTCOUNT('Table'[ID]), 
        'Table'[Exit] = 1),
    BLANK()
)

 

 


My expected result is 1.
There is 1 ID that has previously had a Status_A = A and has EXIT = 1.
This one record has an ID 1001 and occured on the 5/02/2021 12:00.

My actual result is BLANK

As user Jihwan_Kim pointed out, I can get 1 by summing my measure:

 

 

SUMX(VALUES('Table'[ID]), [Measure])

 

 


But this is not able to be grouped by DateTime.

Previous post:
https://community.powerbi.com/t5/Desktop/DISTINCTCOUNT-of-ID-WHERE-a-previous-condition-is-TRUE/m-p/...

Similar Issues:
https://community.powerbi.com/t5/Desktop/DAX-query-to-compare-a-value-in-one-table-to-see-if-it-exis...
https://community.powerbi.com/t5/Desktop/INNER-JOIN/m-p/258597


1 ACCEPTED SOLUTION
jdbuchanan71
Super User
Super User

Yep, you can add a column to your table to assign the correct DateTime to each row for the ID like this.

Exit DateTime = 
CALCULATE (
    MAX ( 'Table'[Date] ),
    ALLEXCEPT ( 'Table', 'Table'[ID] ),
    'Table'[Exit] = 1
)

jdbuchanan71_0-1621610827392.png

Then you use that column to group the measure.

 

View solution in original post

10 REPLIES 10
jdbuchanan71
Super User
Super User

Yep, you can add a column to your table to assign the correct DateTime to each row for the ID like this.

Exit DateTime = 
CALCULATE (
    MAX ( 'Table'[Date] ),
    ALLEXCEPT ( 'Table', 'Table'[ID] ),
    'Table'[Exit] = 1
)

jdbuchanan71_0-1621610827392.png

Then you use that column to group the measure.

 

jdbuchanan71
Super User
Super User

OK, but your Status A and Exit rows have differnt datetime for the same ID.  How do you pick which one they should be grouped by?

DateTime ID Status_A Entry Exit
25/10/2020 14:25 1001 A    
29/10/2020 14:25 1001   1 0
5/2/2021 12:00 1001   0 1

Good point.
I want to know the number of IDs who exit (Exit = 1) who ever had a Status_A = A at the time of exit.
If you can help me articulate this it would be much appriciated 🙂

moosepng
Helper II
Helper II

Can't post the solution becuase of post flooding? i'll try a pic

moosepng_0-1621577014379.png

 

jdbuchanan71
Super User
Super User

@moosepng 

Not sure I am understanding what you are looking for but would something like this work?

Measure = 
VAR _Entry = CALCULATETABLE(VALUES('Table'[ID]),'Table'[Entry] = 1)
VAR _Status = CALCULATETABLE(VALUES('Table'[ID]),'Table'[Status] = "A")
VAR _Combine = INTERSECT(_Entry,_Status)
RETURN
COUNTROWS(_Combine)

jdbuchanan71_0-1621576264406.png

 

Looks like the measure is correct, but it can't be grouped by datetime

When you say grouped by datetime I'm not sure what you mean.  Can you explain?

Sorry. So the original table is by DateTime. My expected result is

moosepng_0-1621607782566.png


I want to know the Calculation (DISTICTCOUNT of ID) by Date (DateTime)

amitchandak
Super User
Super User

@moosepng , Try a measure like

 

calculate(distinctcount(Table[ID]), filter(Table, Table[ID] = calculate(max(Table[ID] ), filter(allselected(table), table[ID] = max(Table[ID]) && 'Table'[Status_A] = "A"))
&& Table[Exit] =1))

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

Works, but no total and can't be grouped by datetime

moosepng_0-1621576463133.png

 

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.