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

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.

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

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
)

PowerBI DAX - IF with AND / OR

I am sure this is a simple question but I have tried a few versions found on here and can't seem to get the syntax right. Have also tried using SWITCH function and similarly encountered errors with the code.
Essentially, I want to mark each record with an 'Overdue' indicator based on the following logic. Within my data, some records have a Target Date populated but many are null. For the records that are null, I want to use the Action Due Date to determine if it is overdue or not.
IF 'STATUS' is not 'CLOSED' AND 'TARGET DATE' less than today = Overdue
OR
IF 'STATUS' is not 'CLOSED' AND 'ACTION DUE DATE' less than today = Overdue
ELSE Not Overdue
The DAX I am trying to write is like this :
Overdue =
IF('Table'[Status] <>"Closed" && 'Table'[Target Date]<Today(), "Overdue", OR(
IF('Table'[Status] <>"Closed" && 'Table'[Action Due Date]<Today(), "Overdue",
"Not Overdue")))
Sorry for the dumb question, really struggling with the progression to DAX from Excel.
Try this then
IF(
OR(
'Table'[Status] <>"Closed" && 'Table'[Target Date]<Today()
,'Table'[Status] <>"Closed" && 'Table'[Action Due Date]<Today()
)
,"Overdue"
,"Not Overdue"
)

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

PowerBI DAX get COUNT DISTINCT with GROUP BY , see SQL query below

I have got this following SQL query that gives me the correct value from the database.
SELECT
SUM( DISTINCT_ORDER_NUMBERS )
FROM
(
SELECT STORE_KEY,
COUNT( DISTINCT TRANSACTION_NUM ) AS DISTINCT_ORDER_NUMBERS,
DATE_KEY,
TRANSACTION_TYPE_KEY
FROM Pos_Data
GROUP BY STORE_KEY,
DATE_KEY,
TRANSACTION_TYPE_KEY
)
AS A
I am however facing challenges writing a DAX formula for a measure in Power BI Here is what I have tried so far but I get an error.
Total Number Of Orders
VAR _TotalOrders =
SUMMARIZE('Pos_Data',
'Pos_Data'[STORE_KEY],
'Pos_Data'[DATE_KEY],
'Pos_Data'[TRANSACTION_TYPE_KEY],
"DISTINCT_ORDER_NUMBERS",
DISTINCTCOUNT('Pos_Data'[TRANSACTION_NUM]))
RETURN SUM(_TotalOrders[DISTINCT_ORDER_NUMBERS])
Please assist
The SUM function expects a base table rather than a calculated table.
Try this instead:
VAR _TotalOrders =
SUMMARIZE('Pos_Data',
'Pos_Data'[STORE_KEY],
'Pos_Data'[DATE_KEY],
'Pos_Data'[TRANSACTION_TYPE_KEY],
"DISTINCT_ORDER_NUMBERS",
DISTINCTCOUNT('Pos_Data'[TRANSACTION_NUM]))
RETURN SUMX(_TotalOrders, [DISTINCT_CHECK_SEQ])
Edit: If the difference you mentioned is related to nulls, then try this in place of DISTINCTCOUNT.
COUNTAX( DISTINCT( 'Pos_Data'[TRANSACTION_NUM] ), 'Pos_Data'[TRANSACTION_NUM] )
The COUNTAX function (as opposed to COUNTX) does not count nulls.