How to calculate cumulative Total and % in DAX? - powerbi

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.

Related

Cumulative running total based on count PowerBI measure

I'm trying to create a chart with 3 informations:
Total cases
% of total per day
cumulative % running total
The first two informations are working, but I can't make the third one work, my chart look like this
And I need it to look like this
Basically, the upper line is a cumulative sum of the lower line, the problem is that the values of the bars are just a count on my table and the lower line I made using the PowerBI function "Show as % of total"
I've tried googling but no luck with this one, tried this measure but didn't work:
Running Total MEASURE =
CALCULATE (
COUNT ( 'Primes Encerrados'[Nome Cliente] ),
FILTER (
ALL ( 'Primes Encerrados' ),
'Primes Encerrados'[Data] <= MAX ( 'Primes Encerrados'[Data] )
)
)
Use a the Quick Measure feature to get your running total formula:
Power BI will generate this formula for you:
Primes encerrados running total in Dias =
CALCULATE(
SUM('Geral'[Primes encerrados]),
FILTER(
ALLSELECTED('Geral'[Dias]),
ISONORAFTER('Geral'[Dias], MAX('Geral'[Dias]), DESC)
)
)
Add the measure to your chart and show it as % of Grand Total like you did with the first line

Power BI Dax Cumulative Line Graph

I have been trying to get a cumulative sum line onto a line chart. If this were only about a single variable (like time) it would be easy. But I need the chart to respond to selections of other dimensional data on other visualizations.
I have mocked up some data to demonstrate the problem.
I created a measure to show the cumulative amounts:
Qty Cumulative = CALCULATE (
[Qty],
FILTER (
ALL ( data ),
data[Month]
<= MAX ( data[Month] )
)
)
But it doesn't respond to selections made elsewhere on the page. So next I tried to add some context back into the measure:
Qty Cumulative 2 = CALCULATE (
[Qty],
FILTER (
ALLSELECTED( data ),
data[Month] <= MAX ( data[Month] )
&& data[Product] = SELECTEDVALUE(data[Product])
&& data[Region] = SELECTEDVALUE(data[Region])
)
)
This one responds to other selections, but it requires exactly one value for each dimension. If no value is selected (so ALL) or two or more values are selected, the Qty Cumulative 2 by Month chart is blank.
Here's a screen capture of what I build in Power BI Desktop.
How can I make add a cumulative line to a chart and still have the output relate to the selections made on the page?
All(data) clears all filters on the table, you only want to clear the filters on date, so you just need to change it to All(data[Month]):
Cumulative =
VAR selectedMonth = SELECTEDVALUE(data[Month])
RETURN CALCULATE([Qty], ALL(data[Month]), data[Month] <= selectedMonth)

How can I get DAX to return just the record with a date closest to the slicer?

I'm hoping someone can help as I've completely run out of ideas.
I'm working on performance reporting data, producing a number of visuals to summarise the most recent data. To allow users to retrospectively produce reports from previous quarters, I have added a date slicer as a way to "View data as at xxxx date".
Here's a rough representation of my data table - the due dates are in English format (dd/mm/yyyy):
The ratings are calculated in another system (based on a set of targets), so there are no calculated columns here. In reality, there are a lot more measures that report on different time periods (some weekly, some annually, etc) and there are different lags before the data is "due".
I eventually managed to get a measure that returned the latest actual:
MostRecentActual =
VAR SlicerDate = MAX ( Dates[Day] )
RETURN
CALCULATE (
SUM ( Data[Actual] ),
Data[Data due] <= SlicerDate,
LASTDATE ( Data[Data due] )
)
I'm not completely sure I've done it right but it seems to work. I'd be happier if I understood it properly, so explanations or alternatives would be welcomed.
What I'm trying to do now is a basic summary pie chart at the beginning which shows the proportion of the measures that were red, amber, green or unrated as at the date selected. So I would need it to count the number of each rating, but only one for each measure and only for the date that is closest to (but before) the slicer date, which would vary depending on the measure. So using the above three measures, if the slicer was set to 10/10/2019 (English format - dd/mm/yyyy), it would count the RAGs for Q3 2019/20 for measures A an C and for Q2 2019/20 for measure B as there is a time lag which means the data isn't ready until the end of the month. Results:- A: Amber, B: Green, C:Red.
If I were able to create the measure that counted these RAGs, I would then want to add it to a pie chart, with a legend that is "Rating", so it would split the chart up appropriately. I currently can't seem to be able to do that without it counting all dates before the slicer (not just the most recent) or somehow missing ratings from the total for reasons I don't understand.
Any help would be very gratefully received.
Many thanks
Ben
Further update. I've been working on this for a while!
I have created a COUNTAX measure to try to do what I was wanting to do. In some circumstances, it works, but not all and not in the crucial ones. My measure is:
TestCountaxpt2 =
VAR SlicerDate = MAX ( Dates[Date] )
VAR MinDiff =
MINX (
FILTER (
ALL ( Data ),
Data[Ref] IN VALUES ( Data[Ref] ) &&
Data[Data due] <= SlicerDate
),
ABS ( SlicerDate - Data[Data due] )
)
VAR thisdate =
MINX (
FILTER (
ALL ( Data ),
Data[Ref] IN VALUES ( Data[Ref] ) &&
ABS ( SlicerDate - Data[Data due] ) = MinDiff
),
Data[Data due]
)
RETURN
COUNTAX (
FILTER ( Data, Data[Data due] = thisdate && Data[Ref] IN VALUES ( Data[Ref] ) ),
Data[RAG]
)
It produces the following table for a subset of the performance measures, which looks almost ok:
Table showing the result of the TestCountaxpt2 measure:
The third column is the measure above and it seems to be counting one RAG per measure and the dates look correct as the slicer is set to 3rd January 2020. The total for column 3 confuses me. I don't know what that is counting and I don't understand why it doesn't add up to 7.
If I add in the RAG column from the data table, it goes a bit more wrong:
Same table but with RAG Rating added:
The pie chart that is produced is also wrong. It should show 2 Green, 2 Red, 2 Grey (no rating) and 1 Amber. This is what happens.......
Pie chart for the DAX measure, with RAG Rating in the legend:
I can see what it is doing, which is to work out the most recent due date to the slicer in the whole table and using that (which is 1st Jan 2020) whereas I want it to calculate this separately for each measure.
Link to PBIX:
https://drive.google.com/file/d/1RTokOjAUADGHNXvZcnCCSS3Dskgc_4Cc/view?usp=sharing
Reworking the formula to count the ratings:
RAGCount =
VAR SlicerDate =
MAX ( Dates[Day] )
RETURN
COUNTAX (
ADDCOLUMNS (
SUMMARIZE (
FILTER ( Data, Data[Data due] <= SlicerDate ),
Data[Ref],
"LastDateDue", LASTDATE ( Data[Data due] )
),
"CountRAG", CALCULATE (
COUNTA ( Data[RAG] ),
Data[Data due] = EARLIER ( [LastDateDue] )
)
),
[CountRAG]
)
Here's the table it produces:
The reason for Total = 4 for the third column is straightforward. The SelectDate is maximal over all of the Refs in the table and there are only four Refs that match that date.
To fix this and get the totals you're after, you'll need to iterate over each Ref and calculate the SlicerDate for each independently and only then do your lookups or sums.
I haven't tested this code but it should give you an idea of a direction to try:
MostRecentActual =
VAR SlicerDate = MAX ( Dates[Day] )
RETURN
SUMX (
ADDCOLUMNS (
SUMMARIZE (
FILTER ( Data, Data[Data due] <= SlicerDate ),
Data[Ref],
"LastDateDue", LASTDATE ( Data[Data due] )
),
"SumActual", CALCULATE (
SUM ( Data[Actual] ),
Data[Data due] = EARLIER ( [LastDateDue] )
)
),
[SumActual]
)
Going inside to outside,
FILTER the table to ignore any dates beyond the SlicerDate.
Calculate the LastDateDue for each Ref using SUMMARIZE.
Sum the Actual column for each Ref value using its specific LastDateDue.
Iterate over this summary table to add up SumActual across all Refs in the current scope.
Note that for 4, only the Total row in your visual will contain multiple Refs since the innermost Data table inside FILTER is not the entire Data table but only the piece visible in the local filter context.

Percentage for each subgroup

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".

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.