I have a simple if Statement in Power Query I feel I need a separate set of eyes on.
I am trying to say if the current day of the week is Monday then I want the system to display whatever the date was 3 days ago, else any other day just show yesterdays date. My formula below is receiving the error "Token RightParen expected." at the "Date" after "then" on the 2nd line.
if(Date.DayOfWeek(DateTime.FixedLocalNow()) = 0 then
Date.AddDays(Date.From(DateTime.FixedLocalNow()),-3) else
Date.AddDays(Date.From(DateTime.FixedLocalNow()),-1))
A few extra characters. Remove first and last parenthesis
let Source = if
Date.DayOfWeek(DateTime.FixedLocalNow()) = 0 then
Date.AddDays(Date.From(DateTime.FixedLocalNow()),-3) else
Date.AddDays(Date.From(DateTime.FixedLocalNow()),-1)
in Source
Related
All, I am trying to isolate the time portion only of a date/time stamp and then create a case statement to populate another column with data. This is how the date/time stamp looks: 2022-03-25 04:00:00
Here is the code I originally wrote but when I just use the hours portion I get an error in PowerBi saying invalid date/timestamp...
case when create_tmstp between '04:00:00' and '07:30:00' then 'N' else 'D' end as Day_Night,
From the looks of it, you're using SQL, not DAX. I say that, because you've tagged this with dax and not SQL.
Problem is that you're not showing us what create_tmsp is, or what version of what kind of sql server you're using. So, just to make it easier, this is an working example to use for instance inside SSMS. This uses getdate() to get the current datetime, and then converts it into just showing what hour it is (0-24), then there is a case when that checks which intervall it falls under. You can use this, and simply replace getdate() with the column you wish to use if you're using a server that accepts this syntax.
select
DATEPART(HOUR,getdate()) AS 'HOUR',
getdate() AS 'Datetime',
CASE WHEN DATEPART(HOUR,getdate()) BETWEEN 9 AND 16
THEN 'Day'
WHEN DATEPART(HOUR,getdate()) BETWEEN 17 AND 24
THEN 'Evening'
WHEN DATEPART(HOUR,getdate()) BETWEEN 0 AND 4
THEN 'Night'
WHEN DATEPART(HOUR,getdate()) BETWEEN 5 AND 8
THEN 'Morning'
END AS 'Label'
Example result:
Hour
Datetime
Label
0
2022-04-04T00:18:13.203Z
Night
Fiddle here:
http://sqlfiddle.com/#!18/56f78/3829
I need to calculate the time spent from one stop to another.
If the trip takes place in the same zipcode area and greater than one hour, I should report it.
For example,
The attached image with the dataframe shows the instance when arrivedTime from one stop to other in same zipcode has difference greater than one hour. I signal it. If it is less than one hour, no action needed. It should be grouped by and order by RuteID, Sequence and arrivedTime. In the attached example, the trip from zipCode 2300 to 2300 takes less then one hour it is ok. But If it is greater than one hour I should report by new column true/false for the number greater than one hour.
I would do this in power query using these steps:
Add a new column "NextSequence" = [Sequence]+1
Do a nested join with the table itsself, linking {"RoutID", "Sequence"} with {"RoutID", "NextSequence"} and when expanding, only keep the departedTime and ZipCode olumns calling them something like "prevDepTime" and "PrevZip"
write an IF-statement saying that if [PrevZip] <> null and [PrevZip] = [ZipCode] and [ArrivedTime]-[PrevDepTime] >= #time(1,0,0) then "Late" else "OK"
Clean up the table, keeping only the columns you want.
Then when loading the new table into power bi I have something that looks like this (I added a ew stops on RoutID2 for testing):
My M-code:
AddNextSequence = Table.AddColumn(PREVIOUS_STEP_NAME, "NextSequence", each [Sequence]+1, Int64.Type),
NestedJoin = Table.NestedJoin(AddNextSequence, {"RouteID", "Sequence"}, AddNextSequence, {"RouteID", "NextSequence"}, "AddedTable", JoinKind.LeftOuter),
ExpandTable = Table.ExpandTableColumn(NestedJoin, "AddedTable", {"DepartedTime", "ZipCode"}, {"PrevDepTime", "PrevZip"}),
Add_OkLate = Table.AddColumn(Expandedtable, "OK_or_Late", each if [PrevZip] <> null and [PrevZip] = [ZipCode] and Number.From([ArriveedTime]) - Number.From([PrevDepTime]) > Number.From(#time(1,0,0) ) then "Late"
else
"OK", type text),
FinalizeTable = Table.SelectColumns(AddOkLate,{"RowNumber", "RouteID", "Sequence", "ArriveedTime", "DepartedTime", "ZipCode", "OK_or_Late"})
in
FinalizeTable
I have a measure to get data from the last four weeks:
Last four weeks = CALCULATE(SUM(THEFT[AMOUNT_STOLEN]);FILTER(THEFT;DATEDIFF(THEFT[DATE_STOLEN];TODAY();WEEK)<5))
I want a second measure that gets the previous 4 weeks so I can divide these two measures. I can't get that to work. I've tried something like this but it gives me no data:
Previous four weeks = CALCULATE(SUM(THEFT[AMOUNT_STOLEN]);FILTER(THEFT;DATEDIFF(THEFT[DATE_STOLEN];TODAY();WEEK)>4)) && CALCULATE(SUM(THEFT[AMOUNT_STOLEN]);FILTER(THEFT;DATEDIFF(THEFT[DATE_STOLEN];TODAY();WEEK)<9))
Try to include the second criteria into your filter function:
Previous four weeks = CALCULATE(SUM(THEFT[AMOUNT_STOLEN]);FILTER(THEFT;DATEDIFF(THEFT[DATE_STOLEN];TODAY();WEEK)>4 && DATEDIFF(THEFT[DATE_STOLEN];TODAY();WEEK)<9))
I`m trying to populate values in column F, "Product Sold Date" in "TABLES" tab
Basically... the logic is as follows:
1) If Column C (Product Status) = "Paused", then return "Paused"
2) If Product start date = NULL or Product end date = NULL, then return NULL
3) If Product start date < today`s date, then return "No Data"
4) If Product start date >= today`s date, return "Upcoming"
5) If product End date <= today`s date, return "Ended"
6) If product start date <= today`s date, return "In Market"
7) If the condition does not belong to any of the above cases, then return the actual Product launch dates
Below is the link to the sample data I`m working on..
I`m pasting the link itself, becaues there are multiple tabs included
https://docs.google.com/spreadsheets/d/120rHOt8Pa_PdMKPgLkSYOKTO2Cv1hBH6PpTrg7FfJLk/edit?usp=sharing
Ultimately, I need to populate the actual "Product Launch Date" by scanning data in each tab
I tried using nested if statements with combination of Index Match.
But I`m totally lost in case of multiple tabs
Can anyone please provide suggestion on this?
Should we think about using query statements instead in this case?
Sidenote: The returned values will be mix of dates and character
[In market/ Ended/ Upcoming/ No Data/ NULL/ Paused/ Actual Date]
=ARRAYFORMULA(
IF(C2:C="Paused", C2:C,
IF((A2:A="")+(B2:B=""), ,
IF(A2:A >= TODAY(), "Upcoming",
IF(B2:B <= TODAY(), "Ended",
IF(A2:A = TODAY(), "In Market",
IF(E2:E<>"", IFERROR(VLOOKUP(D2:D&E2:E,
{'Eaton Centre'!A2:A &"Eaton Centre", 'Eaton Centre'!B2:B;
'Yorkdale Mall'!A2:A&"Yorkdale Mall", 'Yorkdale Mall'!B2:B;
'Vaughan Mills'!A2:A&"Vaughan Mills", 'Vaughan Mills'!B2:B}, 2, 0)), )))))))
Your formula would be
=IF(C2="Paused",C2,if(OR(A2="",B2=""),"",IF(A2<TODAY(),"No Data",IF(A2>=TODAY(),"Upcoming",IF(B2<=TODAY(),"Ended",IF(A2<=TODAY(),"In Market","Actual Product Launch dates"))))))
In the above formula, you should be using a Query formula in place of "Actual Product Launch dates", to extract the actual date.
But the points 3 & 6 don't make any sense. The 6th condtion should be If product start date = todays date, return "In Market"
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.