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

Find everything you need to get certified on Fabric—skills challenges, live sessions, exam prep, role guidance, and more. Get started

Reply
pensas
New Member

COUNTROWS between two DateTime columns

Hello people of the internet! _o/

 

First time posting for help anywhere in general so bare with me...

 

I have two columns of DateTime data (in and out).

I want to calculate the count of events in que when a new event is registred based on these DateTime columns.

 

Here is the example data with expected result (CountInQue column):

IDTimeInTimeOUTCountInQue
130.7.2022 5:2130.7.2022 5:430
21.8.2022 6:011.8.2022 6:140
31.8.2022 6:281.8.2022 6:440
41.8.2022 6:311.8.2022 6:531
51.8.2022 6:591.8.2022 7:180
61.8.2022 7:161.8.2022 7:351
71.8.2022 7:201.8.2022 7:541
81.8.2022 7:281.8.2022 8:042
91.8.2022 7:451.8.2022 8:272
101.8.2022 8:311.8.2022 9:050
111.8.2022 8:401.8.2022 9:121
121.8.2022 8:571.8.2022 9:232

 

I bolded two examples to get the idea what I'm looking for.

 

I tried to use this post: https://community.powerbi.com/t5/Power-Query/Count-rows-if-between-two-time-slots/td-p/1424481 but the outcome counts the values the wrong way around. This example count the rows that happen between certain rows time periods. I have also been trying to modify the measure in the previous link without any success. 

 

Any suggestions? 😥 

1 ACCEPTED SOLUTION
lbendlin
Super User
Super User

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("XY8xDsIwDEWvgjJXle3YTZoVlk4IqUxVxw4MlIXevyFFBmfz93v6sqfJDRfXuPHxXIb1O1zvY57Or219D+ttW9zcTA7zykMbWgKikySqF+zzAopLecI2HqRLgDYjq+ktoWgz/0y2xFedUpqKKRXp/3NIGLWzq0iVvWhnsITAZmE1Y2WaHBN8TCpmdReLNSmoiWCRfb5PIPoSolUZrIqklyJZVYJVyR8HzDs=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t]),
    #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    #"Changed Type with Locale" = Table.TransformColumnTypes(#"Promoted Headers", {{"TimeIn", type datetime},{"TimeOUT", type datetime}}, "de-DE"),
    #"Changed Type" = Table.TransformColumnTypes(#"Changed Type with Locale",{{"ID", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "InQueue", (k)=> Table.RowCount(Table.SelectRows(#"Changed Type",each [ID]<k[ID] and [TimeOUT]>k[TimeIn]))),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"InQueue", Int64.Type}})
in
    #"Changed Type1"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".

View solution in original post

3 REPLIES 3
lbendlin
Super User
Super User

There are lots of gems in them there M functions. Let me know if you like to learn more.

lbendlin
Super User
Super User

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("XY8xDsIwDEWvgjJXle3YTZoVlk4IqUxVxw4MlIXevyFFBmfz93v6sqfJDRfXuPHxXIb1O1zvY57Or219D+ttW9zcTA7zykMbWgKikySqF+zzAopLecI2HqRLgDYjq+ktoWgz/0y2xFedUpqKKRXp/3NIGLWzq0iVvWhnsITAZmE1Y2WaHBN8TCpmdReLNSmoiWCRfb5PIPoSolUZrIqklyJZVYJVyR8HzDs=", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type nullable text) meta [Serialized.Text = true]) in type table [Column1 = _t, Column2 = _t, Column3 = _t, Column4 = _t]),
    #"Promoted Headers" = Table.PromoteHeaders(Source, [PromoteAllScalars=true]),
    #"Changed Type with Locale" = Table.TransformColumnTypes(#"Promoted Headers", {{"TimeIn", type datetime},{"TimeOUT", type datetime}}, "de-DE"),
    #"Changed Type" = Table.TransformColumnTypes(#"Changed Type with Locale",{{"ID", Int64.Type}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "InQueue", (k)=> Table.RowCount(Table.SelectRows(#"Changed Type",each [ID]<k[ID] and [TimeOUT]>k[TimeIn]))),
    #"Changed Type1" = Table.TransformColumnTypes(#"Added Custom",{{"InQueue", Int64.Type}})
in
    #"Changed Type1"

How to use this code: Create a new Blank Query. Click on "Advanced Editor". Replace the code in the window with the code provided here. Click "Done".

Thank you sir!

 

This solved the issue after some tuning.
I have never used M functions and this was way better sollution than what I had in mind.

Helpful resources

Announcements
Europe Fabric Conference

Europe’s largest Microsoft Fabric Community Conference

Join the community in Stockholm for expert Microsoft Fabric learning including a very exciting keynote from Arun Ulag, Corporate Vice President, Azure Data.

Power BI Carousel June 2024

Power BI Monthly Update - June 2024

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

PBI_Carousel_NL_June

Fabric Community Update - June 2024

Get the latest Fabric updates from Build 2024, key Skills Challenge voucher deadlines, top blogs, forum posts, and product ideas.

Top Solution Authors