Custom aggregate column in power bi matrix - powerbi

I'm trying to create a matrix in a Power BI report summarizing Salesperson performance sliced in a number of different ways.
I know how to create a matrix with Rows - Salesperson, Columns - Product Type, and Values - count of Sales which will show the number of Sales per Salesperson per Product Type, but I'd like also be able to do the following:
Add an additional column set to pivot on (e.g. Sales Year), so that I could see count of Sales pivoted by both Product Type and Year in the same table side by side (i.e., not nested).
Add additional summary columns to my matrix showing values such as average Sale Amount by Salesperson, % of total number of Sales by Salesperson.
For clarity, I'd imagine that this would result in a matrix where the column headers read: Salesperson, Product 1, Product 2, ..., Year 1, Year 2, ..., Total Sales Count, Average Sales Amount, % of Total Sales Count. See image link below (I don't have the reputation points to include the actual image yet)
I recognize that I can do this by creating measures which effectively replicate how the matrix is splitting out the values and adding each measure as a value (no Columns), but I don't want to have to create new measures and update the matrix every year or every time we add a new Product Type.
I've also looked at custom visuals on the Power BI marketplace, but didn't see any that would achieve this.

It's possible to do this, but not super easy. You'll need a measure with a SWITCH as well as a table for your headers.
You can create a header table along these lines:
Header =
UNION (
SUMMARIZE ( Sales, Sales[Product], "Group", "By Product", "Index", 1 ),
SUMMARIZE ( Sales, Sales[Year], "Group", "By Year", "Index", 2 ),
DATATABLE (
"Header", STRING,
"Group", STRING,
"Index", INTEGER,
{
{ " Total", "Summarizations", 3 },
{ "% of Total Sales", "Summarizations", 3 },
{ "Avg Sale Size", "Summarizations", 3 }
}
)
)
Which will look like this:
Header, Group, Index,
Product 1, By Product, 1,
Product 2, By Product, 1,
2016, By Year, 2,
2017, By Year, 2,
2018, By Year, 2,
Total, Summarizations, 3,
% of Total Sales, Summarizations, 3,
Avg Sale Size, Summarizations, 3
This table will automatically expand when more products or years are added.
(Note: The Index column is so I can order them properly using Sort by Column.)
Once you have that, you just need to put Group and Header on the columns of a matrix visual and Salesperson on the rows, with a switching measure in the values.
Measure =
VAR Val =
SWITCH (
SELECTEDVALUE ( Header[Group] ),
"By Product", CALCULATE (
SUM ( Sales[Amount] ),
FILTER ( Sales, Sales[Product] = MAX ( Header[Header] ) )
),
"By Year", CALCULATE (
SUM ( Sales[Amount] ),
FILTER ( Sales, Sales[Year] = VALUE ( MAX ( Header[Header] ) ) )
),
SWITCH (
SELECTEDVALUE ( Header[Header] ),
"% of Total Sales", DIVIDE (
SUM ( Sales[Amount] ),
CALCULATE ( SUM ( Sales[Amount] ), ALL ( Sales ) )
),
"Avg Sale Size", AVERAGE ( Sales[Amount] ),
SUM ( Sales[Amount] )
)
)
RETURN
IF (
SELECTEDVALUE ( Header[Header] ) = "% of Total Sales",
FORMAT ( Val, "0.0%" ),
FORMAT ( Val, "0.0" )
)
Each different group gets its own calculation and we have to use the FORMAT function to force the table to format the percentage function properly.
(Note: If you have slicers or filtering, you probably want to use ALLSELECTED where I used ALL above.)
Here's what my table looks like (not the exact same data but similar structure)
and here's the PBIX file I created for this:
https://drive.google.com/file/d/1qxc5p53MgmOm-NH3EcivkZLhLeEHpr4R/

Related

Rolling Year: Actual And Last Year

I have the following fields:
Year
Category
Maker
Month
Month Number
Sales Volume
Sales
Date
So, I have in my dash a filter for "Month Number" and "Year":
My goal is to create two new measure; first with the Rolling Year that need to sum 12 months, ending in the moment that the user select in the mencioned filters. For example if y select Year 2021 and Month 01. The Rolling Year need to sum the sales of a selected category since 2020-02 to 2021-01 (thats mean always 12 months since a pivot month).
For thesecond is exactly the same, a measure called Rolling Last Year, it need to be a rolling sum too, but for the last period in order to compare. Taking the same example if I have the period 2020-02 to 2021-01. The Rolling Last Year for the last period should be 2019-02 to 2020-01.
I tried with this DAX code, that extracted from Microsoft page but without success:
Rolling Year =
CALCULATE (
SUMX ( Table, Table[Sales] ),
FILTER (
ALL (Table[Date] ),
AND (
Table[Date] <= MAX ( Table[Date] ),
DATEADD ( Table[Date], 12, MONTH ) > MAX ( Table[Date] ))))
I share you an extract of my base:
Thanks in advance !!!
Based on the table and code you have shared, it is unclear from where the date filters are being applied. In case you have not done it, I strongly suggest to delete the [Month] and [Month Number] field from your Sales table and keep them in a separate Calendar table, from where the filters should be selected. Then, a simple tweak on you current formula should do the trick:
Rolling Year =
CALCULATE (
SUMX ( Table, Table[Sales] ),
FILTER (
ALL ('Calendar'[Date] ),
AND (
Table[Date] <= MAX ( 'Calendar'[Date] ),
DATEADD ( 'Calendar'[Date], 12, MONTH ) > MAX ( 'Calendar'[Date] ))))
However you can try with this variation for the code, a little bit optimized so as not to scan your whole Sales table each time:
Rolling Year =
VAR EndSelectedDate = MAX ( 'Calendar'[Date] )
VAR StartSelectedDate =
CALCULATE (
MAX ( 'Calendar'[Date] ),
ALL ( 'Calendar'[Year] ),
'Calendar'[Year]
= MAX ( 'Calendar'[Year] ) - 1
)
RETURN
CALCULATE (
SUM ( Table[Sales] ),
ALL ( 'Calendar' ),
'Calendar'[Date] <= EndSelectedDate,
'Calendar'[Date] > StartSelectedDate
)

Group by date, Distinct and ignore other filters - Power BI

I have the dataset where the values in the col 'value' are repeated per month and id, e.g. for 1/1/2020 and id 1, the value is 0.5, for 2/1/2020, the value is 2, etc. The dataset has other columns which are to be used as filters for other calculations.
What will be the measure to get:
so that even when I use filters from the table, e.g. filter1, the value remains grouped by date ONLY?
I've tried with sumx and max; sum and value but nothing gives a result and calculation still reacts on other filters.
When i spoke abount ALL i had in mind this kind of solution:
WithoutExternalFilter =
CALCULATE (
VAR __dist =
ADDCOLUMNS (
SUMMARIZE ( Te, Te[Date], Te[ID] ),
"val", CALCULATE ( MAX ( Te[value] ) )
)
RETURN
SUMX ( __dist, [val] ),
ALL ( Te[filter1] )
)
WHEN we put some filter, value for "2020-08-01" is still 1.1:

Calculating average from measures

This one should be quite easy, but I can't find a way to solve it...
A very simple setup: Three tables: products, sales and budgets.
Budget has four columns from source: ProductID,StartOfperiod,BudgetNumber.
Then I've added a calculated column, "Results" which calculate saleresults by product, month and year:
Results =
CALCULATE (
SUM ( sales[Numbers] );
FILTER ( sales; sales[ProductID] = Budget[ProductID] );
FILTER ( sales; MONTH ( sales[DateofSale] ) = MONTH ( Budget[StartOfperiod] ) );
FILTER ( sales; YEAR ( sales[DateofSale] ) = YEAR ( Budget[StartOfperiod] ) )
)
Works fine.
Then I added a measure for percentage of sales/budget:
SalesPercentage =
DIVIDE ( SUM ( Budget[Results] ); SUM ( Budget[BudgetNumber] ); 0 )
This also shows up fine.
I calculate for four products only, so when put in a matrix this gives a nice table showing sales results for these products.
But then I want to calculate the arithmetic average of SalesPercentage and put that value on i.e. a card. But how can I do that? All the necessary data are available in the budget table, but I can't find a way to just calculate the average of the SalesPercentage column, which is a measure. Anyone who knows how to?
Regards,
John Martin

Is there a way to create measure using variable to return last month Base size in power BI or DAX

I'm creating a report in Power BI, and want to return the last month Size.
I have a table with 4 columns named as Name, Size, Connections, Disconnections. The values on these columns are for the last 12 months. For example, Name column has A, B, C; Size column has 3608445, 2839945,874434; Connections column has 66875,85632,19237 and Disconnections column has 52658,61529 and 15832 values. These values are for the last 12 months. See screenshot below.
The code I used to created the expected table is
last_month_size =
VAR current_month =
MONTH ( TODAY () )
RETURN
CALCULATE (
[Size],
FILTER (
'Monthly Calendar_Lookup',
MONTH ( 'Monthly Calendar_Lookup'[Dates] ) = current_month - 1
)
)
I want to create a measure that will return last month Size column but the Connections and Disconnections remains the same. For example, the Size value changes while the connections and disconnections values remains the last 12 month values.
I find it difficult because the columns are on the same table.
I have researched about the question I have posted and I have found a solution.
This solution to the problem is creating measures and not using variables.
First, I created a measure called Total Size
Total Size = Sum ( Tablename [Size] )
Then, created another measure called prev_month size using DATEADD function with number_of_intervals as 0
prev_month size = CALCULATE ( [Total Size], DATEADD ('Monthly Calendar_Lookup'[Dates], 0, MONTH ) )
Next, I created measures of total connections and total disconnections
Total Connections = Sum ( Tablename [Connections] )
Total Disconnections = Sum ( Tablename [Disconnections] )
Also, I created two measures of rolling 12 months Connections and Disconnections each.
Rolling_Connections_12_months =
CALCULATE ( SUMX ('Tablename', [Total Connections] ),
DATESINPERIOD ('Date'[Month], LASTDATE ( 'Date'[Month] ), -12, MONTH ) )
Rolling_Disconnections_12_months =
CALCULATE ( SUMX ('Tablename', [Total Disconnections] ),
DATESINPERIOD ('Date'[Month], LASTDATE ( 'Date'[Month] ), -12, MONTH ) )
Drag the Name, prev_month size, Rolling_Connections_12_months, and Rolling_Disconnections_12_months on the canvas as a table visualization.
Then finally, I drag a relative Date slicer and set it as Last 1 Month.
This produces the expected results

Cumulative number of entries in dates range

I'm trying to get cumulative number of unique IDs in given timeframe.
My DAX look like this:
Cumulative = CALCULATE(SUM(Data[ID]));DATESBETWEEN(Data[ack_date];DATE(YEAR(NOW());4;1);DATE(YEAR(NOW());11;30)))
There is similar measure for Year-1: [YEAR(NOW())-1]
What I want to achieve is area chart showing growing number od IDs in time comparing same periods this and previous year. When I give those measures as Values for chart and "ack_date" as its Axis what I get is values comparison month by month but not cumulative, just value for certain month.
Try this code. Adjust for year -1.
=
CALCULATE (
DISTINCTCOUNT ( Data[ID] ),
FILTER (
ALL ( Data ),
AND (
Data[Ack_date] <= MAX ( Data[Ack_date] ),
AND (
Data[Ack_date] <= DATE ( YEAR ( NOW () ), 11, 30 ),
Data[Ack_date] >= DATE ( YEAR ( NOW () ), 4, 1 )
)
)
)
)