Percentage for each subgroup - powerbi

I have made an Matrix ("pivot table") in Power BI. The matrix have 3 columns, were the first column is groups of attributes. 2nd column is the attributs and the 3rd column is a count of number of attributes.
Below on every column, there is a total count.
I want to add an additional column to state how much % each count represent for each group.
I have tried to code this in DAX but it seems that the code only calculate the percentage of the grand total, and not for each subtotal.
%Percentage =
COUNT ( Table1[Counter_number] ) /
CALCULATE (
COUNT ( Table1[Counter_number] );
ALLEXCEPT ( Table1; 'Table2'[Type] )
)

The reason you see % of grand total instead of subtotal is the wrong column in ALLEXCEPT. Change your code to this:
%Percentage =
DIVIDE(
COUNT ( Table1[Counter_number] ),
CALCULATE (
COUNT ( Table1[Counter_number] );
ALLEXCEPT ( Table1; 'Table2'[Group] )
)
ALLEXCEPT needs to preserve "Group" filter, not "Type". Think about how "Subtotal" cell is calculated: you need to count all type per 1 Group. Hence "ALL, except GROUP".

Related

Dax Calculation on distinct count of column based on 2nd column distinct values

i need to calculate the distinct count of panel names based on distinct vehicle no's
ex: for 6073kxx vehicle -distinct count of panels are 7 . please provide a dax calculation for this. this is sample data I have more vehicle numbers.
Just create a measure to do the distinctcount on the other column.
Create a measure:
Distinct Count of Panels = DISTINCTCOUNT('your Table name here'[Vehicle No])
Try this measure
Measure =
SUMX ( VALUES ( 'Table'[Vehicle No] ), DISTINCTCOUNT ( 'Table'[panel name] ) )

How do I average a column of values while excluding rows that contain a certain value from another column? PowerBi

I am trying to write a code for a data table in Power BI that averages values of a table but categorizes them based on ID and Project but at the same time exclude a value from another column. Below is what I am trying to accomplish and column AVG is the goal. Excluding Type = "II" and averaging the values based on category columns [ID] and [Project].
Below is the code I am working on but it is incorrect. What would be the best solution?
AVG =
CALCULATE (
AVERAGEX ( FILTER ( Table, Table[Type] <> "II" ), Table[Values] ),
ALLEXCEPT ( 'Table', 'Table'[ID], 'Table'[Project] )
)
How about this?
AVG =
CALCULATE (
AVERAGE ( Table[Values] ),
ALLEXCEPT ( Table, Table[ID], Table[Project] ),
Table[Type] <> "II"
)
I don't see a reason to use an iterator function (AVERAGEX) and a simple Boolean filter should work how you want (instead of using FILTER).

CALCULATE and COUNTROWS returns only 1 - PowerBI DAX

I need to count the number of rows in a subset based on two filters. The filters work, but insted of returning the total number of of rown in my subset, I just get a 1 for every row in the subset.
I need it to return the number of rows in the subset (e.g. 200). I am currently getting a column of 1s.
It works fine without CALCULATE and filters.
Colunm = CALCULATE ( COUNTROWS ( TABLE ) ;
/* FILTER 1 */
TABLE[TEXT_FIELD] = "TEXT";
/* FILTER 2 */
DATESBETWEEN ( TABLE[DATE];
(MAX ( TABLE[DATE] ) - 30);
MAX ( TABLE[DATE] )
))
This is likely because your measure is being evaluated within its local filter context.
Try adding ALL(TABLE) as another filter to remove that filter context.

How to create a column that is the result of a division of two existing columns?

I have the following table in my PowerBI dashboard
In order to get Distinct count of patients per age range I added the patient field and set it to do Count (Distinct)
Now I need a third column that is the result of the division of the budgetfee column and the Count of patient.
How would I calculate that third column?
Create a measure using the DIVIDE function:
MyMeasure =
DIVIDE (
SUM ( MyTable[budgetfree] ),
DISTINCTCOUNT ( MyTable[patient] ),
BLANK()
)

How to calculate cumulative Total and % in DAX?

This might be very simple...
I have the below summary table in Power BI and need to build a Pareto Chart, what I'm looking for is a way to create columns "D" and "E"... Thanks in advance!
The Count from column "B" is a measure I've created in PBI based on multiple filters. I've already tried some Calculate/Sum/Filter type of expressions with no luck.
My raw data looks like Image #2... I have the measures to build the summary table with the exception of column "I" - Running % - (for which I will also need the cumulative total of events per bucket).
Unfortunately, I haven't been able to successfully apply the calculations from DAXPATTERNS.
There is a well-known pattern for cumulative calculations in the DAXPATTERNS blog.
Try this expression for Running % measure:
Running % =
CALCULATE (
SUM ( [Percentage] ),
FILTER ( ALL ( YourTable), YourTable[Bucket] <= MAX ( YourTable[Bucket] ) )
)
And try this for Cumulative count measure:
Cumulative Count =
CALCULATE (
SUM ( [Count] ),
FILTER ( ALL ( YourTable ), YourTable[Bucket] <= MAX ( YourTable[Bucket] ) )
)
Basically in each row you are summing those count or percent values that are less or equal than the bucket value in the evaluated row, which produces the cumulative total.
UPDATE: A posible solution matching your model.
Assuming your Event Count measure is defined as follows:
Event Count = COUNT(EventTable[Duration_Bucket])
You can create a cumulative count using CALCULATE function, which lets us calculate the Running % measure:
Cumulative Count =
CALCULATE (
[Event Count],
FILTER (
ALL ( EventTable ),
[Duration_Bucket] <= MAX ( EventTable[Duration_Bucket] )
)
)
Now calculate the Running % measure using:
Running % =
DIVIDE (
[Cumulative Count],
CALCULATE ( [Event Count], ALL ( EventTable ) ),
BLANK ()
)
You should get something like this in Power BI:
Table visualization
Bar chart visualization
Note my expressions use an EventTable which you should replace by the name of your table. Also note the running % line starts from 0 to 1 and there is only one Y-axis to the left.
Let me know if this helps.