New Customers after rows filtering - powerbi

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))

Related

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.

Filter by last not blank date in Power BI

I have data from multiple countries on a monthly basis. Since the updates are not regular, I want to set up filter to visuals, so they would show the last month for which I have data from all the countries. I have data from each country loaded into a separate dataset, which then are merged into one big. Is there an easy way to place such filter? I managed to use "LASTDATE" function in each of country sets to find which date is last, but if I try to filter with that measure, I simply get nothing in a result. Thanks!
Well, this feels a little clunky to me but I believe it will work for you. There are two steps. The first is to create a summary table that reads through your data and counts the number of distinct countries that you have in each month. This will be a new table in your model, so go into the modeling tab, click 'New Table' and add this DAX. Obviously, correct for your table and column names.
SUMMARIZED_ROWS = SUMMARIZE(
'Table1'
,Table1[Month]
,"CountOfCountries"
,DISTINCTCOUNT(Table1[Country])
)
Now add a measure to the table (or anywhere) like this:
MonthWithMostCountries = CALCULATE(
LASTNONBLANK(SUMMARIZED_ROWS[Month], 1 )
, FILTER(SUMMARIZED_ROWS, SUMMARIZED_ROWS[CountOfCountries] = MAX(SUMMARIZED_ROWS[CountOfCountries]) ) )
This is going to give you the month where you have the most distinct countries in your data. You'll want to look at it in a card or similarly isolated visual as it is a measure and can be affected by filter context.
So, on the left is my mock data - 3 countries, 3 months each with a 1 month stagger. On the right you see the result of the Summarize table. Then the measure showing the final result.
Hope it helps.

Power BI Rank within Matrix Visual

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())

Ignore report level filter in a measure

I've read numerous posts about this, it still doesn't work for me. My scenario is as below -
I have a Date dimension (DimDate) and a Fact (StockAvailability). There are several visuals on the page and a page level filter that shows data for the last 7 days. However, for one of the visuals, I want the data to be shown for last 3 months, i.e., not limited by 7 days report level filter.
So, I created a measure, clearing up the filter on the StockAvailability using ALL, but that results in a single value for all the dates -
Overall Stock Availability = CALCULATE(AVERAGE([Availability], ALL('StockAvailability'))
Further, I try to re-add the 3-months filter above, but still nothing - I get distinct values this time, but only 7 days worth
Overall Stock Availability =
CALCULATE(AVERAGE([Availability]),
ALL('StockAvailability'),
FILTER(DimDate, [IsLast3Months] = true))
How can I Ignore the 7 days report level filter for this given measure?
Any inputs will be greatly appreciated!
Thanks
It's an old question but in case it's still unresolved. I've just had a very similar issue.
I'm assuming you have a relationship between the StockAvailabilty table and DimDate based on a date column in StockAvailability.
For my issue, I put ALL(DimDate) and then apply a FILTER to ALL(StockAvailability[DateColumn])
Overall Stock Availability =
VAR Date3MonthsAgo = EDATE( TODAY(), -3)
RETURN
CALCULATE(
AVERAGE( [Availability] ),
ALL( DimDate ),
FILTER(
ALL( StockAvailability[DateColumn] ),
StockAvailability[DateColumn] >= Date3MonthsAgo
)
)
I'm guessing that this isn't working because your ALL function is removing the filtering on the StockAvailability table but not the DimDate table.
In your second measure above, the DimDate table is evaluated in the filter context it is being evaluated in, which means the last 7 days filter applies here. To ignore that filter, apply the ALL function to DimDate.
Overall Stock Availability =
CALCULATE(AVERAGE([Availability]),
ALL('StockAvailability'),
FILTER(ALL(DimDate), [IsLast3Months] = true))
Note: I'm not positive you actually want the ALL('StockAvailability') part. Try it with and without.