New Customer measure - powerbi

Good day!
I have this measure which used to identify which clients are new based on the parameter (6 month) and also month & year of their ETD.
New Customers = //Cus
VAR CustomerTM = VALUES(Customer[Client])
VAR PriorCustomer = CALCULATETABLE(VALUES(Customer[Client]),
FILTER(ALL('Customer'),
'Customer'[ETD] > Min('Customer'[ETD] ) - Parameter[Parameter Value] && 'Customer'[ETD] < MIN('Customer'[ETD])))
return
COUNTROWS(
EXCEPT(CustomerTM,PriorCustomer))
My raw data:
There are no problems with the coding until the 'branch' slicer is added.
Based on my raw data, Nike should consider new customers for SIN branch in January, but for TYO branch in February.
But this code as a result, NIKE only had new customers on January, is there any part that I was missed?
Attached with pbix: https://drive.google.com/file/d/1mvW9opym6Ot2AlyZy0m0kl2rDeB4P2Wv/view?usp=sharing
Greatly appreciated any helps provided! Thank you!

Try ALLEXCEPT() instesd of ALL in your FILTER. All() removes all filters and slicers from you data. Only Parameter[Parameter Value] infuences your FILTER. So if you do not change it in all other cases you have the same result.
FILTER(
ALLEXCEPT('Customer',Customer[Branch])
,AND(
'Customer'[ETD] > Min('Customer'[ETD] ) - Parameter[Parameter Value]
,'Customer'[ETD] < MIN('Customer'[ETD])
)
)

Related

DAX Flag in Matrix Visual - Multiple levels of analysis

I replicated an issue I am having with the 'Adventure Works DW 2020' pbix file, so if my analysis seems a little out of context, please understand this example is not the true data I am working with. The pbix I used can be downloaded here:
https://drive.google.com/file/d/1vn6CluiE5rrAF3UjYPh5ejb93H2JX6IX/view?usp=sharing
My goal is to create a measure that can flag the subset of records that I want to use for a matrix visual.
I created the following measure with notes in the syntax:
VAR TABLEVAR =
SELECTCOLUMNS(
FILTER(
SUMMARIZE(
CALCULATETABLE(Sales/*Apply several filters to Sales table*/
,NOT Sales[CustomerKey] = -1
,Sales[orderdatekey] > 20180731
,Sales[orderdatekey] < 20190601
)
,[CustomerKey]/*Count the number of products per customer*/
,"Count",COUNT(Sales[ProductKey])
)
,[Count] > 1/*Only keep customers that bought more than 1 product*/
)
,[CustomerKey] /*Select the identifiers of the desired customers*/
)
RETURN
{
SWITCH(TRUE()
,SELECTEDVALUE(Sales[CustomerKey]) IN TABLEVAR/*Flag the customers that were identified in the previous table*/
,1,BLANK()
)
}
Now, in the PowerBI Matrix visual, this seems to work at first:
I had successfully flagged the desired output. Now I just have to filter for the 'Analysis' measure to be 'Not Blank', but then this happens:
Now removing that filter and going down a level:
So you see, the measure does not evaluate at the record level of the table. Does anyone understand the concept I am missing here? I have tried all kinds of different measures but it all comes down to the same problem about flagging different levels of analysis.
Ideally, the output would only include the following(circled in green):
These are the records that are within the date filters I put into the CALCULATETABLE() arguments.
Any help or insight with this problem would be greatly appreciated. Thank you
I'm not 100% clear what you're trying to do but please try the following and see if it helps.
Analysis =
VAR TABLEVAR =
SELECTCOLUMNS(
FILTER(
SUMMARIZE(
CALCULATETABLE(Sales
,NOT Sales[CustomerKey] = -1
,Sales[orderdatekey] > 20180731
,Sales[orderdatekey] < 20190601,
REMOVEFILTERS()
)
,[CustomerKey]
,"Count",COUNT(Sales[ProductKey])
)
,[Count] > 1
)
,[CustomerKey]
)
RETURN
//CONCATENATEX(TABLEVAR, [CustomerKey], ",")
SWITCH(TRUE()
,SELECTEDVALUE(Sales[CustomerKey]) IN TABLEVAR
,1,BLANK()
)

Comparison between previous week and current week total sales in powerBi

I am very new to the PowerBI community and I am confused about how to visualize and create measures/columns for the data which requires comparing last week/month/year data with respect to the current week.
I have tried various solutions available on the internet or other forums. I would appreciate it if anyone can please outline the steps required to achieve the goal.
The data that I have is transactional data and I have also created a Date Table. I am not sure how to go ahead with the problem.
You can create measures like this (for days):
PreviousDay =
var __DayOnRow = SELECTEDVALUE(Calendar[day])
return
CALCULATE( SUM(Table[SomethingToSum]), FILTER(ALL(Calendar),Calendar[day] = __DayOnRow -1 ))
How this work:
SELECTEDVALUE gets a specific day from the current context
__DayOnRow -1 give us a previous Day (not yesterday date< except for today date>)
FILTER with ALL, remove every filter on Calendar (current row is also a filter, so without removing filter we get two excluding conditions )
How do that for WEEK?
PreviousWeek =
var __WeekOnRow = SELECTEDVALUE(Calendar[Week])
var __FirstDayOfWeek = calculate(min(Calendar[Day]), FILTER(ALL(Calendar), __WeekOnRow = Calendar[Week] ))
var __LastDayOfWeek = calculate(max(Calendar[Day]), FILTER(ALL(Calendar), __WeekOnRow = Calendar[Week] ))
return
CALCULATE(SUM(Table[SomethingToSum]), FILTER(ALL(Calendar),Calendar[day] >= __FirstDayOfWeek -7 && Calendar[day] <= __LastDayOfWeek -7 ))

Previous month categorical value Power BI

I have the table which looks something like this. I am trying to find a way to find status change per an account, e.g. if the current month the status is Written off but it was Active last month, the tag should be Newly written Off. Is it feasible in Power BI? I found PREVIOUSMONTH but it deals only with measures, not categorical values like I have.
this seems like a trivial problem, but I found out, that it's not easily solvable in PowerBI.
Before showing the way I solve this, I would recommend you to solve it prior to loading data to PowerBI (ie. in the data source).
If that is not possible here's what you should do:
(recommended) Create T-1 data column == column, which has the previous date for comparison (for you, its previous month or date):
T-1 date =
VAR __acc_id = TABLE[account_id]
VAR __date = TABLE[date]
RETURN
CALCULATE(MAX(TABLE[date]),FILTER(ALL(TABLE), TABLE[account_id] = __acc_id && TABLE[date] < __date))
The filter part of calculation "returns" part of the table, which has the same account_id as current row and smaller date then current row. After that, we simply select the max date, which should be the previous one.
This way, we find the biggest previous date for each account.
If you know what the previous date is, feel free to skip this step.
Prior to the creation of the status column itself, I would create a calculated column, which contains the previous status. The column should be calculated like this:
t-1 status=
var __acc_id = TABLE[account_id]
var tdate = TABLE[T-1 date] //CREATED IN PREVIOUS STEP OR YOUR PREVIOUS DATE METRIC(LIKE dateadd or previousmonth function)
return
CALCULATE(firstnonblank(TABLE[status]), FILTER(ALL(TABLE), TABLE[account_id]=__acc_id && table[date] = tdate))`
With the previous status column, we now have the current and previous status columns, which should be enough to correctly label the "rows" with simple if statement.
The calculated column formula might look something like this:
status_label = if(TABLE[status] == "Written off" && TABLE[t-1 status] == "active", "newly written off", "something else").
If simple IF isn't enough, have a look at switch statement
This sequence of steps should solve your issue, but I have to admit, it's not performance efficient. It would be better to solve it in PowerQuery, but sadly, I do not know how. Any solution using PowerQuery would be highly appreciated.

PowerBI DAX - Identifying first instance based on multiple criteria

Using DAX to identify first instance of a record
I'm faced with trying to identify the first instance in a database where someone (identified by the ID column) has purchased a product for the first time. It's possible for said person to purchase the product multiple times on different days, or purchase different products on the same day. I drummed up an excel formula that gets me there, but am having trouble translating into DAX.
=COUNTIFS(ID,ID,PurchaseDate,"<="&PurchaseDate,Product,Product)
Which results in the correct values in the "First Instance?" Column.
Ideally I won't have to hardcode values, as I would like to use the "Product" column as a parameter in the future. If there are other suggests aside from translating this in DAX, that would also be appreciated! (IE using filters, or other tools in PowerBI)
Thanks in advance!
This is very similar to an answer I gave to another question (which you can find here).
In that question, the request was to see a running count of rows for the given row's criteria (product, year, etc.). We can modify that slightly to get it to work in your problem.
This is the formula I provided in the answer I linked above. The basic concept is to use the EARLIER functions to get the value from the row and pass it into the filter statement.
Running Count =
COUNTROWS(
FILTER(
'Data',
[ProductName] = EARLIER([ProductName]) &&
[Customer] = EARLIER([Customer]) &&
[Seller] = EARLIER([Seller]) &&
[Year] <= EARLIER([Year])
)
)
What I would suggest for your problem is to create this as a TRUE/FALSE flag by simply checking if the running count is 1. This formula will evaluate to a Boolean flag.
First Instance =
COUNTROWS(
FILTER(
'Data',
[ID] = EARLIER([ID]) &&
[Product] = EARLIER([Product]) &&
[Purchase Date] <= EARLIER([Purchase Date])
)
) = 1

Automatic Data Transfer to DAX

I'm new in this so please pardon me for such a basic question.
I'm trying to pass some dates as filters to a measure.
I have a simple measure that counts opened items "last week".
The DAX I used:
OpenedLastWeek = CALCULATE(COUNTROWS(Table);Table[Created
Year]=2018;Table[Created Week]=45)
But there should be a way to pass values to the DAX code to replace manually entered "2018" and "45".
I tried Date Tables, extra tables with dates in it... I'm just confused.
Thanks.
In this case, I would probably use variables.
OpenedLastWeek =
VAR CreatedYear = --<Desired Year Calculation>--
VAR CreatedWeek = --<Desired Week Calculation>--
RETURN
CALCULATE(
COUNTROWS(Table);
Table[Created Year] = CreatedYear;
Table[Created Week] = CreatedWeek
)
Basically, you define the week and year how you'd like and then pass those variables into the CALCULATE function.
Thank you very much. It solved my problem.
For this case, I tried variables before but I was getting wrong results. I guess that was because I was using "CountA()" instead of "Countrows()".
Thanks again.
Have a nice day,
Eld