Including groups with no values in stacked column chart - powerbi

I'm building a chart that will cumulatively sum Invoice Values for the next month, broken out by category of sale. It looks like this:
My problem is that for particular slicer values, there might not be any invoices for a particular category, and thus the groups just don't show in the graph:
This looks scrappy, so I'm trying to force them to show. Given the rows simply don't exist the way I'm trying to do this is to have a new table which has a row per-date-per-category, and use a measure to cumulatively sum all the data from my source table. So for example given this source table:
I've built the structure of this table, but I need to find a way to add the "Cumulative Value" field that's also shown:
Unfortunately I can't work out how to make that work. The usual cumulative sum syntax would be:
Cumulative Value = (
CALCULATE(
SUM('Table 1'[Value]),
FILTER(ALLSELECTED('Table 1'), ISONORAFTER('Table 1'[Date], MAX('Table 1'[Date]), DESC))
)
)
And I can't seem to add in another filter expression without either
Breaking it such that it returns different values per category but the same value for every date
Breaking it such that it returns different values per date but the same value for each category
So; what Measure can I build to create that "Cumulative Value" field?

Never mind, I got it. Full DAX for the answer was:
CumulativeValue =
VAR CurrActionDate = MAX('Table 2'[Date])
VAR CurrTransType = MAX('Table 2'[Category])
RETURN (
CALCULATE(
SUM('Table 1'[Value],
FILTER(
ALLSELECTED('Table 1'),
'Table 1'[Date] <= CurrActionDate
),
FILTER(
ALLSELECTED('Table 1'),
'Table 1'[Category] = CurrTransType
)
)
)
Ta-da! Cumulative sum across different groups with no blank values.

Related

How to produce a snapshot table using Power BI measure

My intention is to populate days of the month to simulate a data warehouse periodic snapshot table using DAX measures. My goal is to show non-additive values for the quantity.
Consider the following transactions:
The granularity of my snapshot table is day. So it should show the following:
Take note that a day may have multiple entries but I am only interested in the latest entry for the day. If I am looking at the figures using a week period it should show the latest entry for the week. It all depends on the context fixter.
However after applying the measure I end up with:
There are three transactions. Two on day 2 and the other on day 4. Instead of calculating a running total I want to show the latest Qty for the days which have no transactions without running accumulating totals. So, day 4 should show 4 instead of summing up day 3 and day 4 which gives me 10. I've been experimenting with LASTNONBLANK without much success.
This is the measure I'm using:
Snapshot =
CALCULATE(
SUM('Inventory'[Quantity]),
FILTER(
ALL ( 'Date'[Date] ),
'Date'[Date] <= MAX( 'Date'[Date] )
)
)
There are two tables involved:
Table # 1: Inventory table containing the transactions. It includes the product id, the date/time the transaction was recorded and the quantity.
Table # 2: A date table 'Date' which has been marked as a date table in Power BI. There is a relationship between the Inventory and the Date table based on a date key. So, in the measure, 'Date'[Date] refers to the Date column in the Date table.
You can use the LASTNONBLANKVALUE function, that returns the last value of the expression specified as second parameter sorted by the column specified as first parameter.
Since LASTNONBLANKVALUE implicitly wraps the second parameter into a CALCULATE, a context transition happens and therefore the row context is transformed into the corresponding filter context. So we also need to use VALUES to apply the filter context to the T[Qty] column. The returned table is a single row column and DAX can automatically convert a single column, single row table to a scalar value.
Then, since we don't have a dimension table we have to get rid of cross-filtering, therefore we must use REMOVEFILTERS over the whole table.
the filter expression T[Day] < MaxDay is needed because LASTNONBLANKVALUE must be called in a filter context containing all the rows preceding and including the current one.
So, assuming that the table name is T with fields Day and Qty like in your sample data, this code should work
Edit: changed in order to support multiple rows with same day, assuming the desired result is the sum of the last day quantities
Measure =
VAR MaxDay =
MAX ( T[Day] )
RETURN
CALCULATE (
LASTNONBLANKVALUE (
T[Day],
SUM ( T[Qty] )
),
T[Day] <= MaxDay,
REMOVEFILTERS ( T )
) + 0
Edit: after reading the comments, this might work on your model (untested)
Measure =
VAR MaxDay =
MAX ( 'Date'[Date] )
RETURN
CALCULATE (
LASTNONBLANKVALUE (
Inventory[RecordedDate],
SUM ( Inventory[Quantity] )
),
'Date'[Date] <= MaxDay
) + 0

DAX DEFINE returns column not found error

This works:
EVALUATE
ADDCOLUMNS(
FILTER (Sales, [StoreKey] = 155 ) ,
"Margin", (Sales[SalesAmount] - Sales[TotalCost])
)
However, if I try to define a function, I get an error:
DEFINE VAR Margin = Sales[SalesAmount] - Sales[TotalCost]
EVALUATE
ADDCOLUMNS(
FILTER (Sales, [StoreKey] = 155) ,
"Margin", Margin
)
Query (1, 21) A single value for column 'SalesAmount' in table 'Sales'
cannot be determined. This can happen when a measure formula refers to
a column that contains many values without specifying an aggregation
such as min, max, count, or sum to get a single result.
What is this error and how to fix it?
There are 2 problems with the second code:
It's missing Row Context
VAR is not a function, it's a variable. Functions in DAX are Measures
What is "Row Context"? In short, in Power BI data is stored in a database. When you are referring to a column in the database, you must either: aggregate the data in it (i.e., sum it), or provide a specific row ("row context").
In your first code, function ADDCOLUMNS is an iterator. It means that it loops a table row by row, and for each record does a calculation. Since during each iteration it "knows" which row it's on, you can refer to the table fields without problems.
In the second code, this line:
Margin = Sales[SalesAmount] - Sales[TotalCost]
has no row context - it does not know which record to use for the calculation, and hence the error. You must either aggregate the data first, or put this calculation inside an iterator.
In this particular case, the simplest solution is to use aggregation:
DEFINE
MEASURE 'Sales'Margin = SUM ( Sales[SalesAmount] ) - SUM ( Sales[TotalCost] )
EVALUATE
ADDCOLUMNS (
CALCULATETABLE ( Sales, Sales[StoreKey] = 155 ),
"Margin", [Margin]
)
Here, we first aggregate amounts and costs and calculate margin. Then, inside of ADDCOLUMNS we iterate table Sales filtered for a specific store, and for each row we call the measure defined above.
If you need to use an iterator instead of aggregation, you can do something like this:
DEFINE
MEASURE 'Sales'Margin = SUMX(Sales, Sales[SalesAmount] - Sales[TotalCost] )
EVALUATE
ADDCOLUMNS (
CALCULATETABLE ( Sales, Sales[StoreKey] = 155 ),
"Margin", [Margin]
)

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?

How to Show last period (Fiscal Year) of sale, based on multi-filters report

I tried create a dashboard based on fiscal year, with more Filters, like region, sales rep name, ...
Example files avaliable on dropbox:
https://www.dropbox.com/sh/l25kdz6enmg35yb/AABPuOk3kKOpfQdKDfRUcnX2a?dl=0
On my closest attempt, i tried this follow:
Add one column on my data set, naming each period as distinct number, like: "17";"18";"19", due to deslocated fiscal year (april to march).
Then create a measure:
PREVIOUS CROP_YEAR = SWITCH(TRUE();
SELECTEDVALUE('dataset'[Crop-X])=16;(CALCULATE(SUM('dataset'[Order Value]);ALL('dataset')));
SELECTEDVALUE('dataset'[Crop-X])=17;(CALCULATE(SUM('dataset'[Order Value]);ALL('dataset')));
SELECTEDVALUE('dataset'[Crop-X])=18;(CALCULATE(SUM('dataset'[Order Value]);ALL('dataset')));
SELECTEDVALUE('dataset'[Crop-X])=19;(CALCULATE(SUM('dataset'[Order Value]);ALL('dataset')));
0)
Expected output was:
Values based on all filters applied, But instead i just get an empty charts
The measure is return the total because you are explicitly asking for it by using the ALL function. This removes all the filters from the dataset thus returning a grand total. This can work but it creates a complexity in your dataset with respect of having two time dimensions. The way to solve this is to first make sure you filter the date correctly with respect to both dimensions
PREVIOUS YEAR =
CALCULATE(
SUM('dataset'[Order Value]);
FILTER(
ALL ( 'dataset' ) ;
AND (
'dataset'[Crop-X] = MAX('dataset'[Crop-X]) -1 ;
'dataset'[YEAR] = MAX('dataset'[YEAR] ) -1
)
)
)
Furthermore, this measure still uses the ALL function which means any other filters get ignored. Using ALLSELECTED instead would result in the relative time filtering to result in nothing as soon as you select any time based slicer in your dashboard, this prevents the filter from looking at any other part of the dataset that is not within the primary sliced dataset. The workaround would be to use ALLEXCEPT and add the filters you want to be able to use as arguments. Downside is that any filter you add to your dashboard will have to be added to the exception manually.
PREVIOUS YEAR =
CALCULATE(
SUM('dataset'[Order Value]);
FILTER(
ALLEXCEPT( 'dataset' ; Dim1[Group] ; Dim1[Manager] ; Dim1[Region] ) ;
AND (
'dataset'[Crop-X] = MAX('dataset'[Crop-X]) -1 ;
'dataset'[YEAR] = MAX('dataset'[YEAR] ) -1
)
)
)

DAX - Histogram for relative frequencies and total column

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.