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
HerbertDashwood
Frequent Visitor

Analysis of conditions over time with interval periods (week couter - conditional rows SUM)

Hi,

I'm working on an analysis on payroll data to check if pensions are paid appropriately for all employees. I'm working with a dataset which shows if an employee has worked in a given week (see attached). The pension fee of an employee is dependent on the 'phase' an employee is in. Based on the amount of weeks an employee has worked a phase can be determined. Even if an employee has only worked 1 hour the whole week counts as a 'worked week'. 

 

For this analysis I'm tryring to add a column to my table in which the expected phase per week is included based on the following rules:

  1. Phase A: 0-26 weeks, no pesions fees
  2. Phase B: 26-78 weeks, basis fee
  3. Phase C: >78 weeks, plus fee
  4. Weeks without hours worked do not count for the phase system
  5. If an employee has not worked for over 26 weeks the previous phase is applied (so C becomes B again)

 

I tried working with a counter which tracks the number op applicable weeks ( this is how I would approach this in say SQL) but as I'm fairly new to PowerBI I did not yet succeed. I'm not sure what the best apporach is here. Especially rule 4 & 5 are a mistery. Does anyone have a suggestion?

 

Below is a table with dummy data. The column 'Phase' and 'Counter' are the columns I wish to add and now only show the desired results. 

 

Week-YearEmployeeWorkedPhaseCounter
1-2019E5590264YesA1
2-2019E5590264YesA2
3-2019E5590264YesA3
4-2019E5590264YesA4
5-2019E5590264YesA5
6-2019E5590264YesA6
7-2019E5590264YesA7
8-2019E5590264YesA8
9-2019E5590264YesA9
10-2019E5590264YesA10
11-2019E5590264YesA11
12-2019E5590264YesA12
13-2019E5590264YesA13
14-2019E5590264YesA14
15-2019E5590264YesA15
16-2019E5590264YesA16
17-2019E5590264YesA17
18-2019E5590264YesA18
19-2019E5590264YesA19
20-2019  A19
21-2019  A19
22-2019E5590264YesA20
23-2019E5590264YesA21
24-2019E5590264YesA22
25-2019E5590264YesA23
26-2019E5590264YesA24
27-2019E5590264YesA25
28-2019E5590264YesA26
29-2019  A26
30-2019E5590264YesB27
31-2019E5590264YesB28
32-2019E5590264YesB29
33-2019E5590264YesB30
34-2019E5590264YesB31
35-2019E5590264YesB32
36-2019E5590264YesB33
37-2019E5590264YesB34
38-2019E5590264YesB35
39-2019E5590264YesB36
40-2019E5590264YesB37

 

1 ACCEPTED SOLUTION
Greg_Deckler
Super User
Super User

@HerbertDashwood OK, either in PQ or DAX, get yourself an Index column. In DAX it is:

Index = LEFT([Week-Year],SEARCH("-",[Week-Year])-1)+0

After that the Counter column is a breeze:

Counter = COUNTROWS(FILTER('Table16',[Index]<=EARLIER([Index]) && [Worked]<>""))

Which then makes Phase obvious:

Phase = 
SWITCH(TRUE(),
    [Counter]<=26,"A",
    [Counter]>78,"C",
    "B"
)

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

View solution in original post

4 REPLIES 4
Greg_Deckler
Super User
Super User

@HerbertDashwood OK, either in PQ or DAX, get yourself an Index column. In DAX it is:

Index = LEFT([Week-Year],SEARCH("-",[Week-Year])-1)+0

After that the Counter column is a breeze:

Counter = COUNTROWS(FILTER('Table16',[Index]<=EARLIER([Index]) && [Worked]<>""))

Which then makes Phase obvious:

Phase = 
SWITCH(TRUE(),
    [Counter]<=26,"A",
    [Counter]>78,"C",
    "B"
)

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

@Greg_Deckler First of all, many thanks! This is very helpfull. Currently I'm trying to work the last things out.

 

The dataset I am using is a bit more complex than previously mentioned as it:

  1. has data from mulitple years in it (2019 and 2020)
  2. Contains all employees
  3. Does not record weeks without hours worked

 

Problem 1 & 3 were fixed by making a few adjustments:

 

Index =
IF (
    RIGHT ( [WeekYear], 4 ) = "2019",
    LEFT ( [WeekYear], SEARCH ( "-", [WeekYear] ) - 1 ) + 0,
    LEFT ( [WeekYear], SEARCH ( "-", [WeekYear] ) - 1 ) + 53
)

 

 

 

Counter = COUNTROWS(FILTER('CompleteTable',[Index]<=EARLIER([Index])))

 

 

Do you have a sugestion on how I could adjust the 'Counter' a bit more to determine the counter for all individual 'Employee' separately included in the total database? I'm now looking into the filtering options.

@HerbertDashwood Maybe:

Counter = COUNTROWS(FILTER('CompleteTable',[Employee]=EARLIER([Employee]) && [Index]<=EARLIER([Index])))

@ me in replies or I'll lose your thread!!!
Instead of a Kudo, please vote for this idea
Become an expert!: Enterprise DNA
External Tools: MSHGQM
YouTube Channel!: Microsoft Hates Greg
Latest book!:
The Definitive Guide to Power Query (M)

DAX is easy, CALCULATE makes DAX hard...

It is now that I truly begin to understand what EARLIER () does. Code is all fixed. For future references solutions is provded below:

 

 

Counter = 
COUNTROWS(
    FILTER(
        'CompleteTable',

// Filter all rows with the EmployeeID of the current row

        EARLIER(CompleteTable[EmployeeID]) = CompleteTable[EmployeeID] 

// Filter all rows <= to the index numer of the current row

        && [Index]<=EARLIER([Index])
        )
)

 

 

Thanks again for you help!

 

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.