Slicer for a measure in Power BI - powerbi

How to add a slicer with measure as a field. If not possible, what is the work around for that?
I have a table with data as follows:
City Name Count
x a 5
y b 7
x c 6
I want to have a table as follows with a slicer if total population of a city is greater than 10 or not.
City Total population
x 11
y 7
Here total population is a measure

Add following calculated columns:
cityPopulation =
var temp = Table1[city]
return calculate(sum(Table1[count]),ALL(Table1),Table1[city]=temp)
bigCity = Table1[cityPopulation] > 10
Results in
city,cityPopulation,count,name,bigCity
x, 11, 5, a, true
x, 11, 6, c, true
y, 7, 7, b, false
Then use bigCity in a slicer.

Related

How can I calculate the GAP between two products from different tables and with a set of conditions?

I'm relatively new around the world of Power BI. I've got two different types of diesel, each of them with different prices.
I've also got calculated Moving Averages of both, and I need to see the average GAP between them but under the condition they need to have a value in the same DAY to calculate such average, otherwise it wouldn't be valid. The tables and expected result is kind of as follows:
TABLE DIESEL TYPE A
Date
Price DIESEL TYPE A
01-feb
1,2
05-may
1,3
06-ago
1,09
06-ago
1,1
07-sep
1,5
TABLE DIESEL TYPE B
Date
Price DIESEL TYPE B
01-feb
0,9
05-may
1,05
06-ago
0,8
06-ago
0,75
12-nov
0,7
Date
Average A
Average B
01-feb
1,2
0,9
05-may
1,3
1,05
06-ago
1,095
0,775
07-sep
1,5
-
12-nov
-
0,7
The expected GAP should be:
Date
GAP Average
01-feb
0,30
05-may
0,25
06-ago
0,32
07-sep
-
12-nov
-
In September 7th and November 12th I DONT want to have these averages calculated or shown on my graph, i.e. on my measure.
Getting an average of the difference between these two prices by date and under the condition there should be values for the same date in both type of diesels, otherwise I don't want to calculate such average, if for instance, there's a value 07-sep for Type A but no for Type B, and viceversa.
Use this measure:
GAP Average =
VAR avgA =
AVERAGE('DIESEL TYPE A'[Price DIESEL TYPE A])
VAR avgB =
AVERAGE('DIESEL TYPE B'[Price DIESEL TYPE B])
RETURN
IF(
OR(ISBLANK(avgA), ISBLANK(avgB)),
BLANK(),
avgA - avgB
)

Calculate aggregate value of last 12 months in a Measure Power BI

I'm using this measure to calculate a aggregate sum of a value in the last 12 months. The measure is working well if I start using it from the month 12. But, the problem is, if the month is not in the 12 or higher, the value is not right.
Example, if you are in the first month of the sample, I would like to multiply this value by 12 (1st month + 11 months). If it was the second month, I'd like you to average the two months and multiply it by 12. And so on.
could you please help me?
SumRevenue =
var vSumNet12 =
CALCULATE(
Table[Trevenue],
DATESINPERIOD(
CalendarM[Data],
MAX(CalendarM[Data]),
-12,
MONTH
)
)
return
vSumNet12
Example table:
Date Customer Net Trevenue SumRevenue ROA ROA I Want
09/30/20 A 237767115,6 327444,2478 327444,2478 0,14% 1,65%
10/31/20 A 245689276,3 251934,78 579379,0278 0,24% 1,41%
11/30/20 A 252916933,6 262294,89 841673,9178 0,33% 1,33%
12/31/20 A 241424127 509883,07 1351556,988 0,56% 1,68%
01/31/21 A 244721140,9 259250 1610806,988 0,66% 1,58%
02/28/21 A 250913741,4 246740,33 1857547,318 0,74% 1,48%
03/31/21 A 282215365,7 550897,35 2408444,668 0,85% 1,46%
04/30/21 A 312759343,1 544161,63 2952606,298 0,94% 1,42%
05/31/21 A 325535894 419360,97 3371967,268 1,04% 1,38%
06/30/21 A 371306315 390650,41 3762617,678 1,01% 1,22%
07/31/21 A 379780645,3 527254,43 4289872,108 1,13% 1,23%
08/31/21 A 415390274,9 409196,3 4699068,408 1,13% 1,13%
09/30/21 A 433837730,6 598924,02 4970548,18 1,15% 1,15%
10/31/21 A 482659906,7 254086,32 4972699,72 1,03% 1,03%
11/30/21 A 501568104,7 318924,53 5029329,36 1,00% 1,00%
12/31/21 A 507124350,5 754897,79 5274344,08 1,04% 1,04%
01/31/22 A 510220304,2 179153,11 5194247,19 1,02% 1,02%

Power Bi show data with multiple conditions

I am trying to help a public school here, but I have very limited knowledge in Power Bi so I hope your guys could enlight me on this case:
we have a very simple report with a table and a kpi
Kpi counts all students
table shows studants grades
Student Math Portuguese History Science
StD A 6 6 7 8
StD B 6 7 6 7
StD C 8 9 7 8
StD D 6 6 6 6
StD E 6 7 8 8
StD F 8 6 7 7
the rule that must be applied to the kpi (count(Students)) and to the table is to show studenst only if:
at least 2 subjects are equal or under 6
portuguese is equal or under 6
math is under 6
all the rest should not be showed in the table or counted in the KPI. In this case I would see/count only students A, B, D,E & F
any help would be very appreciated
To tackle your task try the following:
Create a calculated column in your table with the following DAX code:
isValid =
VAR cond_2_subjects = (('Table'[Math] <= 6 ) + ('Table'[Portuguese] <= 6) + ('Table'[History] <= 6) + ('Table'[Science] <= 6)) >= 2
VAR cond_portuguese = 'Table'[Portuguese] <= 6
VAR cond_math = 'Table'[Math] < 6
RETURN
-- This will check if any of the given conditions is true
IF(
cond_2_subjects || cond_portuguese || cond_math,
TRUE(),
FALSE()
)
The table should then look like this:
The KPI (measure) can then be written like so:
# Students =
CALCULATE(
COUNT('Table'[Student]),
-- only count Students where conditions are true (calculated column isValid = True)
'Table'[isValid] = TRUE()
)
The final result should then look like this:
The table on the left has specified 'Table'[isValid] = TRUE() as filter on visual

Applying Rcpp on a dataframe

I'm new to C++ and exploring faster computation possibilities on R through the Rcpp package. The actual dataframe contains over ~2 million rows, and is quite slow.
Existing Dataframes
Main Dataframe
df<-data.frame(z = c("a","b","c"), a = c(303,403,503), b = c(203,103,803), c = c(903,803,703))
Cost Dataframe
cost <- data.frame("103" = 4, "203" = 5, "303" = 6, "403" = 7, "503" = 8, "603" = 9, "703" = 10, "803" = 11, "903" = 12)
colnames(cost) <- c("103", "203", "303", "403", "503", "603", "703", "803", "903")
Steps
df contains z which is a categorical variable with levels a, b and c. I had done a merge operation from another dataframe to bring in a,b,c into df with the specific nos.
First step would be to match each row in z with the column names (a,b or c) and create a new column called 'type' and copy the corresponding number.
So the first row would read,
df$z[1] = "a"
df$type[1]= 303
Now it must match df$type with column names in another dataframe called 'cost' and create df$cost. The cost dataframe contains column names as numbers e.g. "103", "203" etc.
For our example, df$cost[1] = 6. It matches df$type[1] = 303 with cost$303[1]=6
Final Dataframe should look like this - Created a sample output
df1 <- data.frame(z = c("a","b","c"), type = c("303", "103", "703"), cost = c(6,4,10))
A possible solution, not very elegant but does the job:
library(reshape2)
tmp <- cbind(cost,melt(df)) # create a unique data frame
row.idx <- which(tmp$z==tmp$variable) # row index of matching values
col.val <- match(as.character(tmp$value[row.idx]), names(tmp) ) # find corresponding values in the column names
# now put all together
df2 <- data.frame('z'=unique(df$z),
'type' = tmp$value[row.idx],
'cost' = as.numeric(tmp[1,col.val]) )
the output:
> df2
z type cost
1 a 303 6
2 b 103 4
3 c 703 10
see if it works

Adding elements in a row corresponding to a column

I'm extracting data from a CSV with 5 rows and 5 columns.
For example
print("Year Age Scholarship Academic Stipend")
print("1982 20 $20000.00 $1000.00")
print("1983 21 $25000.00 NA")
print("1984 22 $30000.00 $500.00")
print("1982 20 $16000.00 $200.00")
print("1983 21 $17500.00 $600.00")
I extracted individual lists with all these elements:
Year = [1982,1983,1984,1982,1983]
Age = [20,21,22,20,21]
Scholarship = [20000, 25000, 30000, 16000, 17500]
Stipend_Amount = [1000, NA, 500, 200, 600]
I want to group all my years together. How do I add the corresponding elements in column 4, corresponding only to the elements in Year?
For example. I want to be able to print
#Year Total_Scholarship_Granted
#1982 36000.00
But my for loop below is just adding all the elements together:
Start_Fund = 0
for i in range(len(year)):
Start_Fund += Scholarship[i]
print(year[i],Start_Fund)
#1982 108500
I want my results to be:
1982 36000
(which is acquired by adding all amounts from 1982)
You are missing an if statement, to check if the year in the loop is the year you want (in your case 1982). So your pseydocode should look like this:
Start_Fund = 0
my_year=1982
for i in range(len(year)):
if (year[i]==my_year)
Start_Fund += Scholarship[i]
print(my_year,Start_Fund)