I'm trying to group the far right column based on the (top 5/ all the rest) of the rank column. I need a result where I can display a chart that shows the aggregated 'number of sales' for the (top 5/all the rest)
Here is an example:
I'm using RANKX to rank based on the 'Units sold', and using an if statement to segment into a yes/no.
How do I aggregate this further so I can find the total units sold for YES and total units sold for NO? I need to keep this in the pivottable. I'm thinking of adding a measure that can be used as a filter or aggregation but I can't add measures to filter. I just started with DAX and I might be missing something.
If you just need a straight list of items with a total at the bottom, you could do this:
assume the table is called 'data':
--
Calculated Columns:
isTop5 = if(rankx(all(data),data[units])<=5,1,0) -- top five gets a 1, everything else 0
--
Measures:
numTopFive:=sum([isTop5]) -- add up your top fives (maybe your population only had 4 lines?)
numNotTop5:=count([code])-[numTopFive] -- subtract number of top 5s from total population count
--
-However-
If you try to subgroup this (i.e. group your codes into regions), then the example I provided will not show five top-5s for each subgroup, it will only show the sum of top 5s if a particular code so happened to be in the top 5 of the entire population. hopefully that makes sense. If you need this using sub-grouping I can provide a different example.
Related
So this is a one I have been cracking my head because I know there has to be a way.
I have a Top 10 Country column ranked based on sales. Unfortunately in my data source there were null values for countries, no value was defining it, and in order to fix those null values I replaced them with a string Unknown. The idea was to create Other category and everything not in the top 5 sum in there and finally have a total of all the values.
I started with ranking column defining,
Country Sales Rank = RANKX(
ALL (Country[Country Name]),
CALCULATE([Sales], ALLEXCEPT(Country,Country[Country Name])), ,DESC
The problem is when adding the rank to the countries the Unknown sales are top 4.
Unknown in the top 5
I also tried categorizing the countires in Other and then grouping them; one group for top 5 another Other group for the rest but the Unknown still remains in the top 4.
Country Top 5=
IF(
Country[Country Sales Rank] IN {1,2,3,4,5}, Country[Country Name], "Others")
Ideally this is what I am going for:
End result
Did anyone come across a similar issue I am having? Thanks!
I am trying to get my head around DAX and am struggling. I have a PowerBI Matrix in which I need to calculate the average of a measure. The measure is '% of population' and on the surface it appears to work exactly as expected.
It calculates correctly in the top matrix for the two levels and also summarises correctly in the bottom table.
As an example, I have highlighted in red the order of calculations for "A3"
For the record the % population is set to
% of Population = sum(Data[Value])/sum('Level'[Population])
The problem occurs when I filter on the Country and only select Country 2...
Country 2 does not have data for "D13". Although the Values sum up correctly (170), the Sum of the Population includes the 300 from the missing D13 row making a total of 600 and the '% population' of 28.33% (instead of 170 / 300 = 57%)
I am happy to turn off the group totals in the top grid so that the 28.33 does not show; so my real problem is actually with the bottom grid.
I think I need a new measure to be displayed in the bottom grid. I think it simply needs to sum up the values and divide by the sum of the populations - but only when the value is present. How do I do that?
Or am I totally on the wrong track and there is an obvious answer that I am missing?
The PowerBI file can be downloaded from here
Thanks in advance.
The reason this is happening is that the Country table does not filter the Level table in the relationship diagram since they both only filter one way to the Data table and there are no other relationships.
Without changing your data model, one way to fix this in DAX is to specify that you only want to count Population where Level[LevelId] matches a Data[SecondLevelId] in your current filter context.
Population =
DIVIDE (
SUM ( Data[Value] ),
CALCULATE (
SUM ( 'Level'[Population] ),
'Level'[LevelId] IN VALUES ( Data[SecondLevelId] )
)
)
I've started to manage PowerBi from a couple of weeks so i'm a little bit confused about some things.
My problem is that i need a Matrix in my dashboard with percent values but i want the total in number value because the total of a percent of row shows me always 100% and i dont know about the number i'm working
This is my Matrix with percentage values
This is how i want the total of row returns me but with the columns values ins percentage
I've tried to make a measure counting the values
COUNT(OPSRespuestas[answer])
After that turn off the total of rows and add this measure to the values in my matrix but this is what i get
This is my table after trying add a measure with the total
It returns me the total for each of the columns and not the total of all my rows.
These are the tables i'm working with
This my top header values
This is my left header values
The answer column is what i need to count
This is my relationship between this 3 tables although i have many more intermediate table aside from this 3 as you're going to see in the next picture:
My relationship tables
So finally what i need is that this matrix shows me the total of answer in percentage for each of departments and group of questions and then show me total by department but with number value
The problem you are experiencing has to do with context. Each row is seen as it own total population hence the 100% total. Each column in this row is evaluated against the total of that row to provide a percentage value.
In addition to adding a custom measure to replace the total, you could also consider computing a percentage against the grand total of all dimensions. This means that each cell gets evaluated against the the total of all rows and columns. In this ways the cell value would change compared to your first table but the row total does not evaluate to 100% anymore.
SUM ( [Value] ) / CALCULATE ( SUM ( [Value] ) ; ALL ( 'Your Table' ) )
I need a calculated column for champions and runner up. To become a champion, a team must attain 4 wins in a given year in the final round. I just don't know how to translate that for DAX. I want to be able to have a Year Slicer that will show the Champion and Runner Up for a given year in a card.
I have tried a summary table and using TOPN, but since the count of finalists are all one, I am getting no luck. In the picture below, I would like a column delegating if a team is a champion or runner up
Create a new column by clicking in the Modeling tab and selecting 'New Column". Using the code below I created a new column which takes the column which contains the 'Count of Champions' and IF the number is greater than or equal to 4 then "Champion" is placed in the adjacent column, if the number is below 4, then "Runner Up" is placed in the adjacent column.
Status = IF(Table1[Count of Champions] >= 4, "Champion", "Runner Up")
I am learning to use Power Pivot and write DAX and I am working through a book I bought written by Rob Collie and Avichal Singh called "Power Pivot and Power BI".
Early on it explains what happens when using CALCULATE() in a measure. A key point of understanding is explained as follows:
If a filter argument acts on a column that IS already on the pivot, it
will override the pivot context for that column
So in a simple table called "GSR" I have a series of invoices with an invoice date, a product and an amount. I have another column that converts all invoice dates to the last day of the month to gather them together. I have created a measure called "Total orders" that is counting the number of rows.
I have created a pivot of this data with:
Products in the rows
Month end in the columns (but I've actually chosen Month and Year from the automatic breakdown Power Pivot has done on my Month_End column)
Orders in the values
This pivot renders correctly.
Now the issue:
I've created two slicers that are feeding off two disconnected tables; one containing month numbers, and one containing years. Based on the selection from each of these I have a measure that creates a month end using the EOMONTH(DATE(),0). This measure is called "Comparison_Month_End".
I then have another measure called "Compare_Orders" that contains the following:
CALCULATE([Total orders],FILTER(GSR,GSR[Month_End]=[Comparison_Month_End]))
The point of all of this - this is meant to get the orders from the GSR where the month and year match the slicer selection. I want this value to appear in the pivot for every month selected i.e. not filtered by the pivot.
It doesn't work, however. This seems to me to be counter to what the book says, which is that if the filter in the measure is applied to a column on the pivot (in this case Month_End), the pivot filter is overriden. So, for example, if I have 31-Mar-18 in a column on my pivot and the comparison month end is 31-Jan-18, I would expect to see the January orders within "Compare_Orders" sitting next to the March orders, the February orders, the December orders and so on, but it only appears next to January.
To me this is doing exactly the same as an example in the book where there is a pivot with Year in the rows, Total Sales in column 1 and then 2002 sales in column 2. Column 1 shows sales for each year, column 2 shows the same sales figure (i.e. 2002 sales) against every row, even where the row year is 2001, 2003 or 2004. The 2002 Sales measure is using CALCULATE() summing Total Sales, filtering on "Sales[Year] = 2002".
Could anyone please explain why what I expected to happen doesn't seem to be happening please?
Thank you.
CALCULATE does indeed override the filter context when evaluating its first argument; using the rows provided to its second argument as the new filter context. However; CALCULATE does not alter the filter context when evaluating its second argument, and so pre-existing filters remain unless they are explicitly removed.
In FILTER(GSR,GSR[Month_End]=[Comparison_Month_End]), FILTER only iterates over rows of GSR that are present in the pre-existing filter context (the pivot context). To get all rows, use ALL:
CALCULATE([Total orders],FILTER(ALL(GSR),GSR[Month_End]=[Comparison_Month_End]))