ranking one column grouped by another column - powerbi

I am trying to add a calculated column which sums up per account no. Then I am trying to rank these as per account group separately.
First I tried to create the Total per Account column as
sub_total =
sumx(filter(all('Mizan Full'),'Mizan Full'[Account No] = EARLIER('Mizan Full'[Account No])),'Mizan Full'[Total])
this one is ok but :
then how can i give the ranks column as grouped by another column ?
rankx(allselected('Mizan Full'),'Mizan Full'[sub_total],,0)
or
RANKX(filter(('Mizan Full'), 'Mizan Full'[Account Group]= EARLIER('Mizan Full'[Account Group])), 'Mizan Full'[sub_total],,,Dense)
what am i doing wrong ?
Account Group
Account No
Total
sub_total
Rank per Group
120
120.01.C8716
-15.924,00
2.890.730,00
1
120
120.01.C8716
2.906.654,00
2.890.730,00
1
120
120.01.C8717
-290.733,00
-274.809,00
2
120
120.01.C8717
15.924,00
-274.809,00
2
159
159.01.003
108.475,00
108.475,00
1
335
335.01.C16805
3.238.202,00
3.238.202,00
1
335
335.01.C16808
2.154.455,00
2.154.455,00
2
335
335.01.C16807
290.733,00
290.733,00
3
335
335.01.C16806
2.154.455,00
-
4
335
335.01.C16806
-2.154.455,00
-
4
335
335.01.C15052
-1.500,00
-1.500,00
5
335
335.01.C16809
-70.911,00
-70.911,00
6
335
335.01.C16810
-70.911,00
-70.911,00
6
335
335.01.C16811
-89.000,00
-89.000,00
7
393
393.02.002
-70.911,00
-70.911,00
1
393
393.02.001
-70.911,00
-191.284,00
2
393
393.02.001
-70.911,00
-191.284,00
2
393
393.02.001
-70.911,00
-191.284,00
2
393
393.02.001
15.924,00
-191.284,00
2
393
393.02.001
5.525,00
-191.284,00
2
Edit :
I am attaching the sample file. I think I am almost there but I dont understand why
under group 335 - zero value must get rank 4 but it gets rank 8 and rank 4 is missing
under group 120 - listed as rank 2 and rank 4 but it should be 1 & 2
under group 393 - listed as rank 1 and rank 10 ???
Sample File

I solved it by adding a new table and measure but i am sure there is a better way...
Table :
MeasureTablo =
SUMMARIZECOLUMNS (
'Table'[Account Group ],
'Table'[Account No ],
"Toplam", SUM ( 'Table'[Total ] )
Measure :
Sira No =
RANKX (
FILTER (
ALLSELECTED ( MeasureTablo ),
MeasureTablo[Account Group ] = EARLIER ( MeasureTablo[Account Group ] )
),
( MeasureTablo[Toplam] ),
,
,
DENSE
)

Related

DAX - Rankx by multiple Categories Issue

I have 4 Categories (GP, ID, Age, Date). I would would like to create calculated column and group by GP, ID, and Age and Rank/ count by Date to see how many months each member has in past 24 month.
My Code works until I have members who cancelled their membership for a few months and then resumed after. I need to restart from the first month after skip. for example :
GP ID AGE DATE RKING Desired RANK
1 220 35-44 202206 12 6
1 220 35-44 202205 12 5
1 220 35-44 202204 12 4
1 220 35-44 202203 12 3
1 220 35-44 202202 12 2
1 220 35-44 202201 12 1
1 220 35-44 202012 24 24
1 220 35-44 202011 23 23
1 220 35-44 202010 22 22
1 220 35-44 202009 21 21
1 220 35-44 202008 20 20
1 220 35-44 202007 19 19
1 220 35-44 202006 18 18
1 220 35-44 202005 17 17
1 220 35-44 202004 16 16
… … … … … …
1 220 35-44 201901 1 1
This is what I have tried but doesn't work for dates skipping.
RKING Column=
RANKX (
CALCULATETABLE (
VALUES ('tbl'[Date] ),
ALLEXCEPT ( 'tblW', 'tbl'[GP], 'tbl'[ID] ),
'tbl'[AGE] = 'tbl'[AGE],
'tbl'[date] >= start_date && 'tbl'[date] <= end_date // date slicer
),
[Date] ,
,ASC
)
Looking through the code you were trying to make a measure for a visual (For a calcCol the measure is added as well). And as I got a point, you want to show a sum of consequtive months in a matrix for each date in accordance to ID/GP/AGE/DATE I see a following way.
As you know, calculations performs for each row in a matrix and filter the data model according to data presented in matrix rows and columns (slicers as well). So, my idea is -
Get date from matrixRow and use it as max date for the table.
Then use a FILTER(). FILTER() is an iterative function, so it goes throw each row and checks filtering condition - if true row remains if false - not.
I use following filtring conditions:
Get dateInMatrix-dateInACurrentTableRow (for example: 202203-202201= 2 months)
Then check how many rows in the table with min=202201 and max<202203
if there are less rows then date difference then it FALSE() and the row is out of table.
3) The last step is counting of rows it a filtered table.
A measure for matrix:
Ranking =
VAR matrixDate=MAX('table'[DATE])
VAR filteredTable =
FILTER(
ALL('table')
,DATEDIFF(
DATE(LEFT([DATE],4),RIGHT([DATE],2),1)
,DATE(LEFT(matrixDate,4),RIGHT(matrixDate,2),1)
,MONTH
)
=
VAR dateInRow=[DATE]
RETURN
CALCULATE(
COUNTROWS('table')
,'table'[DATE]>=dateInRow
,'table'[DATE]<matrixDate
)
)
RETURN
COUNTROWS(filteredTable)
[![enter image description here][1]][1]
A measure for calcColl:
RankColl =
VAR currentDate=[Start_Date]
Var MyFilt={('Table'[AGE],'Table'[ID],'Table'[GROUP])}
VAR withColl =
ADDCOLUMNS(
CALCULATETABLE(
'table'
,ALL('Table')
,TREATAS(MyFilt,'Table'[AGE],'Table'[ID],'Table'[GROUP])
)
,"dateDiff",
DATEDIFF(
[Start_Date]
,currentDate
,MONTH
)
,"RowsInTable",
VAR dateInRow=[Start_Date]
Var startDate=IF(dateInRow<currentDate,dateInRow,currentDate)
VAR endDay =IF(dateInRow>currentDate,dateInRow,currentDate)
VAR myDates = GENERATESERIES(startDate,endDay,1)
RETURN
COUNTROWS(
CALCULATETABLE(
'Table'
,ALL('Table')
,TREATAS(MyFilt,'Table'[AGE],'Table'[ID],'Table'[GROUP])
,TREATAS(myDates,'Table'[Start_Date])
)
)
)
VAR filtered =
FILTER(
withColl
,[dateDiff]=[RowsInTable]-1 -- for ex.:
-- dateDiff=01/01/2022-01/01/2022=0,
-- but it will be 1 row in the table for 01/01/2022
)
RETURN
CountRows( filtered)

How to create a column based on grouped condition?

My test tabe in powerbi:
IdRecord
Date
Value
1
2022-04-25 23:45:00.000
100
1
2022-04-24 18:07:00.000
344
2
2022-05-01 23:45:00.000
5
2
2022-05-02 18:07:00.000
66
2
2022-05-03 18:07:00.000
31
I require to create a calculated column to mark the earliest of the records grouped by id.
Desired output
IdRecord
Date
Value
IsFirst
1
2022-04-25 23:45:00.000
100
0
1
2022-04-24 18:07:00.000
344
1
2
2022-05-01 23:45:00.000
5
1
2
2022-05-02 18:07:00.000
66
0
2
2022-05-03 18:07:00.000
31
0
Answering to myself
FirstRes= VAR MYMIN = CALCULATE(
MIN(Table[Date]),
FILTER ( Table, Table[IdRecord] = EARLIER(Table[IdRecord]))
)
RETURN
IF(CALCULATE(
MIN(MIN(Table[Date]),MYMIN),
FILTER ( Table, Table[IdRecord] = EARLIER ( Table[IdRecord] ) )
) = Table[Date],1,0)

create a countif measure not impacted by filter in visual in powerBI

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 )
)

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))
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

Return latest value present in power BI

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 )
)