I am quite new to Power BI and now facing a problem, illustrated below.
Here
**DATA**
LEVEL | PROJECT NAME| BUDGET_TYPE | BUDGET_CODE | BUDGET_AMOUNT
1 xxxx A 0000001 4,800,000
1 xxxx A 0000002 4,300,000
1 xxxx A 0000002 900,000
1 A 0000003 1,300,000
1 A 0000004 4,780,000
1 A 0000010 3,900,000
1 A 0000010 3,900,000
1 A 0000011 200,000
1 A 0000015 1,028,165
1 A 0000015 1,028,165
1 B 0000016 83,000,000
1 B 0000017 83,000,000
1 B 0000017 28,200,000
1 B 0000018 15,000,000
1 B 0000019 4,800,000
1 B 0000020 7,000,000
1 B 0000020 7,000,000
PIVOT TABLE from Excel
Row Labels | Max of BUDGET_AMOUNT
LEVEL: 1 83,000,000
TYPE:A 4,800,000
0000001 4,800,000
0000002 4,300,000
0000003 1,300,000
0000004 4,780,000
0000010 3,900,000
0000011 200,000
0000015 1,028,165 **20,308,165**
TYPE:B 83,000,000
0000016 83,000,000
0000017 83,000,000
0000018 15,000,000
0000019 4,800,000
0000020 7,000,000 **192,800,000**
Grand Total 83,000,000
I have raw data and PIVOT TABLE, respectively. What I want is to find MAX amount of each CODE first.
Then, SUM those MAX values only with distinct CODE.
I have attached the PIVOT TABLE for simplifying my problem. What I really want at the end is the SUM amount which is
LEVEL 1
TYPE A: 20,308,165
TYPE B: 192,800,000
Is there any way I can do that? Please help. Thanks!
You can do a measure that summarizes the different codes, calculates the max for each code, and sums up this table row-by-row:
Sum :=
VAR _tbl =
SUMMARIZE (
'Table' ,
'Table'[Level] ,
'Table'[Budget Code] ,
"Max Budget" , CALCULATE ( MAX ( 'Table'[Budget] ) )
)
RETURN
SUMX ( _tbl , [Max Budget] )
See minimal data example here:
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'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
Proc SQL Version=9.4. No windows functions to use.
There are client id, time period(month), amount and corresponding class.
client_id data_period amount class
1 200801 30000 2
2 200801 17000 1
3 200801 9000 1
1 200802 30000 2
2 200802 55555 2
3 200802 11000 2
Threshold amount = 20 000.
amount > 20k gives class = 2, amount <= 20k makes class = 1
client_id = 1, amount and class are the same for 200801 and 200802.
client_id = 2, amount gets higher from 17k to 55.5k, class change is correct, from 1 to 2.
client_id =3, amount changed within the same class 1 (<20K), but class changed incorrectly.
Desired result is
client_id oldDate newDate AmtOld AmtNew ClassOld ClassNew Good Bad
2 200801 200802 17000 55555 1 2 1 0
3 200801 200802 9000 11000 1 1 0 1
I tried to applied self join to get all the differences btw data periods, but there are too many rows in output. Data below is not from example above, real numbers.
client_id oldDate newDate AmtOld AmtNew ClassOld ClassNew
A001687463 200808 200802 -5613 1690386 I03 I04
A001687463 200807 200802 -5613 1690386 I03 I04
A001687463 200806 200802 -5613 1690386 I03 I04
A001687463 200805 200802 -5613 1690386 I03 I04
PROC SQL;
CREATE TABLE WORK.'Q'n AS
SELECT distinct
t1.client_id, t1.data_period as oldDate, t2.data_period as newDate, t1.amount as expAmtOld, t2.amount as expAmtNew, t1.class as classOld, t2.class as classNew
FROM WORK.'E'n t1, WORK.'E'n t2
where
t1.client_id = t2.client_id and
t1.amount <> t2.amount
order by t1.client_id;
Do not attempt to do sequential processing using SQL. It is not built for that.
It should be easy to do in a data step. For example let's convert your printout into an actual SAS dataset so we have something to code with.
data have ;
input client_id data_period amount class ;
cards;
1 200801 30000 2
2 200801 17000 1
3 200801 9000 1
1 200802 30000 2
2 200802 55555 2
3 200802 11000 2
;
And let's sort it by client and period.
proc sort data=have ;
by client_id data_period ;
run;
Now just set the data and use the LAG() function to get the previous values.
Not sure what you definition of GOOD and BAD were so I just created new class variables based on your rule of 20K.
data want ;
set have ;
by client_id;
old_period = lag(data_period);
old_class = lag(class);
newclass = 1 + (amount > 20000) ;
old_newclass = lag(newclass);
if first.client_id then call missing(of old_:);
bad = (class ne newclass) or (old_newclass ne old_class) ;
run;
So here are the results.
client_ data_ old_ old_ old_
id period amount class period class newclass newclass bad
1 200801 30000 2 . . 2 . 0
1 200802 30000 2 200801 2 2 2 0
2 200801 17000 1 . . 1 . 0
2 200802 55555 2 200801 1 2 1 0
3 200801 9000 1 . . 1 . 0
3 200802 11000 2 200801 1 1 1 1
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;