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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
Anonymous
Not applicable

Count if someone entered but not left

Good morning,

 

I have a database that registers people access in a building, it has more info but the one's I'm looking for are:

Person's IDDirectionDate an time
1in16/10 10:00:00
2in16/10 11:00:00
1out16/10 11:00:00
3in16/10 12:00:00
4in16/10 13:00:00

 

And I want a formula ta count the people that are still IN the building in the current time. So if I looked at this dashboard at 16/10 14:00:00, I want it to show me that there are 3 people inside (ID 2, 3 and 4)
How can I do that?

1 ACCEPTED SOLUTION

Please try this expression that should be responsive to your building, etc. slicers.

 

People In =
VAR thisdatetime =
    MIN ( InOut[Date an time] )
VAR summary =
    ADDCOLUMNS (
        ALLSELECTED ( InOut[Person's ID] ),
        "@ins",
            CALCULATE (
                COUNTROWS ( InOut ),
                InOut[Direction] = "in",
                ALLSELECTED ( InOut[Date an time] ),
                InOut[Date an time] <= thisdatetime
            ),
        "@outs",
            CALCULATE (
                COUNTROWS ( InOut ),
                InOut[Direction] = "out",
                ALLSELECTED ( InOut[Date an time] ),
                InOut[Date an time] <= thisdatetime
            )
    )
RETURN
    COUNTROWS (
        FILTER (
            summary,
            [@ins] > [@outs]
        )
    )

 

Regards,

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


View solution in original post

9 REPLIES 9
amitchandak
Super User
Super User

@Anonymous , try a new column

new column =
var _in = countx(filter(Table,able[Persons ID] =earlier([Persons ID]) && [Direction] ="in"),[Persons ID])
var _out = countx(filter(Table,Table[Persons ID] =earlier([Persons ID]) && [Direction] ="in"),[Persons ID])
return
if(_in >_out , "Still There", "Out")

 

or new measure with person id

new measure =
var _in = countx(filter(allselected(Table),Table[Persons ID] =max([Persons ID]) && [Direction] ="in"),[Persons ID])
var _out = countx(filter(allselected(Table),Table[Persons ID] =max([Persons ID]) && [Direction] ="in"),[Persons ID])
return
if(_in >_out , 1, blank() )

Anonymous
Not applicable

@amitchandak  thank you! That's somewhat what I was looking for. But if I want it to be filtered by date and the building the user is on? Is it possible?

Hi @Anonymous ,

 

You can use the following dax:

 

 

Measure =
COUNTAX (
    FILTER (
        SUMMARIZE (
            'Table',
            'Table'[Person's ID],
            "still IN",
                VAR a =
                    CALCULATE (
                        MAX ( 'Table'[Date an time] ),
                        FILTER ( 'Table', 'Table'[Date an time] <= NOW () )
                    )
                VAR b =
                    CALCULATE ( MAX ( 'Table'[Direction] ), 'Table'[Date an time] = a )
                RETURN
                    IF ( b = "in", 1, 0 )
        ),
        [still IN] = 1
    ),
    [Person's ID]
)

 

 

Capture9.PNGCapture10.PNG

 

If this post helps, then please consider Accept it as the solution to help the other members find it more quickly.

 

Best Regards,

Dedmon Dai

Please try this expression that should be responsive to your building, etc. slicers.

 

People In =
VAR thisdatetime =
    MIN ( InOut[Date an time] )
VAR summary =
    ADDCOLUMNS (
        ALLSELECTED ( InOut[Person's ID] ),
        "@ins",
            CALCULATE (
                COUNTROWS ( InOut ),
                InOut[Direction] = "in",
                ALLSELECTED ( InOut[Date an time] ),
                InOut[Date an time] <= thisdatetime
            ),
        "@outs",
            CALCULATE (
                COUNTROWS ( InOut ),
                InOut[Direction] = "out",
                ALLSELECTED ( InOut[Date an time] ),
                InOut[Date an time] <= thisdatetime
            )
    )
RETURN
    COUNTROWS (
        FILTER (
            summary,
            [@ins] > [@outs]
        )
    )

 

Regards,

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Anonymous
Not applicable

Thanks @mahoneypat it worked perfectly!!

 

If I want to compare the measure People In with the values of another table, how can I do that?

 

I have the people limit of each building in another table, and I want to show which building are "occupied" or "exceeded" by comparing People In and the max of each building

You can add another variabile to the expression that calculates your max level, also turn the Return with Countrows also into a variable, and then make a new return that divides your People variable by your max level variable.

Regards,

Pat

 





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Anonymous
Not applicable

@mahoneypat  I'm sorry, I didn't understand.

 

Let me explain a little better.

 

I have this table that I showed you

Person's IDDirectionDate an time

 that also has a column called Building ID

 

And then I have another table with the columns:

Building IDBuilding NameBuilding Max Level

 

The Measure People In already helped me a lot to show the status of a building in a time.

And I want to put another visual that shows all the building that are Occupied (People In = Building Max Level), Over occupied (People In > Building Max Level), and Free (People In < Building Max Level), but when I want try to make this measure, it says I can't bc Building Max Level is in another table, different from People In

 

If I understand your model correctly, you should be able to make a table visual with the building column from the 2nd table (had max level column).  You can then aggregate the max level column in a measure (average, min, max shouldn't matter if a single value for max), and then add the People measure to it.

 

You could also make it a matrix and put Date, Time, etc. as the columns to see how the occupancy changes over time.  Or you can make a measure that give you the % occupied (my previous suggestion). 

% Occupied = var maxlevel = [Max Level Measure]

var people = [People Measure]

return DIVIDE(people, maxlevel)

 

Regards,

Pat





Did I answer your question? Mark my post as a solution! Kudos are also appreciated!

To learn more about Power BI, follow me on Twitter or subscribe on YouTube.


@mahoneypa HoosierBI on YouTube


Anonymous
Not applicable

It worked! Thank you!

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

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

April Fabric Community Update

Fabric Community Update - April 2024

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