DAX - Histogram for relative frequencies and total column - powerbi

I built a diagram that displays the relative frequencies of my clusters (on the values in the column) and the cumulated frequencies (on the values on the line).
My chart has this aspect:
I would like to add a new column to the right that is equal to the sum of all the values of the previous columns.
The relative frequencies are created using the code below:
"Frequencies UL" :=
CALCULATE (
DISTINCTCOUNT ( 'table1'[Column1] );
USERELATIONSHIP ( 'Order Cluster'[Cluster Name]; table1[tag Cluster] );
SUMMARIZE ( table1; table1[tag Cluster] )
)
I would really appreciate some help!
Thanks

Simply it was necessary to do this:
"Frequencies UL" := IF(SELECTEDVALUE('Order Cluster'[Is Total]);
CALCULATE(DISTINCTCOUNT ('table1'[Column1]); ALL('Order Cluster')); DISTINCTCOUNT('table1'[Column1]))
And this is the result I got!

I'd suggest creating a new table to use for your x-axis.
If your 'Order Cluster' table looks like this:
ClusterName Order
ClusterA 1
ClusterB 2
... ...
ClusterZ 26
You want to add a Total row to the end so try something along these lines:
NewTable = UNION('Order Cluster', {("Total", MAX('Order Cluster'[Order]) + 1)})
Use NewTable[ClusterName] for your chart's x-axis and tweak your cumulative measure to reference NewTable[Order] in the FILTER inequality.
You'll also need to adjust your frequency measure to handle the case when you have the Total cluster (use an IF or SWITCH) and make sure you're evaluating within the correct filter context. Something like this logic:
IF( MAX( NewTable[ClusterName] ) = "Total",
CALCULATE( [Frequency Calc], ALLSELECTED( table1 ) ),
CALCULATE( [Frequency Calc],
FILTER(
ALLSELECTED( table1 ),
table1[tag Cluster] = MAX( NewTable[Order] )
)
)
)
P.S. You might be better off adding the total row to your 'Order Cluster' table in the query editor instead of having another table floating around. In any case, the logic is similar; add a total row to the column you're using for your axis and adjust your measures to handle that category how you want.
A waterfall chart might be another option to consider (and doesn't need nearly as much work), though I don't know that you can include the percent info except in the tooltip.

Related

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.

DAX SUMMARIZE miss applied slicers

I have a slicer, called COUNTRY and applied to table MY_TABLE. When I calculate a measure, everything works as expected:
-- calculates distinct count only for COUNTRY = x
Some Measure = DISTINCTCOUNT('MY_TABLE'[SOME_COLUMN])
The problem is SUMMARIZE ignores slicer selection:
-- calculates distinct count accross all countries: x, y, z, etc.
Calculated Table =
RETURN SUMMARIZE(
'SOME_TABLE',
[CATEGORY],
"COUNT", DISTINCTCOUNT('SOME_TABLE'[SOME_COLUMN])
)
How to make SUMMARIZE take into account slicers?
Only Measures are "responsive", calculated tables and columns get calculated and created once, when the data are loaded.
Note that if a calculated table is used inside a measure it will behave correctly, but as you may know, a measure must return a scalar value and not a table. (ie you can use summarize inside a measure, you can then filter the obtained table and return the sum of one column)
Of course, you can filter calculated table with a slicer. If you can, go for SUMMARIZECOLUMNS because this function is better optimized then SUMMARIZE, and has arguments for filtering.
Filtering SUMMARIZECOLUMNS
If you want to stick to SUMMARIZE, you can filter your table by wrapping it with CALCULATETABLE.
Calculated Table =
CALCULATETABLE (
SUMMARIZE (
'SOME_TABLE',
[CATEGORY],
"COUNT", DISTINCTCOUNT ( 'SOME_TABLE'[SOME_COLUMN] )
),
Dim[Color]
= SELECTEDVALUE ( Slicer[SlicerValues] )
)
Should FILTER be used inside or outside of SUMMARIZE?

DAX ALLEXCEPT to sum by category of multiple dimension tables

I would like to calculate total by category. The category is in the dimension table.
Here is sample file:
DAX ALLEXCEPT total by category.pbix
I have the following model:
These are my expected results. Total by Color:
I thought I could achieve expected results by the following measure:
ALLEXCEPT_color =
CALCULATE (
[Sales],
ALLEXCEPT (
FactTable, -- surprisingly 'dim1' table in that place gives wrong results
dim1[Color]
)
)
Or alternatively using method suggested by Alberto Ferrari https://www.sqlbi.com/articles/using-allexcept-versus-all-and-values/:
ALL_VALUES_color =
CALCULATE (
[Sales],
ALL (FactTable), -- again, 'dim1' produces wrong results, has to be FactTable
VALUES ( dim1[Color] )
)
Both these measures work and return proper results. However they multiply displayed results making Cartesian product of all the dimensions. Why? How to prevent it?
I achieve expected results with measure:
Expected_Results_Color =
IF (
ISBLANK ( [Sales] ),
BLANK (),
[ALLEXCEPT_color]
)
Probably I am missing something about ALLEXCEPT function so I do not get what I want for the first shot. What is the logic behind using ALLEXCEPT function with multiple tables, especially with far off dimensions, away from the center of star schema.
What pattern to use? Here I found promising solution which looks like this:
ByCategories =
CALCULATE (
SUM ( FactTable[Sales] ),
ALLEXCEPT (
dim1,
dim1[Color]
),
ALLEXCEPT (
dim2,
dim2[Size]
),
ALLEXCEPT (
dim3,
dim3[Scent]
)
)
But as I tested it before it does not work. It does not aggregate [Sales] by dimensions but produces [Sales] as they are.
So I found out that this is the correct direction:
ByCategories =
CALCULATE (
SUM ( FactTable[Sales] ),
ALLEXCEPT (
FactTable, -- here be difference
dim1[Color],
dim2[Size],
dim3[Scent]
)
)
I speculate there might be also another way.
Measure =
var MyTableVariable =
ADDCOLUMNS (
VALUES ( dim1[color] ),
"GroupedSales", [Sales]
)
RETURN
...
If only we could retrieve single scalar value of GroupedSales from MyTableVariable and match it with appropriate color in table visual.
I would be very grateful for any further insights in calculating total for category.
This is expected behaviour.
Power BI tables will include every row for which any measure in the table does not evaluate to BLANK().
ALLEXCEPT stops the values in the id and size columns from affecting the filter context when [Sales] is computed, and so every possible value for these two columns will give the same (non-blank) result (this causes the cartesian product that you see).
For example, on the (a, black, big) row, the filter context for the measures contains:
FactTable[id] = {"a"}
dim1[color] = {"black"}
dim2[size] = {"big"}
Then CALCULATE([Sales], ALLEXCEPT(...)) removes the FactTable[id] and dim2[size] from the filter context when evaluating [Sales]; so the new filter context is just:
dim1[color] = {"black"}
[Sales] in this filter context is not BLANK(), so the row is included in the result.
The proper way to fix this is to wrap the result in an IF, as you do in your Expected_Results_Color measure, or to add a filter on [Sales] not Blank to the table in Power BI.

Calculate cumulative sum of summarized table column

I having trouble calculating the cumulative sum of a column on PowerBI.
I have a big offer table and I want to run a pareto analysis on it. Following many tutorials, I created a SUMMARIZED table by offer and a sum of their sales. So the table definition is:
summary = SUMMARIZE(big_table; big_table[offer]; "offer sales"; sum(big_table[sales]))
Many of the forums and stackoverflow answers I found have direct me to the following formula for cumulative sum on column:
cum_sales =
CALCULATE(
sum([offer_sales]);
FILTER(
ALLSELECTED(summary);
summary[offer_sales] <= max( summary[offer_sales])
)
)
However the resulting table is not correct:
What I need is simply to have the offers ordered by sales descending and then add the current row's sales amount to the previous row's sales,
So I excepted numbers closer to:
1st row: 1.5M
2nd row: 2.1M
3rd row: 2.6M and so on
But (maybe) because of my data structure and (certainly) lack of knowledge on how PowerBI works, I'm not getting the right results...
Total Amount = SUM ( 'Fact'[Amount] )
Offer Visual Cumulative =
VAR OfferSum =
ADDCOLUMNS (
ALLSELECTED ( 'Offer'[Offer] ),
"amt", [Total Amount]
)
VAR CurrentOfferAmount = [Total Amount]
VAR OffersLessThanCurrent =
FILTER (
OfferSum,
[amt] <= CurrentOfferAmount
)
RETURN
SUMX (
OffersLessThanCurrent,
[amt]
)
There's no need to pre-aggregate to a summary table. We can handle that as in the measure above.
This assumes a single fact table named 'Fact', and a table of distinct offers, 'Offer'.
Depending on what you're doing in terms of other filters on 'Offer', you may need to instead do as below:
Offer Visual Cumulative =
VAR OfferSum =
ADDCOLUMNS (
ALLSELECTED ( 'Offer'[Offer] ),
"amt", CALCULATE ( [Total Amount], ALLEXCEPT ( 'Offer', 'Offer'[Offer] ) )
)
...
The rest of the measure would be the same.
The measure is fairly self-documenting in its VARs. The first VAR, OfferSum is a table with columns ('Offer'[Offer], [amt]). This will include all offers displayed in the current visual. CurrentOfferAmount is the amount for the offer on the current row/axis label of the visual. OffersLessThanCurrent takes OfferSum and filters it. Finally, we iterate OffersLessThanCurrent and add up the amounts.
Here's a sample:

DAX Measure to determine % of parent row in column chart

I've been struggling to find a way to achieve the result of a matrix visualization in Power BI, in a line and clustered column chart.
I have tried using ALLSELECTED, ALL, ALLEXCEPT in order to ignore one filter but no matter how I try it, I'm not reaching my end goal.
CarCountPerCategory =
CALCULATE(
DISTINCTCOUNT(Table[CarID]),
ALLSELECTED(Table[Filter])
)
This still breaks down the measure to the Filter level instead of ignoring it. I am looking for something that will give me the total distinct count regardless of the Filter column. E.g. TimesReshopped 1 - 190 on all 3 columns, TimesReshopped 2 - 182 on all 3 columns of the Filter Column series. I need this as a sidestep in order to divide the distinct car count to the total distinct car count per times reshopped in order to reach the percentage that I am looking for. (the one in the matrix)
Can someone help me with some advice? Thank you.
Try this:
[Total Category Count]:=
SUMX ( SUMMARIZE ( Table, Table[TimesReshopped] ), [Count of CarID] )
I got to the bottom of this by using ALLEXCEPT and providing all the filter that I was using:
CarCountPerCategory =
CALCULATE(
DISTINCTCOUNT(Table[CarID]),
ALLEXCEPT(Table,Table[Year],Table[Month],Table[LocationName],Table[CustCode],Table[TimeReshoped])
)