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"
)
Related
Using the table attached as an example 'Financial_Tab'.
I want to sum [Budget] based on whether "1.2.4" is within [CBS Code], and where "NOP1" or "NOP2" or "NOP3" is within [CBS Name]. Returning £50 in the example table.
I'm struggling looking for a succinct and functional way of searching for NOP1/NOP2/NOP3 in one go. I'm returning blank results. I think IN {} could be used but I cant get it to work with containsstring.
I really appreciate any help.
Thanks
This is what you're looking for:
Sum Budget =
CALCULATE(
SUM('Table'[Budget]),
CONTAINSSTRING('Table'[CBS Code], "1.2.4"),
CONTAINSSTRING('Table'[CBS Name], "NOP1")
|| CONTAINSSTRING('Table'[CBS Name], "NOP2")
|| CONTAINSSTRING('Table'[CBS Name], "NOP3")
)
This is my first stack overflow question so please forgive me if I haven't followed best practices/formatting for this forum.
I am trying to use DAX to write some unit tests for my powerBi reports however I'm struggling to make my tests both dynamic whilst testing against verified historic data.
The problem comes from the fact that many of the measures I would like to test have a date related measure and/or function nested inside for example:
Yesterday's amount = CALCULATE(SUM('Cost Analysis'[computedAmount]),('Cost Analysis (New)'[timeUsageStarted] = TODAY() - 1))
I would like my test to be able to dynamically test [yesterday's amount] (even if somebody changed some of the measure logic) however I need to hard code a date in place of the TODAY() function in order to test it against an expected value that I have access too.
I have been able to rewrite the measure logic (adding a company filter and rounding in the process) with a hard coded date for my expected value but this means changes made to the measure would make my test obsolete.
EVALUATE
ADDCOLUMNS(
UNION(
ROW(
"Test Name", "Testing [Yesterday's Amount] where yesterday is set to 20/03/22"
,"Expected Value"
, 48.81
,"Actual Value"
,ROUND(CALCULATE(SUM('Cost Analysis (New)'[computedAmount]), 'Cost Analysis (New)'[Companies] = "company name",
FILTER(ALL('Cost Analysis (New)'),
'Cost Analysis (New)'[timeUsageStarted] = DATE(2022, 03, 20)
)
) ,2
),"Test Failed", IF([Expected Value] <> [Actual Value], "Yes", BLANK())
)`
on the other side I can make my expected value equal to the output of [Yesterday's amount] however I then don't have control over the value of TODAY() so my expected value would need to be changed every day (which is of course not useful).
Is there a way that I could make my expected value = [Yesterday's amount] instead of my hardcoded rewrite whilst passing in a value to hard code the value for TODAY()?
I have the same problem with nested date measures such as [current month] being part of a [month to date spend] calculation.
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.
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.
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