Count the Number of Sick Occasions in Dax - Calculated Column - PowerBI

MikeCobra

New Member
Joined
May 3, 2023
Messages
9
Office Version
  1. 365
Platform
  1. Windows
Hi All,

I was wondering if anyone know how to count the number of Sick Occasions in Dax? i know how in Excel but dax is a completely different beast.

Looking at the example i have posted. I want it to show 0 if they were not absent. Then increment once they become absent.

But then they return to work and then become ill again. I want it to show 2 this time. As seen in the example column (H). Also for it to be related to each persons Employee ID. so that it can reset etc.

Just so i can track the number of occasions a colleague has gone off sick. I have tried Power Query but it never finished loading due to the size of the data (over 30000 rows)

I also tried in dax using the Earlier() function but could never get it to work.

Any help would be greatly appreciated.

Thanks MikeCobra
 

Attachments

  • Count Number of Sick Occasions Example.PNG
    Count Number of Sick Occasions Example.PNG
    35 KB · Views: 13

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
Here a solution in Power Query. I formatted the first groupby step for better readability.

Power Query:
let
    Source = Excel.CurrentWorkbook(){[Name="Table1"]}[Content],
    grp = Table.Group(Source, {"Employee name", "Employee ID"}, 
        {
            {"tbl", each  
                Table.AddColumn(
                    Table.AddIndexColumn(_,"ind",0),"tCol", each let r = Source[Absent]{[ind]-1} in 
                            try if r =0 and [Absent] = 1 then [ind] 
                                else if  r =1 and [Absent] = 0 then 0 else null 
                            otherwise 0
                        ) 

            }
        }
    ),
    exp = Table.Combine(grp[tbl]),
    fill = Table.FillDown(exp,{"tCol"}),
    grp2 = Table.Group(fill, {"Employee name", "tCol"}, {{"Count", each _}}),
    grp3 = Table.Group(grp2, {"Employee name"}, {{"tbl", each Table.AddIndexColumn(_,"Sick Occasions",0)}}),
    index = Table.Combine(grp3[tbl]),
    exp2 = Table.ExpandTableColumn(index, "Count", {"Date", "Present", "Authorised Holiday", "Absent"}, {"Date", "Present", "Authorised Holiday", "Absent"}),
    dCol = Table.RemoveColumns(exp2,{"tCol"}),
    cType = Table.TransformColumnTypes(dCol,{{"Date", type date}}),
    result = Table.Sort(cType,{{"Employee name", Order.Ascending}, {"Date", Order.Ascending}})
in
    result


Book1
ABCDEFGHIJKLM
1Employee nameEmployee IDDatePresentAuthorised HolidayAbsentEmployee nameDatePresentAuthorised HolidayAbsentSick Occasions
2a115-2-2024100a5-2-20241000
3a116-2-2024100a6-2-20241000
4a117-2-2024100a7-2-20241000
5a118-2-2024001a8-2-20240011
6a119-2-2024001a9-2-20240011
7a1112-2-2024001a12-2-20240011
8a1113-2-2024100a13-2-20241000
9a1114-2-2024001a14-2-20240012
10a1115-2-2024001a15-2-20240012
11a1116-2-2024010a16-2-20240100
12b125-2-2024100b5-2-20241000
13b126-2-2024100b6-2-20241000
14b127-2-2024100b7-2-20241000
15b128-2-2024001b8-2-20240011
16b129-2-2024001b9-2-20240011
17b1212-2-2024001b12-2-20240011
18b1213-2-2024100b13-2-20241000
19b1214-2-2024001b14-2-20240012
20b1215-2-2024001b15-2-20240012
21b1216-2-2024010b16-2-20240100
Sheet1
 
Upvote 0
hi JEC,

I have tried this but because of my dataset (over 30000 rows)

It just constantly loads and never finishes the loading

Thanks
 
Upvote 0
Hmm you could try to buffer steps. Give this a try

Power Query:
let
    Source = Table.Buffer(Excel.CurrentWorkbook(){[Name="Table1"]}[Content]),
    grp = Table.Group(Source, {"Employee name", "Employee ID"}, 
        {
            {"tbl", each  
                Table.AddColumn(
                    Table.AddIndexColumn(_,"ind",0),"tCol", each let r = Source[Absent]{[ind]-1} in 
                            try if r =0 and [Absent] = 1 then [ind] 
                                else if  r =1 and [Absent] = 0 then 0 else null 
                            otherwise 0
                        ) 

            }
        }
    ),
    exp = Table.Combine(grp[tbl]),
    fill = Table.Buffer(Table.FillDown(exp,{"tCol"})),
    grp2 = Table.Group(fill, {"Employee name", "tCol"}, {{"Count", each _}}),
    grp3 = Table.Group(grp2, {"Employee name"}, {{"tbl", each Table.AddIndexColumn(_,"Sick Occasions",0)}}),
    index = Table.Combine(grp3[tbl]),
    exp2 = Table.ExpandTableColumn(index, "Count", {"Date", "Present", "Authorised Holiday", "Absent"}, {"Date", "Present", "Authorised Holiday", "Absent"}),
    dCol = Table.RemoveColumns(exp2,{"tCol"}),
    cType = Table.TransformColumnTypes(dCol,{{"Date", type date}}),
    result = Table.Sort(cType,{{"Employee name", Order.Ascending}, {"Date", Order.Ascending}})
in
    result
 
Upvote 0
Hi JEC,

The buffer has allowed the code to run but its not running correctly

Like for this instance - it has found that it is a sickness but because they come after each other. They should be the same index rather than a new one. Since it is still apart of the same instance

See attached. They should both be 1 rather than 1 and 2.

Thanks Mike
 

Attachments

  • Indexs are different for Same Sickness Instance.PNG
    Indexs are different for Same Sickness Instance.PNG
    5.8 KB · Views: 5
Upvote 0

Forum statistics

Threads
1,215,077
Messages
6,122,991
Members
449,094
Latest member
masterms

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top