cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
jojo54 Frequent Visitor
Frequent Visitor

Start and end time

Hi there, pretty new to Power BI.  I'm trying to replicate something I do in Excel in Power BI, mainly because excel can't handle the amount of data that's being processed.

 

I'm trying to figure out how full a cafe is in different parts of the day.  I have the following data about each customer and what time they arrive and leave the cafe.

 

NameEntry TimeExit Time
Person 16:009:00
Person 27:008:30
Person 38:009:15
Person 46:4510:00
Person 57:157:45

 

And I want to see how many people are actually in the cafe at any one point in time, so like the below

 

 6:006:156:306:457:007:157:307:458:00
Person 1                1                1                1                1                1                1                1                1                1
Person 2                    1                1                1                1                1
Person 3         
Person 4                   1                1                1                1                1                1
Person 5                     1                1                1 
Total                1                1                1                2                3                4                4                4                3

 

I can definitely do it in excel but I can't figure out how to get it done in Power BI! Any help would be great!

1 ACCEPTED SOLUTION

Accepted Solutions
Geradav Regular Visitor
Regular Visitor

Re: Start and end time

Hi @jojo54 

 

See if the following can fix it for what you need

Number of clients 2 =
VAR vMinVal =
    MIN ( TimeTable[Value] )
VAR vMaxVal =
    MAX ( TimeTable[Value] )
VAR vNumberOfClients =
    COUNTROWS (
        FILTER (
            ALLSELECTED ( ClientsTbl ),
            ClientsTbl[Entry Time] <= vMinVal
                && ClientsTbl[Exit Time] >= vMaxVal
        )
    )
RETURN
    vNumberOfClients

Annotation 2019-08-09 120930.jpg

 

Whenever I have more time I could think of something better.

View solution in original post

12 REPLIES 12
mussaenda Senior Member
Senior Member

Re: Start and end time

Hi @jojo54 ,

 

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("i45WCkgtKs7PUzBU0lEyszIwAFKWICpWBy5lBBQzh0hZWBmjSBmDxaC6DE2RpUzABpqYAilDAzQTTcEmGkIooJLYWAA=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [Name = _t, #"Entry Time" = _t, #"Exit Time" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Name", type text}, {"Entry Time", type time}, {"Exit Time", type time}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Custom", each List.Times([Entry Time], 20, #duration(0, 0, 15, 0))),
    #"Expanded Custom" = Table.ExpandListColumn(#"Added Custom", "Custom"),
    #"Changed Type1" = Table.TransformColumnTypes(#"Expanded Custom",{{"Custom", type time}}),
    #"Added Custom1" = Table.AddColumn(#"Changed Type1", "Custom.1", each if [Exit Time] < [Custom] then "Hide" else "Show"),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom1", each ([Custom.1] = "Show"))
in
    #"Filtered Rows"

From the result you posted, The person 3 must also have count in 8:00 time because 8:00 is his entry time, right?

2019_08_03_10_58_24_Time_Duration_Power_BI_Desktop.png

File is here

Re: Start and end time

Nice approach @mussaenda ,   why use the 20 in List.Times?

 

Also, @jojo54 ,  given this code will work in Excel I'd be interested to know what performance issues your having that you're trying PowerBI.   Is it the 32 Bit Excel version that's limiting you?

 

Wyn

 

mussaenda Senior Member
Senior Member

Re: Start and end time

Hi @wynhopkins,

 

Thank you fo the compliment. I just used 20 because I assumed that a person stays in a cafe max like 5 hours.

20 can be changed, @jojo54 .  depends on your need. 🙂

 

Have a nice day!

Geradav Regular Visitor
Regular Visitor

Re: Start and end time

Hi @jojo54 

 

Here is an alternative using DAX

Number of clients =
VAR vMinVal =
    MIN ( TimeTable[Value] )
VAR vMaxVal =
    MAX ( TimeTable[Value] )
VAR vClientEntry =
    CALCULATE (
        COUNTROWS ( ClientsTbl ),
        TimeTable[Value] <= vMinVal,
        ALL ( TimeTable )
    )
VAR vClientExit =
    CALCULATE (
        COUNTROWS ( ClientsTbl ),
        TimeTable[Value] >= vMaxVal,
        ALL ( TimeTable ),
        USERELATIONSHIP ( ClientsTbl[Exit Time], TimeTable[Value] )
    )
RETURN
    MIN ( vClientExit, vClientEntry )

Create a time table, then create a relationship between Start Time and the time value, and a second one between Exit Time and the time value. One will be an active relationship, while the second will be inactive.

Like this:

Annotation 2019-08-03 141705.jpg

 

Then you can use that measure in your visualization. Time value in X axis and the measure in the Value area

Annotation 2019-08-03 142013.jpg

And you can also make use of the same DAX code in the data model of Excel (Power Pivot)

 

Let us know what works for you

mussaenda Senior Member
Senior Member

Re: Start and end time

Hi @Geradav ,

 

Nice approach using dax! Is there a way to make the interval evey 15mins instead of every 5mins?

Highlighted
Geradav Regular Visitor
Regular Visitor

Re: Start and end time

Hi @mussaenda 

 

I used a table function to generate a time series with an increment of 5 minutes, so yes, it can easily be changed to 15 minutes.

TimeTable =
GENERATESERIES ( TIME ( 5, 0, 0 ), TIME ( 13, 0, 0 ), TIME ( 0, 15, 0 ) )
jojo54 Frequent Visitor
Frequent Visitor

Re: Start and end time

Thanks all! Apologies for late reply - going to take a look and give these solutions a go!

jojo54 Frequent Visitor
Frequent Visitor

Re: Start and end time

I have ~3m lines of data so excel is extremely limiting! I'm looking at it across every day, across a few years

jojo54 Frequent Visitor
Frequent Visitor

Re: Start and end time

@Geradav 

 

Thanks for this solution!  I like that I can actually write this down to work out what it's doing!

 

But I'm finding an issue with the below: (updated table)

NameEntry TimeExit Time
Person 16:009:00
Person 27:008:30
Person 38:159:15
Person 46:4510:00
Person 57:157:45

 

If I were to look at 8am, according to the DAX query,

vcliententry = 4

vclientexit = 4

So query will return 4 being min (4,4)

 

However there's actually only 3 people who's at the cafe (Person 1,2,4)

 

I tried writing this on paper and I've worked out that this mis-calculation occurs when there are people on the list who have exited prior to the TimeValue AND there are people who not yet entered into the cafe at the TimeValue.

 

Any ideas on how to fix this?

 

Thanks!


 

Helpful resources

Announcements
New Kudos Received Badges Coming

New Kudos Received Badges Coming

Kudos to you if you earned one of these! Check your inbox for a notification.

Microsoft Implementation for Communities Wins Award

Microsoft Implementation for Communities Wins Award

Learn about the award-winning innovation that was implemented across Microsoft’s Business Applications Communities.

Power Platform World Tour

Power Platform World Tour

Find out where you can attend!

Top Kudoed Authors (Last 30 Days)
Users online (3,937)