Integrating Slicer selection with measure - powerbi

I have posted a similar question already but I want to post it again with more detail.
I have a slicer which the user must select a value and then based on the selected value the calculated column below must dynamically update.
Now after reading some threads I know I need to use a measure rather than a calculated column as columns don't support dynamic updates
I have listed my code below, note that ChangeDaysPrevMonth & DaysBetweenExpandOrder are calculated columns
Code
VAR SelectedMeasure =
SELECTEDVALUE(DynamicData[Measure], 10)
VAR DynamicMeasureValue =
SWITCH (
TRUE(),
isblank('Data'[ChangeDaysPrevMonth]),"No Expiry Date available",
'Data'[ChangeDaysPrevMonth]=0, "No Change",
'Data'[ChangeDaysPrevMonth]>0 || 'Data'[ChangeDaysPrevMonth]<0 && 'Data'[DaysBetweenExpAndOrder]>=SelectedMeasure && 'Data'[DaysBetweenExpandOrder]<=0,"Order Soon",
'Data'[ChangeDaysPrevMonth]>0 || 'Data'[ChangeDaysPrevMonth]<0 && 'Data'[DaysBetweenExpAndOrder]<SelectedMeasure ,"No need to order until next month",
'Data'[ChangeDaysPrevMonth]>0 || 'Data'[ChangeDaysPrevMonth]<0 &&'Data'[DaysBetweenExpAndOrder]>0,"Order Now"
)
My question is how do I make this into a measure?
I tried to use the max function(see below), but I had no luck.
VAR DynamicMeasureValue =
maxx('Data',
SWITCH (
TRUE(),
isblank('Data'[ChangeDaysPrevMonth]),"No Expiry Date available",
'Data'[ChangeDaysPrevMonth]=0, "No Change",
'Data'[ChangeDaysPrevMonth]>0 || 'Data'[ChangeDaysPrevMonth]<0 && 'Data'[DaysBetweenExpAndOrder]>=SelectedMeasure && 'Data'[DaysBetweenExpandOrder]<=0,"Order Soon",
'Data'[ChangeDaysPrevMonth]>0 || 'Data'[ChangeDaysPrevMonth]<0 && 'Data'[DaysBetweenExpAndOrder]<SelectedMeasure ,"No need to order until next month",
'Data'[ChangeDaysPrevMonth]>0 || 'Data'[ChangeDaysPrevMonth]<0 &&'Data'[DaysBetweenExpAndOrder]>0,"Order Now"
) )
The output I want is below, I need to count the occurrences of each of the groups created in the switch statement above (i.e. count of the following categories No Expiry Available, No Change, Order Soon, No need to order until next month, Order Now)
Many thanks

Related

Do 2 things when IF is true DAX

I'm trying to find either a work-around for my current statement or a better way to do this all together. I need to set the bundle_id but, once it's set, I don't want the formula changing it. I tried if(isblank(OB1_excel_log[bundle_id]) but it wouldn't let me use that. so I created another column called Assigned next to my bundle_id and figured I would change the value from blank to a 1 once I updated my bundle_id and next time the formula ran it would skip all the rows with a 1 in the Assigned field. Here's the DAX formula
bundle_id = if(isblank(OB1_excel_log[Assigned]),CALCULATE(min(Bundles[bundle_id]), filter(Bundles, OB1_excel_log[Dr Acct #] = Bundles[dr_account_no] && Bundles[Active]="Yes")) && OB1_excel_log[Assigned]=1)
It works until I put that extra action on the end
&& OB1_excel_log[Assigned]=1
Thoughts on how I can perform that additional action ? Or a better way to set the bundle_id without using the 2 columns? Thanks in advance
After formatting your DAX properly at https://www.daxformatter.com you can immediatly see what's going wrong. Your extra action belongs to the else section of the IF clause and does't make much sense.
bundle_id =
IF (
ISBLANK ( OB1_excel_log[Assigned] ),
CALCULATE (
MIN ( Bundles[bundle_id] ),
FILTER (
Bundles,
OB1_excel_log[Dr Acct #] = Bundles[dr_account_no]
&& Bundles[Active] = "Yes"
)
)
&& OB1_excel_log[Assigned] = 1
)

Create Measure - Calculate the Average of values if filters are selected else use the value of "CH"

I tried to create a measure which should calculate the average number of the selected regions (via Chiclet Slicer).
If there is no filter set it should take the value of the whole country (CH - which is also part of this table).
In terms of logic, it should read something like this: If FilterIsActive take the average of median_R_mean of the selected regions else select the median_R_mean of "CH".
I have tried this : 
RE =
IF(
ISFILTERED(Region[Bezeichnung]),
AVERAGE(COVID19Re_geoRegion_Last_value[median_R_mean]),
FILTER(
COVID19Re_geoRegion_Last_value,
"CH"
)
)
Unfortunately this approach does not work.
Can anyone help me on what possible approach I can follow here?
If I understand your issue correct, you are looking for a measure as below-
RE =
IF(
ISFILTERED(Region[Bezeichnung]),
AVERAGE(COVID19Re_geoRegion_Last_value[median_R_mean]),
AVERAGEX(
FILTER(
ALL(COVID19Re_geoRegion_Last_value),
COVID19Re_geoRegion_Last_value[geoRegion] = "CH"
),
COVID19Re_geoRegion_Last_value[median_R_mean]
)
)

Filter function does not work properly, not filtering (DAX)

I have a problem with my filter function.
I want my code to calculate a column that states whether the row is a returning customer for the therapist - so there has to be an earlier profile id (employment number), an earlier customer id, an earlier booking date, and the order status must be active. These things means that it's a returning customer and that it's a returning customer to the specific therapist (profile id)
ReturningCforT = IF(COUNTROWS(
FILTER(ALL(orders),
orders[Column1.Profile_ID] =EARLIER(orders[Column1.Profile_ID]) &&
orders[Column1.Customer_ID] = EARLIER(orders[Column1.Customer_ID]) &&
orders[Column1.OrderStatus] = "Active" &&
orders[Column1.BookingDate] > EARLIER(orders[Column1.BookingDate])
)) >= 1, "Yes", "No")
Currently I can see in my newly created column that there is a "Yes", but the orderstatus is not active on this line, which means that the orderstatus = Active filter, is not currently working.
I have tried to replace all function with value function, and I get the same result.
Can anyone help me solve this? Thank you.
Your filter is looking at the entire table and checking each of the conditions for each row. Since there is a row that satisfies all of them, it returns "Yes" even though that row is not the current row.
I think you intended to apply that condition to the current row, not any row, so I'd suggest moving that outside the FILTER.
ReturningCforT =
IF (
orders[Column1.OrderStatus] = "Active" &&
COUNTROWS (
FILTER (
ALL ( orders ),
orders[Column1.Profile_ID] = EARLIER ( orders[Column1.Profile_ID] )
&& orders[Column1.Customer_ID] = EARLIER ( orders[Column1.Customer_ID] )
&& orders[Column1.BookingDate] > EARLIER ( orders[Column1.BookingDate] )
)
) >= 1,
"Yes",
"No"
)

Create measure based on several filter conditions PowerBI

I want to create a PowerBI measure, to perform some filter tasks.
Basically, I want to create a measure that returns 1 if ColumnA = empty OR column B is empty OR columns C = empty. I'm just getting started with measures and I am getting stuck.
My code attempt:
M_DataIssues = FILTER(
ALL(Table[ColA] ; Table[ColB] ; Table[ColC])
Table[ColA] = "" || Table[ColB]= "" || Table[ColC] = "")
Any help would be appreciated.
PS. due to my user permissions, I can only create measures. I do not have rights to do other modelling tasks or create columns e.g.
Thanks!!
KR ~M.
will this measure work for you? if yes here is the formula
Measure = CALCULATE(DISTINCTCOUNT('Table'[ColumA]) || DISTINCTCOUNT('Table'[ColumB]) || DISTINCTCOUNT('Table'[ColumnC]);FILTER('Table';'Table'[ColumA]="" || 'Table'[ColumB]="" || 'Table'[ColumnC]=""))

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