Power BI Rank within Matrix Visual - powerbi

I've spent many weeks trying to get this to work including a posting at another site. No luck. Not sure what I'm missing.
The following code gets me the table of data that I need. However, I want just the "rank" value and I want it for each row within a matrix visual.
First some background, the PowerBI page has slicers on dates and "base" product. Any given product may be sold in multiple countries. All carry the same base product suffix with a country-specific prefix.
The matrix in question displays several aspects of all the product variants for the selected base product. I want to show where each variant ranks (unit sales) within its country.
VAR tblCountrySales =
SUMMARIZE (
FILTER(
tblSalesUnits,
RELATED(tblProducts[Country])="US"
),
tblProducts[ProdID],
"Total Sales", SUM ( tblSalesUnits[Units] )
)
RETURN
ADDCOLUMNS (
tblCountrySales,
"ProductRank", RANKX ( tblCountrySales, [Total Sales] )
)
Is there a way to either pull just the rank for a single product from the above code, or, better yet, a DAX pattern that will get me the rank by unit sales of each product variant within its respective country?
I've use these statements to successfully get the product ID and country for each row within the visual.
VAR ProdSales = SUM(tblSales[Units])
VAR ProdCountry = SELECTEDVALUE(tblProducts[Country])
Any help is greatly appreciated. I assume I'm missing something with row context within the visual as a simple COUNTROWS on the table expressions above always returns "1". Being new to PowerBI and DAX I'm still trying to grasp the nuances of filter and row contexts.
The products table contains data like...
ProdID.....BaseID....Name...Country
7190...........7190.....xxxx.....US
150207190......7190.....XXXX....Panama
241807190......7190.....xxxx.....Spain
The sales table contains data like...
ProdID......SalesMonth......Units.....USD
7190........July 2010.......4563....$23491.00
150207190...July 2010.......2543....$16235.00
241807190...July 2010.......1263....$8125.00
There is a dates table as well that links the SalesMonth to the usual selection of date display formats. The two tables above are linked via ProdID, though the visuals are sliced by the BaseID. I'd attach a picture, but don't have permissions to do so.

With some more tinkering I found this solution.
Country Rank =
VAR ProdSales = SUM(tblSales[Units])
VAR ProdCountry = SELECTEDVALUE(tblProducts[Country])
VAR tblCountrySales =
SUMMARIZE (
FILTER(
ALLEXCEPT(tblProducts,tblProducts[BaseID]),
tblProducts[Country]=ProdCountry
),
tblProducts[ProdID],
"Total Sales", SUM ( tblSales[Units] )
)
RETURN
IF(ProdSales>0,COUNTROWS(FILTER(tblCountrySales,[Total Sales]>ProdSales))+1,BLANK())

Related

Can't do a cumulative sum with legends in a stacked chart (Power Bi)

I am new to Power Bi. My manager asked me to illustrate the growth of the company with a stacked chart.
All is fine when i don't add legends to it (the different stores). But when i do this happens :
Broken chart
I want the orange part (Omega company) to also appear in 2022.(even though they made no benefits for that year)
What measure do I need to make this happen ?
Any help would be much appreciated.
Here's a simplified caption of what the excel file for the power bi report looks like.
Thank you !!
Excel sample
You can use the Quick measure "Running Total" to achieve that, but you need a separate "Dates" table that you can build as a calculated table via
Dates = GROUPBY('Table','Table'[Date])
and that you need to relate 1-to-many to your original table.
Now in the Quick measures UI for "Running total" you set the Base value to "Sum of Benefits" and the Field to "Date - Year"
With that measure "Benefits running total in Year" and your Excel sample data from above your Stacked column chart looks like this:
What does your measure look like?
If you have a relationship between your fact table and date dimension table, you can try something like this
Running Total Benefits =
CALCULATE (
SUM ( 'Fact Table'[Benefits] ),
ALL ( 'Date Table' ),
'Date Table'[Date] <= EARLIER ( 'Date Table'[Date] )
)
Or use DATESBETWEEN function if they don't have relationship

PowerBI - How to show only 10 categories and wrap the rest on "Others"

I have a customer service ticket list and need to build a report with it, one of the charts must be a "Qty per motive" and the problem is that I have too many motive to show on a chart, so I want to show let's say 10 of them and wrap the rest on a "Others" categories.
I've found some posts showing hot to do this with values like Sales, but I couldn't figure out how to make it work with a count on my data.
the structure of the data is like
ClientID | Ticket ID | Date | Motive | Description
In the posts that I've found the solution involves a SUM() and then sorting by the SUM() column, but I don't have a value column, I need to count per motive
There are a couple of ways to doing that.
New group
use built-in PBI feature that you have to manually set up. Right-click on a column name in the Fields section and chose New group. A new window will pop up. There, you have to choose the categories you want to label as others. A new field (Column name (groups)) will appear in your table. This method requires you to decide arbitrarily which columns you want to be displayed.
Create calculated table
You could create a brand new Calculated table in your model that is going to set new labels based on the total quantity. In that case, the labels will dynamically change rather than being static. If new motive comes to the model and its quantity will be in TOP N then it will be visible on the chart. This solution creates a separate table that you have to connect via relationship with your main table later.
Select Calculated table on the ribbon and then write DAX:
TopNCategories =
VAR keepLabels = 3
VAR tbl =
ADDCOLUMNS(
VALUES( 'Product'[Brand] ),
"#TotalSales", CALCULATE( [Sales Amount] )
)
VAR addRank =
ADDCOLUMNS(
tbl,
"#Rank",
RANKX(
tbl,
[#TotalSales],
,
DESC
)
)
VAR result =
SELECTCOLUMNS(
addRank,
"BrandKey", 'Product'[Brand],
"NewLabel", IF( [#Rank] <= keepLabels, 'Product'[Brand], "Others" )
)
RETURN
result
Calculated column
If you don't want to create another instance in your model, you can use the above logic to create a set of Calculated column in your main table instead. Depends on your model size and cardinality, this solution may have an impact on the performance of the whole report.

The PeriodCustomer column is not found or cannot be used in this expression. Power BI (DAX)

I am writing code to determine the Running Total Customers: All Customers Until This Period. Now, I have created a This period's Customers table to contain the following code:
PeriodeKlant = DISTINCTCOUNT(Text[PCHN])
I have now created the code as below:
Running Total Customers =
var _currdate=MAX(Tekst[Datum].[Date])
var _salesthisperiod=Tekst[Verkoopdoc]
return
if(_salesthisperiod>0,
CALCULATE(
[PeriodeKlant],
FILTER(
ALLSELECTED(Tekst[Datum].[Date]),
ISONORAFTER(Tekst[Datum].[Date], _currdate, DESC)
)
)
)
I get the message that the previously created column cannot be used, this column has the value integer and at summary it says sum. I don't know if that is why I would not be able to load this data?
But because of this error I can't run my measure.
The way your DAX is written, it's expecting [PeriodeKlant] to be a measure rather than a calculated column. Try deleting this calculating this column and defining it as a measure instead.

need assistance\ideas on building what is hopefully a simple DAX measure

I am trying to figure out the DAX to create a new measure in an SSAS Tabular data model. An example of what I am trying to do is more easily shown than described. My SSAS Tabular dataset produces the following table. Cols A and B are from the stores table, Col C is a measure from the Sales table, Col D is a measure from the Products table, and Col E is C/D. This all works fine. Data has been mocked up in Excel to protect the innocent, but it is working in Power BI.
What I would like to do is add a new measure which calculates the Sales/Product at the state level and have that measure show for each store in that state, as shown below
Presumably I have to iterate over all rows and calculate the total sales/state and total products sold/state and divide those 2 to get the answer, but can't work out the DAX to get there. I have tried numerous combinations of
calculate(
sumx(...),
filter(
all(...),
...
)
)
to no avail.
You should use FILTER with ALL to manipulate a context(remove current context);
MesureSumStateLevel = calculate(SUM('Table'[Amount]),
FILTER(ALL('StoreStateTab'), 'StoreStateTab'[State] =
SELECTEDVALUE('StoreStateTab'[State])))
https://dax.guide/filter/
https://dax.guide/selectedvalue/
https://dax.guide/all/
Thanks for the tip. I originally tried that and dropped it because I couldn't get it working. I revisited this morning and solved it. Here is what I did:
State Ttl =
var trxYr = convert(SELECTEDVALUE(dim_date[Year]), INTEGER) //needed because Year is stored as text in the model
var trxMo = SELECTEDVALUE(dim_Date[Short Month Name])
var trxState = SELECTEDVALUE(fact_Sales[state])
Return
CALCULATE(
SUM(fact_sales[SalesAmt])
,all(fact_sales)
,year(fact_sales[SaleDATE]) = trxYr
,dim_Date[Short Month Name] = trxMo
,dim_Stores[state] = trxState
)

New Customers after rows filtering

i have used the DAX code below in Power BI (DAX patterns 2015), which works fine to get NEW CUSTOMERS.
Customer is counted as NEW either if he uses Product_1 or Product_2.
Now I would like to create the same, but this time related to each product separately. So NEW CUSTOMER PRODUCT_1 measure would be if the client started to use Product_1 for the first time (without any regard to Product_2). In the data model FACT table I have Column with Product 1 and 2, if in one month was used only Product 2, then the Product 1 is blank in the same row and vice versa.
I tried to add
Filter(MAIN;MAIN([Product_1]>0))
but it gave me strange results. The new clients repeated whole year, instead of being counted only once.
Absolute_NEW_Customers(PRODUCT_1) =
COUNTROWS (
FILTER(
ADDCOLUMNS(VALUES(MAIN[Customer No]);"PreviousSales";
CALCULATE(COUNTROWS(MAIN);FILTER(ALL('DateKey');DateKey[Date]<MIN('DateKey'[Date]))));
[PreviousSales]=0))
I have finally found the function that helped. It was necessary to use the "CALCULATETABLE" function by which I filtered the whole table. Below is the final code:
Absolute_NEW_Customers(Product_1) =
COUNTROWS (
FILTER(
ADDCOLUMNS(
CALCULATETABLE(VALUES(MAIN[Customer No]);FILTER(MAIN;NOT ISBLANK(MAIN[Product_1])));"PreviousSales";
CALCULATE(COUNTROWS(CALCULATETABLE(MAIN;FILTER(MAIN;NOT ISBLANK(MAIN[Product_1]))));FILTER(ALL('DateKey');DateKey[Date]<MIN('DateKey'[Date]))));
[PreviousSales]=0))