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

Be one of the first to start using Fabric Databases. View on-demand sessions with database experts and the Microsoft product team to learn just how easy it is to get started. Watch now

Reply
NB689
Helper I
Helper I

Sum Days Logged in Based on user and date

I'm looking to create a measure that will show the sum number of days, that user(s) have logged in.

 

Here is a sample of the data from a table called Sheet1:

User IDLoginLogoutDays Logged In
A7/18/2022 8:00 AM7/21/2022 4:00 PM4
A7/19/2022 8:00 AM7/21/2022 4:00 PM3
A7/21/2022 8:00 AM7/23/2022 4:00 PM3
B7/18/2022 8:00 AM7/21/2022 4:00 PM4
B7/18/2022 8:00 AM7/20/2022 4:00 PM3
C7/18/2022 8:00 AM7/19/2022 4:00 PM2
C7/21/2022 8:00 AM7/22/2022 4:00 PM1
C7/17/2022 8:00 AM7/18/2022 4:00 PM1
Total  15

 

The sumation per row is fairly straight forward. This is the logout time minus the login time, and I add a 1 so that I get the total number of calendar dates.

 

The sumation Total for all would be counted like this.

 

For user A, they were logged in for 6 days (The 18th, 19th, 20th, 21st, 22nd, & 23rd).

For user B, they were logged in for 4 days (The 18th, 19th, 20th, & 21st).

For user C, they were logged in for 5 days (The 17th, 18th, 19th, 21st & 22nd.)

 

So the total value for the column that I would like to calcuate would be 15 days(6 days/4 days, 5 days.)

 

I do have a current measure created, but it does not sum everything correctly. It looks like this:

 

Days = SUMX ( 
        Sheet1,
        DATEDIFF ( Sheet1[Login], Sheet1[Logout], Day )
    ) +1

 

 

 

Any help on this issue will be much appreciated. Thank you!

1 ACCEPTED SOLUTION
tamerj1
Super User
Super User

Hi @NB689 

If I correctly understand correctly then use

Days =
SUMX (
    VALUES ( Sheet1[User ID] ),
    CALCULATE (
        DATEDIFF ( MIN ( Sheet1[Login] ), MAX ( Sheet1[Logout] ), DAY ) + 1
    )
)

View solution in original post

8 REPLIES 8
vapid128
Solution Specialist
Solution Specialist

image.png

 

rankindex =
var _rankLogin =RANKX(FILTER('Table','Table'[User ID]=EARLIER('Table'[User ID])),[Login],,ASC)
var _rankLogOut = RANKX(FILTER('Table','Table'[User ID]=EARLIER('Table'[User ID])),[Logout])
return _rankLogin+DIVIDE(1,_rankLogOut)+RAND()/1000
 
Rank = RANKX(FILTER('Table','Table'[User ID]=EARLIER('Table'[User ID])),[rankindex],,ASC)
 
NextLogin = LOOKUPVALUE('Table'[Login],'Table'[User ID],'Table'[User ID],'Table'[Rank],'Table'[Rank]+1)
 
daysLogin =
var _loginReal =IF([NextLogin]>0, MIN([Logout],[NextLogin]),[Logout])
return _loginReal-[Login]
 
 
That doesnt works when your data like:
User IDLoginLogoutDays Logged In
A7/18/2022 8:00 AM7/22/2022 4:00 PM5
A7/19/2022 8:00 AM7/21/2022 4:00 PM3

Normally Login Logout data shouldn't happned this case.
tamerj1
Super User
Super User

Hi @NB689 

If I correctly understand correctly then use

Days =
SUMX (
    VALUES ( Sheet1[User ID] ),
    CALCULATE (
        DATEDIFF ( MIN ( Sheet1[Login] ), MAX ( Sheet1[Logout] ), DAY ) + 1
    )
)

I entered the exact test data into Power BI just to be sure it wasn't something with my larger data set throwing it off. So here I added a column for the first measure and the most recent one you sent with MIN and MAX.

 

NB689_0-1658515610264.png

The results that I intend to see with this new column would look like:

User IDDays (expected outcome)
A4
A3
A3
B3
B4
C2
C2
C2
Total15

 

Why do I want to see 15? I only want to count each day one time per user. So for this view:

NB689_1-1658515887847.png

I would want to see the following:

User IDDays (expected outcome)
A4
A3
A3
Total6

 

I would want to see 6 because the only days that this user was actually logged in were on the 18th, 19th, 20th, 21st, 22nd, & 23rd. With the other measure, I would be double counting some of the days.

 

So for user B, I would want to see a total of 4 days (The 18th, 19th, 20th, & 21st) 

 

For user C I would want to see 5 days (The 17th, 18th, 19th, 21st & 22nd.) This one might make the DAX tricky, since it skips a day in the series.

 

Let me know if you're not sure of where I got some of these numbers I can clarify. Thanks for your help!

tamerj1
Super User
Super User

Hi @NB689 

please try

 

Days = SUMX ( 
        Sheet1,
        DATEDIFF ( Sheet1[Login], Sheet1[Logout], Day ) + 1
    )

 

This gets me a total of 21, so a sum total of all days, but I need to deal with setting up measure to not count the same days multiple times.

@NB689 

Please clarify further perhaps with some screenshots 

amitchandak
Super User
Super User

@NB689 , You have to correct logout first

 

Use this logout column

 

New Logout=
Var _max = minx(filter(Table, [User ID] = earlier([User ID]) && [Login] > earlier([Login])),[Login])
return
if([Logout] >_max, _max, [Logout])

Join us as experts from around the world come together to shape the future of data and AI!
At the Microsoft Analytics Community Conference, global leaders and influential voices are stepping up to share their knowledge and help you master the latest in Microsoft Fabric, Copilot, and Purview.
️ November 12th-14th, 2024
 Online Event
Register Here

This is probably a syntax thing that I'm looking over, but Power Pivot doesn't like any of my column names. I do have a date column for Login/Logout, and I created a measure that I can use within this measure, whichever way I am supposed to use it.

 

NB689_0-1658509970711.png

 

Helpful resources

Announcements
Las Vegas 2025

Join us at the Microsoft Fabric Community Conference

March 31 - April 2, 2025, in Las Vegas, Nevada. Use code MSCUST for a $150 discount!

Dec Fabric Community Survey

We want your feedback!

Your insights matter. That’s why we created a quick survey to learn about your experience finding answers to technical questions.

ArunFabCon

Microsoft Fabric Community Conference 2025

Arun Ulag shares exciting details about the Microsoft Fabric Conference 2025, which will be held in Las Vegas, NV.

December 2024

A Year in Review - December 2024

Find out what content was popular in the Fabric community during 2024.