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

Power Query: Add column of previous value with condition

Hi all, 

 

i am having some trouble with my Power BI report. I am looking at employee data to analyze different status types, e.g. holidays, sickness. I am interested in patterns, e.g. how many sickness cases with how many sick days there were for each employee over the last 5 years. Since there are more then 50 employees this table gets rather big. 

Here are some examples of the table to get an idea what i want: 

 

1) 24/7 Shift System - not every employee works on every day, but everyday someone is working:

 Names
 DayNight
01.01.1+27+8
02.01.3+41+2
03.01.5+63+4
04.01.7+85+6
05.01.1+27+8
06.01.3+41+2

 

2) Status Data (already added are the columsn PrevStatus and NextStatus with a highlighted section of the desired outcome)

DateNameNumberStatusPrevStatusNextStatus
01.01.2023Name110001PresentnullPresent
01.01.2023Name210002PresentnullPresent
02.01.2023Name310003SicknullPresent
02.01.2023Name410004PresentnullPresent
03.01.2023Name510005HolidaynullPresent
03.01.2023Name610006PresentnullPresent
01.01.2023Name710007PresentnullPresent
01.01.2023Name810008HolidaynullPresent
02.01.2023Name110001PresentPresentSick
02.01.2023Name210002PresentPresentPresent
03.01.2023Name310003PresentSickPresent
03.01.2023Name410004PresentPresentPresent
04.01.2023Name510005PresentHolidayHoliday
04.01.2023Name610006PresentPresentPresent
04.01.2023Name710007PresentPresentPresent
04.01.2023Name810008PresentHolidayPresent
05.01.2023Name110001SickPresentPresent
05.01.2023Name210002PresentPresentHoliday
06.01.2023Name310003PresentPresentPresent
06.01.2023Name410004PresentPresentPresent
07.01.2023Name510005HolidayPresentPresent
07.01.2023Name610006PresentPresentPresent
05.01.2023Name710007PresentPresentPresent
05.01.2023Name810008PresentPresentPresent
06.01.2023Name110001PresentSicknull
06.01.2023Name210002HolidayPresentnull
07.01.2023Name310003PresentPresentnull
07.01.2023Name410004PresentPresentnull
08.01.2023Name510005PresentHolidaynull
08.01.2023Name610006PresentPresentnull
08.01.2023Name710007PresentPresentnull
08.01.2023Name810008PresentPresentnull

 

So far i did this with a calculated column in Power BI, but since my import got too big i am getting the error that there is not enough RAM. I tried some changes in the options (max. number of  parallel queries, max. RAM per query) but it didnt resolve, so i wanted to solve this by addid a column via Power Query instead of a calculated column. 

The code for the calculated column is as follows:

 

PrevStatus = 

var index = Table[Index]
var previndex = CALCULATE(max('Table'[Index]),ALLEXCEPT('Table','Table'[Number]),Table[Index]<index)

return
CALCULATE(max('Table'[Status]),ALLEXCEPT('Table','Table'[Number]),'Table'[Index]=previndex)

 

Any advice on how to do this? 

 

Best regards

Sebastian

1 ACCEPTED SOLUTION
Ahmedx
Super User
Super User

2 REPLIES 2
seppel123
Frequent Visitor

Perfect, that worked! Thank you so much. 
Interesting way with the grouping, i will look into that

Ahmedx
Super User
Super User

pls try M Code
Screenshot_2.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.