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

Earn a 50% discount on the DP-600 certification exam by completing the Fabric 30 Days to Learn It challenge.

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
LearnSurvey

Fabric certifications survey

Certification feedback opportunity for the community.

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.