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

Get Fabric Certified for FREE during Fabric Data Days. Don't miss your chance! Request now

Reply
FreedJustine
Helper I
Helper I

How to Countifs in Power Bi (use in 2 tables)

Hi! I need help, im currently counting numbers of people who are active between two time range.

I have two tables

  • Table 1 : Logs
  • Table 2: Time Table
 

Capture.JPG

 

1 ACCEPTED SOLUTION

Hi @FreedJustine ,

 

I created a measure for you.

Here is the result.
1-1.PNG

Here is my test file for your reference.

 

Community Support Team _ Eads
If this post helps, then please consider Accept it as the solution to help the other members find it.

View solution in original post

6 REPLIES 6
rajulshah
Resident Rockstar
Resident Rockstar

Hello @FreedJustine,

 

Please use the following M Query for the table as below:

let
    Source = Table.FromRows(Json.Document(Binary.Decompress(Binary.FromText("ZY5LDsAgCAWv0rg2KSgf5SrG+1+jpa4KCSve8Ji1CrZeagG832mAfE1D/m9QfbXrKsQSIjCKtFiHj1YO1cMgV9OpnhoizR7s9w4PoRRx9miHbhStxQCSSD90Zw0RGkaa/N3eDw==", BinaryEncoding.Base64), Compression.Deflate)), let _t = ((type text) meta [Serialized.Text = true]) in type table [#"User ID" = _t, #"Log-in DateTime" = _t, #"End DateTime" = _t]),
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"User ID", Int64.Type}, {"Log-in DateTime", type datetime}, {"End DateTime", type datetime}}),
    #"Added Custom" = Table.AddColumn(#"Changed Type", "Log-In Time", each List.DateTimes(DateTime.Date([#"Log-in DateTime"])&
Time.From(Text.From(Time.Hour(Time.From([#"Log-in DateTime"])))&":"&(if Time.Minute(Time.From([#"Log-in DateTime"]))>= 30 then "30" else "0")&":00"),Number.From(60*24*([End DateTime]-[#"Log-in DateTime"])/30)+2, #duration(0,0,30,0))),
    #"Expanded Log-In Time" = Table.ExpandListColumn(#"Added Custom", "Log-In Time"),
    #"Added Custom1" = Table.AddColumn(#"Expanded Log-In Time", "EliminateRows", each if Time.From([#"Log-in DateTime"])> Time.From([#"Log-In Time"]) then 1 else if Time.From([#"Log-In Time"])>Time.From([End DateTime]) then 1 else 0),
    #"Filtered Rows" = Table.SelectRows(#"Added Custom1", each ([EliminateRows] = 0)),
    #"Changed Type1" = Table.TransformColumnTypes(#"Filtered Rows",{{"End DateTime", type time}, {"Log-In Time", type time}})
in
    #"Changed Type1"


Then, create a Dimension Table for the Time as below:
TableTime.png

Create relationship between this table and our table using Time Column and then create measure as below:

Users Count = 
VAR CountUsers = COUNT('Login Timings'[User ID])
RETURN IF(ISBLANK(CountUsers),0,CountUsers)


You will get the result as expected. See below:
timeduration.png

 

















Hope this helps.

@rajulshah 

 

Thank you very much!

 

Can you share the pbix?

Thanks @rajulshah 

 

However what if I have thousands of user ID and trx date, this process will generate lot of data and im afraid my pbix will slow down.

Is there any other ways? like using a measure? Thank you very much!

Hi @FreedJustine ,

 

I created a measure for you.

Here is the result.
1-1.PNG

Here is my test file for your reference.

 

Community Support Team _ Eads
If this post helps, then please consider Accept it as the solution to help the other members find it.

@v-eachen-msft 

 

Thank you very much! SELECTEDVALUE is working well, I add some filters too for other parameters.

Helpful resources

Announcements
Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

October Power BI Update Carousel

Power BI Monthly Update - October 2025

Check out the October 2025 Power BI update to learn about new features.

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.

Top Solution Authors