Do 2 things when IF is true DAX - powerbi

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
)

Related

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

Error 'The expression refers to multiple columns

Does anyone know why this measure is throwing up an error 'The expression refers to multiple columns. Multiple columns cannot be converted to a scalar value.
MALS Lastweek =
CALCULATE (
COUNT ( vw_sana_account[new_mal] ) = 1
&& FILTER ( DateTable, DateTable[LastWeek] = "Yes" )
)
Yep, your syntax is wrong. This is what you are looking for:
MALS Lastweek = Calculate(Count(vw_sana_account[new_mal]), DateTable[LastWeek]="Yes")
You dont need Count() = 1. It does automatically add 1 in the count function and if you use Calculate() your second argument is the filter.
This measure needs a relationship between the vw_sana_account and the DateTable tables by the way.

OLE DB or ODBC Error: We cannot convert the value null to type Logical

I am calculating following calculated column in query editor:
End Date =
if [Date_1] <> null
then [Date_1]
else if [Date_2]<>null
then [Date_2]
else DateTime.Date(DateTime.LocalNow())
Based on this column, the following table is calculated:
Resident Payer Dates =
SELECTCOLUMNS (
GENERATE (
'Table1',
FILTER (
ALLNOBLANKROW ( Dates[Date] ),
Dates[Date] >= 'Table1'[Start Date]
&& Dates[Date] <= 'Table1'[End Date]
)
),
"Id", 'Table1'[Id],
"Date", Dates[Date]
)
Everything is working fine till here.
However, for some reason, I need to change the End Date column with the following formula:
End Date =
if [Date_1] <> null
then Date.AddDays([Date_1], -1)
else if [Date_2]<>null
then Date.AddDays([Date_2],-1)
else DateTime.Date(DateTime.LocalNow())
However, when I try to apply the changes, I am getting the following error:
I am totally clueless about why we are running in this error with a simple change like above, as the change is not producing any null values.
Any help and guidance would be appreciated.
For anyone who faces such issue in future:
Here, the query editor shows no errors and the evaluation is completed till the last step successfully. However, when you try to apply the changes, the error mentioned above is encountered.
With a lot of searches, I figured out that the column got corrupted, for reasons being still unknown.
To solve this, all you have to do is to remove the step/column completely and recreate it, the error will go away.

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