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

How to calculate strength of students in a Class using DAX?

Hello All,

 

I have a requirement as i need to calculate the present strength of students in class.

Example.:-

Class A having 5 students A, B, C, D, E.

Now i will be getting the infomration from id swipecard machines that whether they are entered into the class or exited.

So now my data looks like this.

 

Student IDTimeIn/Out
A8:10:00 AMIn
B8:20:00 AMIn
C8:34:00 AMIn
D8:35:00 AMIn
A8:43:00 AMOut
A8:56:00 AMIn
E9:10:00 AMIn
C9:15:00 AMOut
C9:23:00 AMIn
D9:47:00 AMOut
B9:59:00 AMOut
B10:00:00 AMIn
D10:10:00 AMIn
A11:12:00 AMOut
B11:34:00 AMOut
C12:01:00 PMOut
D12:31:00 PMOut
E1:02:00 PMOut

 

 

Now If i categorige these timings with every half an hour duration, and calculate the strength of students that are present in class.

the expected output is as looks like this.

 

DurationStrength In Class
8:00 to 8:302 (A,B IN)
8:30 to 9:004 (B is IN already, C,D are New Entries, and A exited and enterd again)
9:00 to 9:305 (A,B,D Are IN already,E New Entry, and C exites and entered again)
9:30 to 10:003 (A, C, E are still in IN, but B and D are exited)
10:00 to 10:305 (All are present IN class)
10:30 to 11:005 (All are present IN class)
11:00 to 11:304 (A exited, B,C,D,E still IN Class)
11:30 to 12:003 (B Exited, C,D E still IN Class)
12:00 to 12:302 (C Exited, D,E Still IN Class)
12:30 to 1:00 PM1 (D Exited, E Still IN Class)
1:00 to 1:30 PM0 ()

 

So how can i calculate this in powerbi.

Please help.

I will be so thankful for your ideas sugestions.

let me know if you need any extra information.

 

Thanks,

Mohan V

1 ACCEPTED SOLUTION
Anonymous
Not applicable

I think i got the solution on my own.

 

I broke down the 24 hours into 30 min gap each.

 and i have written if condtion for all the durations and took only In employees count.

#"Added Custom1" = Table.AddColumn(#"Changed Type1", "Attendance", each if [#"Date-Time"] >= [BEGIN_DATE]&#time(0,0,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(0,30,0) then "0-0:30" 
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(0,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(1,0,0) then "0:30-1" 
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(1,0,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(1,30,0) then "1-1:30" 
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(1,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(2,0,0) then "1:30-2"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(2,0,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(2,30,0) then "2-2:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(2,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(3,0,0) then "2:30-3"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(3,0,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(3,30,0) then "3-3:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(3,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(4,0,0) then "3:30-4"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(4,0,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(4,30,0) then "4-4:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(4,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(5,0,0) then "4:30-5"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(5,0,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(5,30,0) then "5-5:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(5,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(6,0,0) then "5:30-6"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(6,0,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(6,30,0) then "6-6:30" 
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(6,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(7,0,0) then "6:30-7"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(7,0,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(7,30,0) then "7-7:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(7,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(8,0,0) then "7:30-8"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(8,0,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(8,30,0) then "8-8:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(8,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(9,0,0) then "8:30-9"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(9,0,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(9,30,0) then "9-9:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(9,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(10,0,0) then "9:30-10"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(10,0,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(10,30,0) then "10-10:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(10,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(11,0,0) then "10:30-11"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(11,0,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(11,30,0) then "11-11:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(11,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(12,0,0) then "11:30-12"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(12,0,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(12,30,0) then "12-12:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(12,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(13,0,0) then "12:30-13"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(13,00,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(13,30,0) then "13-13:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(13,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(14,0,0) then "13:30-14"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(14,00,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(14,30,0) then "14-14:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(14,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(15,0,0) then "14:30-15"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(15,00,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(15,30,0) then "15-15:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(15,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(16,00,0) then "15:30-16"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(16,00,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(16,30,0) then "16-16:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(16,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(17,0,0) then "16:30-17"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(17,00,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(17,30,0) then "17-17:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(17,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(18,0,0) then "17:30-18"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(18,00,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(18,30,0) then "18-18:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(18,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(19,0,0) then "18:30-19"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(19,00,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(19,30,0) then "19-19:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(19,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(20,0,0) then "19:30-20"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(20,00,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(20,30,0) then "20-20:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(20,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(21,0,0) then "20:30-21"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(21,00,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(21,30,0) then "21-21:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(21,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(22,0,0) then "21:30-22"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(22,00,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(22,30,0) then "22-22:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(22,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(23,0,0) then "22:30-23"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(23,00,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(23,30,0) then "23-23:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(23,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(24,00,0) then "23:30-24"
else null),

 

And it worked for me.

View solution in original post

9 REPLIES 9
v-juanli-msft
Community Support
Community Support

Hi @Anonymous

There are some calculated columns created in your example dataset.

Please have a look and try based on my pbix.

6.png

Since there is no value between "10:30 to 11:00", my formula can't convert this period for the table.

If your data is not very large and complex, a temporary way is to enter data in a new table of this row and union it to your table.

If you have trouble doing this, please let me know.

 

Best Regards

Maggie

 

Anonymous
Not applicable

Thanks for the reply @v-juanli-msft.

 

That something unexpected.

 

At some point i lost hope that i can get a help from you. but really thanks.

 

Coming to your pbix, i tried all the dax and calculated columns you have given and yes i got the output.

But the issue is my table contains around 50lakhs of records.

So for now, i jus took a single date values(6/1/2018) and it is around 750 records.

 

After that, when i tried those dax's, the second dax, "END" i gota error as

1.JPG

 

I really didnt get whats the error about.

 

Then i filtered it out by few of the employee ids, and tried the same, then it worked.

i got the strength final values.

But i got lots of negative values.

 

Output:- 

2.JPG

 

Then i have checked The strength values are not as expected.

 

I ll be sharing you the data, please look into it whenever you got free time.

 

I hope, n i wish to see your magnificient support.

 

Thanks,

Mohan V

 

Hi @Anonymous

The error is one column which is used in the formula is in text type, but we need it to be in date/time type.

I haven't recevice your data until now.

 

Best Regards

Maggie

Anonymous
Not applicable

@v-juanli-msft sent link in private message.
Please check once.

Anonymous
Not applicable

@v-juanli-msft could you please suggest me on this.

Anonymous
Not applicable

I think i got the solution on my own.

 

I broke down the 24 hours into 30 min gap each.

 and i have written if condtion for all the durations and took only In employees count.

#"Added Custom1" = Table.AddColumn(#"Changed Type1", "Attendance", each if [#"Date-Time"] >= [BEGIN_DATE]&#time(0,0,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(0,30,0) then "0-0:30" 
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(0,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(1,0,0) then "0:30-1" 
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(1,0,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(1,30,0) then "1-1:30" 
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(1,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(2,0,0) then "1:30-2"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(2,0,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(2,30,0) then "2-2:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(2,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(3,0,0) then "2:30-3"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(3,0,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(3,30,0) then "3-3:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(3,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(4,0,0) then "3:30-4"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(4,0,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(4,30,0) then "4-4:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(4,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(5,0,0) then "4:30-5"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(5,0,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(5,30,0) then "5-5:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(5,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(6,0,0) then "5:30-6"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(6,0,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(6,30,0) then "6-6:30" 
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(6,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(7,0,0) then "6:30-7"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(7,0,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(7,30,0) then "7-7:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(7,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(8,0,0) then "7:30-8"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(8,0,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(8,30,0) then "8-8:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(8,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(9,0,0) then "8:30-9"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(9,0,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(9,30,0) then "9-9:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(9,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(10,0,0) then "9:30-10"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(10,0,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(10,30,0) then "10-10:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(10,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(11,0,0) then "10:30-11"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(11,0,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(11,30,0) then "11-11:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(11,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(12,0,0) then "11:30-12"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(12,0,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(12,30,0) then "12-12:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(12,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(13,0,0) then "12:30-13"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(13,00,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(13,30,0) then "13-13:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(13,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(14,0,0) then "13:30-14"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(14,00,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(14,30,0) then "14-14:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(14,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(15,0,0) then "14:30-15"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(15,00,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(15,30,0) then "15-15:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(15,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(16,00,0) then "15:30-16"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(16,00,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(16,30,0) then "16-16:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(16,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(17,0,0) then "16:30-17"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(17,00,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(17,30,0) then "17-17:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(17,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(18,0,0) then "17:30-18"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(18,00,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(18,30,0) then "18-18:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(18,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(19,0,0) then "18:30-19"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(19,00,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(19,30,0) then "19-19:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(19,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(20,0,0) then "19:30-20"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(20,00,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(20,30,0) then "20-20:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(20,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(21,0,0) then "20:30-21"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(21,00,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(21,30,0) then "21-21:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(21,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(22,0,0) then "21:30-22"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(22,00,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(22,30,0) then "22-22:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(22,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(23,0,0) then "22:30-23"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(23,00,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(23,30,0) then "23-23:30"
else
if [#"Date-Time"] >= [BEGIN_DATE]&#time(23,30,0) and [#"Date-Time"] < [BEGIN_DATE]&#time(24,00,0) then "23:30-24"
else null),

 

And it worked for me.

RMDNA
Solution Sage
Solution Sage

@Anonymous,

 

Is the text in the Strength in Class column just there for extra clarity in your example, or do you need that to actually be part of the result?

Anonymous
Not applicable

@RMDNA Could you please suggest me on this.

 

I will be so thankful to you.

 

Thanks,

Mohan V

Anonymous
Not applicable

@RMDNA yes Its for your extra clarity.

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.