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

Counting numbers per hour between a start and end time

Hello,

 

I am trying to find the count of people at any give hour. For example, lets say we have a column withh people's names called People, a column with start time called StartTime and a column with the end time called EndTime. What I want to do is see the count of people in any given hour range. 

 

An example I found online that exactly matches my problems in this: https://stackoverflow.com/questions/33435898/counting-numbers-per-hour-between-a-start-and-end-time 

 

This example has a answer in SQL but I was not sure how to translate this to DAX. 

 

Please let me know if you need any other information.

 

Thanks

1 ACCEPTED SOLUTION

@Anonymous,

 

Please follow steps below:

 

Click Modeling-> New Table, input DAX formula below:

Table_temp = GENERATESERIES(HOUR(MIN('Table'[Start_time])), HOUR(MAX('Table'[End_time])))

This will general a table with only one column [Value] to achieve all the hour included in the table, then right click on the new table to create a calculate column using DAX formula below:

Count_Number = CALCULATE(COUNTROWS('Table'), FILTER(ALL('Table'), HOUR('Table'[Start_time]) <= Table_temp[Value] && HOUR('Table'[End_time]) >= Table_temp[Value]))

Capture.PNG 

 

You can also refer to the attached sample pbix file if you want.

 

Regards,

Jimmy Tao

View solution in original post

9 REPLIES 9
sps-reporter
Helper II
Helper II

Thanks for this thread. Exactly what I was looking for and need to go the next step and do the counts with filters on attributes from the original table like date and colors for example. If someone got to the last part of this and figure out how to do this would enjoy any tips.

Thanks

Anonymous
Not applicable

Hello,

 

I am learning powerBI. I followed the above solution but somehow it's not working for my data. I wanted to show employee count by the hour and date. Here is what the sample data look like:

r_kurkur_0-1639750659816.png

 


Any help on this is appreciated 😊

Thanks
Richa

v-yuta-msft
Community Support
Community Support

@Anonymous,

 

To be general, you can create a measure using DAX formula like pattern below:

 

Number =
COUNTROWS (
    FILTER ( table, table[date] <= EndTime && table[date] >= StartTime )
)

Regards,

Jimmy Tao

Anonymous
Not applicable

Lets just say we only have a table with columns Person, StartTime, and EndTime. Both StartTime and EndTime are in a time format like 9:00:00 AM. I want to get a count of the number of people that are present at each hour period.

 

I have an example of this in the link I gave but I can do another example. Lets say Adam has a StartTime of 9 AM and EndTime of 11 AM. He would then have a count of 9AM-10 AM: 1 and 10AM-11AM: 1. This is because he was present for those two hours and these counts would continue for all of the people in the table. 

 

Please look at the link and look at the SQL code answer because that may lead to some ideas on how to tackle this in Power BI.

@Anonymous,

 

Have you solved your issue by now? If you have, could you please help mark the correct answer to finish the thread? Your contribution will be much appreciated.

 

Regards,

Jimmy Tao

@Anonymous,

 

Please follow steps below:

 

Click Modeling-> New Table, input DAX formula below:

Table_temp = GENERATESERIES(HOUR(MIN('Table'[Start_time])), HOUR(MAX('Table'[End_time])))

This will general a table with only one column [Value] to achieve all the hour included in the table, then right click on the new table to create a calculate column using DAX formula below:

Count_Number = CALCULATE(COUNTROWS('Table'), FILTER(ALL('Table'), HOUR('Table'[Start_time]) <= Table_temp[Value] && HOUR('Table'[End_time]) >= Table_temp[Value]))

Capture.PNG 

 

You can also refer to the attached sample pbix file if you want.

 

Regards,

Jimmy Tao

Anonymous
Not applicable

@v-yuta-msft Thank you this works but I want to pose one last question. How could we possibly connect this temp table to the original table so that different filter could happen? If there was a unique identifier in the original table would it be possible to get this in the temp table and then make a relationship between the two tables?

@Anonymous,

 

Yes, you use DAX function ADDCOLUMNS, SUMMARIZE or SELECTCOLUMNS based on your specific requirement, although you can't view the temp_table in query editor, you can create relationship between original table and temp table.

Capture.PNG 

 

Regards,

Jimmy Tao

 

Hi, I have a similar case, but I have one more date column aside from People, Start time, End time.

I can create temp time table with above method.

But i am not able to apply fiter to date. Can you explain more how to use DAX function ADDCOLUMNS, SUMMARIZE or SELECTCOLUMNS to create relationship such that filter works?

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.