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))
I filter on emp number to get the data of that employee
I get the prevDate by getting the max of all dates which are lower
than selected date
last step is to get the one row and return the score
Related
I have the below table. I need to group them base on product and increment group number when set = 1 but returns back to 1 if new product is in next line. I have created an index already.
Index
Product
Set
1
Table
0
2
Table
0
3
Table
1
4
Table
0
5
Table
0
6
Table
1
7
Table
0
8
Table
1
9
Chair
0
10
Chair
0
11
Chair
0
12
Chair
1
13
Chair
0
14
Chair
0
15
Chair
1
Here's the result I'm after:
Index
Product
Set
Group
1
Table
0
1
2
Table
0
1
3
Table
1
1
4
Table
0
2
5
Table
0
2
6
Table
1
2
7
Table
0
3
8
Table
1
3
9
Chair
0
1
10
Chair
0
1
11
Chair
0
1
12
Chair
1
1
13
Chair
0
2
14
Chair
0
2
15
Chair
1
2
With this
Grouping=
RANKX (
FILTER (
'fact',
'fact'[Set] <> 0
&& EARLIER ( 'fact'[Product] ) = 'fact'[Product]
),
'fact'[Index],
,
ASC
Need help in creating measures that will reflect the actual count of rows in the table when filtered.
Example:
ID
RankC
RankA
Avg Diff
RankC_count
RankA_count
Avg Diff_count
1000
AAA
XYZ
+01.00 to +01.25
5
6
4
1001
AAA
ZY1
+01.5.00 to +01.75
5
1
5
1002
AAB
XYZ
+01.5.00 to +01.75
3
6
5
1003
AAB
ZY2
+01.5.00 to +01.75
3
1
5
1004
AAB
XYZ
+01.00 to +01.25
3
6
4
1005
AAA
XYZ
+01.00 to +01.25
5
6
4
1006
AAA
ZY3
+01.00 to +01.25
5
1
4
1007
AAC
XYZ
+01.25.00 to +01.5
1
6
2
1008
AAA
ZY4
+01.25.00 to +01.5
5
2
2
1009
AAZ
ZY4
+01.5.00 to +01.75
1
2
5
1010
ABY
XYZ
+01.5.00 to +01.75
1
6
5
The last 3 columns represent the count of each entry. If I use the measure such as below, it provides the correct count. However, when I use in the visual, filtering by ID, say ID 1000, I want it to show line 1 with 5,6, and 4 on the counts, instead of all 1.
Questions:
Is there any measure to give me the correct result? say summarize the table first then do a lookup?
is creating a column the only choice? I cannot create columns since I need 1000 of these calculated columns. whereas using measure, I can create 1000 in one go.
Thanks for any help.
AverageDiff_Count =
CALCULATE (
COUNTROWS (
FILTER ( '28Jun_1973', [Average Diff] = '28Jun_1973'[Average Diff] )
)
)
The ALL function is useful here. It removes filter context so that it uses the whole table instead of just the part in the current filter context.
AvgDiff_Count =
VAR CurrAvgDiff = SELECTEDVALUE ( '28Jun_1973'[Avg Diff] )
RETURN
COUNTROWS (
FILTER ( ALL ( '28Jun_1973' ), '28Jun_1973'[Avg Diff] = CurrAvgDiff )
)
I am trying to create a calculated column in power BI called most recent score that gives me the most recent score for each employee.
Employee Number Date Score Most recent score
1234 01/01/2019 1 1
1235 01/01/2019 4 2
1236 01/01/2019 2 3
1288 01/01/2019 0 0
1259 01/01/2019 0 1
1234 01/02/2019 3 1
1235 01/02/2019 4 2
1236 01/02/2019 1 3
1288 01/02/2019 2 0
1259 01/02/2019 4 1
1234 01/03/2019 1 1
1235 01/03/2019 2 2
1236 01/03/2019 3 3
1288 01/03/2019 0 0
1259 01/03/2019 1 1
1234 01/04/2019 BLANK 1
1235 01/04/2019 BLANK 2
1236 01/04/2019 BLANK 3
1288 01/04/2019 BLANK 0
1259 01/04/2019 BLANK 1
I am using the below measure which seems to work unless the most recent score is a "0" in which case it pulls through the most recent non "0" score.
Most Recent Score =
VAR MRSM = Master[Employee ID]
RETURN
CALCULATE (
LASTNONBLANK ( Master[Score], Master[Score] ),
FILTER ( Master, Master[Employee ID] = MRSM )
)
Any help would be appreciated
EDITED ANSWER
This seems to do what you need.
Most Recent Score =
VAR EmpID = 'Master'[Employee ID]
VAR tblScores =
FILTER ('Master', 'Master'[Employee ID] = EmpID && NOT ( ISBLANK ( 'Master'[Score] ) )
)
VAR mrsDate = CALCULATE ( MAX ( [Date] ), tblScores )
RETURN
CALCULATE ( MAX ( 'Master'[Score] ), FILTER ( tblScores, 'Master'[Date] = mrsDate )
)
How do I rank my data based on only a specific set of value in category? Say if there are category 2, 4, and 5 based on same client/date, then 5 should trump 4 and 2, 4 should trump 2, 5 should trump 2, etc. Other than that, any other category should always rank it as 1. (for sake of simplicity, values have been modified). So, any category of (2/4/5), check trumping logic. Any other category, rank it as 1. Please see below for desired results. Thanks!
want
ClientName Date Category Age rank
A 1/1/2018 1 25 1
A 1/1/2018 2 25 3
A 1/1/2018 3 25 1
A 1/1/2018 4 25 2
A 1/1/2018 5 25 1
A 2/1/2018 2 25 1
B 3/1/2018 1 26 1
B 3/1/2018 3 26 1
I tried
proc rank data=test out=test2 ties=low descending;
by clientname date;
var category;
ranks rank;
run;
In a Panda's data frame, I'd like combine all 'other' rows from col_2 into a one row for each value from col_1 by assigning col_3 the sum of all corresponding values.
EDIT - Clarification: In total, I have about 20 columns (where values in those columns is unique for each col_1. there however 80,000 other fields; however, there are three columns affecting my question
Current dataframe df:
col_1 col_2 col_3
1 a 30
1 b 25
1 other 1
1 other 5
2 a 321
2 b 1
2 other 45
2 other 52
2 other 17
2 other 8
Desired resultin :
col_1 col_2 col_3
1 a 30
1 b 25
1 other 6
2 a 321
2 b 1
2 other 122
How can I do this in Pandas?
You can groupby on col_1 and col_2 and call sum and then reset_index:
In [188]:
df.groupby(['col_1','col_2']).sum().reset_index()
Out[188]:
col_1 col_2 col_3
0 1 a 30
1 1 b 25
2 1 other 6
3 2 a 321
4 2 b 1
5 2 other 122