简体   繁体   中英

Power BI Retrieve Last Column Value by Group

I'm working in a dashboard that control some kpis at my company. Now, I need to compare every result with the previous one, according to employee number. In the sample below, I show a little exemple of my data, and the expected result in the last column (previous score). I've tryed to solve that using a lot os calculated columns. I got close using the following:

PreviousScore = 
VAR EMPNUMBER = BASE[Employee Number]
VAR REF = BASE[Score]

RETURN
CALCULATE(LASTNONBLANK(BASE[Employee Number];EMPNUMBER);FILTER(BASE;BASE[Employee Number]=EMPNUMBER);FILTER(BASE;BASE[Date]<EARLIER(BASE[Date])))

Employee Number Date Score Previous score

1234 01/01/2019 1 BLANK

1235 01/01/2019 4 BLANK

1236 01/01/2019 2 BLANK

1288 01/01/2019 0 BLANK

1259 01/01/2019 0 BLANK

1234 01/02/2019 3 1

1235 01/02/2019 4 4

1236 01/02/2019 1 2

1288 01/02/2019 2 0

1259 01/02/2019 4 0

1234 01/03/2019 1 3

1235 01/03/2019 2 4

1236 01/03/2019 3 1

1288 01/03/2019 0 2

1259 01/03/2019 1 4

1234 01/04/2019 2 1

1235 01/04/2019 3 2

1236 01/04/2019 8 3

1288 01/04/2019 BLANK 0

1259 01/04/2019 BLANK 1

I hope someone could help with this issue.

LW

You have no dependance between your dates, you pickup the earlier column and by accident they are in the right order.

The following query will give you the right result:

Previous score = 
    var empNR = score[Employee Number]
    var theDate = score[Date]
    var empData = FILTER(score;score[Employee Number] = empNR)
    var prevDate = CALCULATE(MAX(score[Date]);empData;score[Date] < theDate)
    return CALCULATE(MAX(score[Score]);FILTER(empData; score[Date] = prevDate))
  1. I filter on emp number to get the data of that employee
  2. I get the prevDate by getting the max of all dates which are lower than selected date
  3. last step is to get the one row and return the score

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM