Microsoft Power BI: RANKX AND AGGREGATION OF RESULT - powerbi

I am having an issues ranking a set of rows then getting the total to use as input to another calculation. I have attempted nested CALCULATE statements and intermediate table using CALCULATETABLE unsuccessfully.
Scenario is as follows:
Original table
Item Sales
A 3
B 4
C 2
D 7
E 5
Ranking top N (3)
Item Sales
D 7
E 5
B 4
TOTAL 16
In this example, I am interested in the value 16 for onward processing

Create a CALCULATED COLUMN to rank the sales.
Sale Rank = RANK.EQ(SalesData[Sales], SalesData[Sales])
Create a MEASURE to get the top 3 sales.
Top 3 Sales = CALCULATE(SUM(SalesData[Sales]), SalesData[Sale Rank] <= 3)

Related

How to create a Cumulative count in Power BI

Hello I am a beginner in Power BI and would need your help.
I have a data at my end and I need to have Cumulative count of my data based on Month.
ID
Code
DATE
GENDER
Cumulative Count
1
A
5/30/2022
M
1
2
A
5/31/2022
F
3
3
A
5/31/2022
M
3
4
A
6/1/2022
F
2
5
A
6/1/2022
F
2
6
A
6/2/2022
M
3
Cumulative Count column above is the output that I am expecting to create in Power BI. Here I have created it manually.
On 30th May total count was 1 then on 31st May count was 2 hence value is 3. Once the month ends and new month starts it should start from 0 and hence on 1st June there are 2 observation and hence total count is 2 and so on.
Try this
CALCULATE(
COUNTROWS('Table')
,ALLEXCEPT('Table','Table'[Date])
,DATESMTD('Table'[Date])
)

I would like to create a measure in powerBI that is the standard deviation of distinct count by month

I would like to create a measure in powerBI that is the standard deviation of the distinct counts of IDs by month. i.e:
for this data set:
Month | ID
1 A
1 B
1 A
2 C
2 D
2 E
3 F
3 G
The Counts would be:
Month | Distinct Count
1 2
2 3
3 2
Standard Dev = 0.47
Thank you!!!!!
Below is an iteration of the code I tried that hasn't worked:
standard dev by month = STDEVX.P(values('Table'[Reporting Date By Month Year]), DISTINCTCOUNT('Table'[ID]))
Using the table you provided named as "Table" you can use the following measure to get the standard deviation.
Standard Deviation =
var grouped_table = SUMMARIZE('Table','Table'[Month],"Distinct Count",DISTINCTCOUNT('Table'[ID]))
var st_dev = STDEVX.P(grouped_table,[Distinct Count])
return(st_dev)
The measure uses variables to make the measure more easy to read. The first table generates the group of months and the respective distinct count of IDs. The second variables calculated the standard deviation of that table by distinct count measure.

Power BI Measure: Aggregate By, Filter By, Group By, Simple Math

I am attempting to make a measure that calculates:
SUM(Num_Compliant) / SUM(Num_Asked) for a District, filtered to the current Fiscal Year (i.e. Date on or after 07/01/2018, or July 1, 2018).
District Date Section Num_Compliant Num_Asked
A 11/12/2018 I 3 8
A 1/12/2018 I 3 8
A 11/17/2018 II 1 6
A 5/18/2018 II 3 6
B 2/20/2019 I 4 8
B 4/20/2018 I 5 8
B 11/12/2018 II 6 6
B 1/12/2018 II 1 6
C 11/17/2018 I 2 8
C 5/18/2018 I 3 8
C 4/20/2018 II 5 6
With the above sample data, I expect the following results
District Numerator Denominator Value
A 4 14 0.29
B 10 14 0.71
C 2 8 0.25
I intentionally made the denominator different in district C to reflect the fact that sections are sometimes missing.
I have been trying to solve this for about a day, and am brand-new to Power BI, so I apologize if this is a very simple question.
The following measure looks like it solves your needs:
Value =
var __date = DATE(2018,7,1)
var __Numerator = CALCULATE(sum(Compliancy[Num_Compliant]),ALLEXCEPT(Compliancy,Compliancy[Distict]),Compliancy[Date]>__date)
var __Denominator = CALCULATE(sum(Compliancy[Num_asked]),ALLEXCEPT(Compliancy,Compliancy[Distict]),Compliancy[Date]>__date)
return __Numerator/__Denominator
Please note you will have to change "Compliancy" to your table name throughout the DAX formula. Also, the date is currently hard coded into the DAX, it might be best to use a date slicer so you can change the values on the fly. The measure without the date filter hard coded as is as follows:
Value =
var __Numerator = CALCULATE(sum(Compliancy[Num_Compliant]),ALLEXCEPT(Compliancy,Compliancy[Distict]))
var __Denominator = CALCULATE(sum(Compliancy[Num_asked]),ALLEXCEPT(Compliancy,Compliancy[Distict]))
return __Numerator/__Denominator
Table in Power BI:
As a side note of things to watch out for:
The measure "Value" needs to be set to the format "Decimal Place"
with 2 decimals.
The date field must be appropriately set to the
"Date" data type.

Take row wise percentages on power BI tables

I Have the following table on power BI:
For which I have to take the percentages by of each category by class, but when a show the values as percentages of the total on power BI I get the following:
Which is a column wise percentage, how can I get it this way:
If you unpivot those columns, in the query editor so that your data is shaped like this:
Class Attribute Value
A Aproved 10
A Reproved 10
B Aproved 3
B Reproved 2
C Aproved 2
C Reproved 9
D Aproved 3
D Reproved 1
Then it's as simple as putting Class on rows, Attribute on columns and setting the implicit measure for Value to Percent of row total.

Creating a calculated table by passing a measure or parameter

I have a requirement where I have a data like this,
Date Name Age
1-1-2018 A 1
2-2-2018 B 1
3-3-2018 B 1
6-6-2018 C 2
7-7-2018 B 6
I am trying to give a slicer to the user to select the required number of months from the last month.
So to do that, I am using a calculated column like this:
Month Year = DATEDIFF((Table1[Date]), TODAY(), MONTH) + 1
So that changes the data to something like this:
Date Name Age MonthYear
1-1-2018 A 1 7
2-2-2018 B 1 6
3-3-2018 B 1 5
6-6-2018 C 2 2
7-7-2018 B 6 1
The user selects the Month Year from the Slicer.
For example, when he selects 2, I want to display the last 2 months records in the table.
Expected Output:
Date Name Age
6-6-2018 C 2
7-7-2018 B 6
This works for me if I hardcode it like this:
Calculated Table = CALCULATETABLE(Table1,
FILTER(Table1, OR(Table1[MonthYear] > 2, Table1[MonthYear] = 2)))
But it fails when I try to pass the value in the place of 2 dynamically through a measure using SelectedValue function.
Calculated columns and calculated tables cannot reference a slicer value since they are only computed when you load your data.
If you want to apply this filtering to a visual, I'd suggest creating a separate table for your slicer. For example, you could use Months = GENERATESERIES(1,12) and then rename the column Months as well.
Use the Months[Months] column for your slicer and then create a measure which references it to filter your table/matrix visual.
Filter = IF(SELECTEDVALUE(Months[Months]) >= MAX(Table1[Month Year]), 1, 0)
Then use that measure in your Visual level filters box: