Get previous four weeks with DAX - powerbi

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

Related

If Statement in Power Query

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

How to split graphs in two halves (15 days each)

I want to split graph in two halves of 15 day each using dates
i have tried using Top N filters Top and Bottom but when date range is changed i have duplicated graphs.
Split is happening using above filter but when i reduce date filter range duplicate data is shown in two graphs
i tried Flag = IF(DAY(MAX('Table'[Date]))<=15,1,2) as well but was getting data not in sequential order
I'm new to this any help will be thankful.
I just split it in half, rather than by 15 days. But you could change the math to use 15 days instead, if that's what you still want. Regardless, seeing this might help.
I started with a table like this, called Sheet1:
Then I created a chart like your original:
Then I created this DAX measure:
Chart Selector =
var minDate = CALCULATE(FIRSTDATE(Sheet1[Date]), ALL(Sheet1[Date]))
var maxDate = CALCULATE(LASTDATE(Sheet1[Date]), ALL(Sheet1[Date]))
var midDate = minDate + (DATEDIFF(minDate, maxDate, DAY)/2)
return
IF(CALCULATE(SUM(Sheet1[Date]),FILTER(Sheet1,Sheet1[Date]<=midDate)),"First","Second")
(You could change the midDate variable's calculation, above, to one that does 15 days instead.)
Then I made two copies of the chart and, in each copy, I added the new measure to the chart's filters. In the first chart's filter, I filtered for if it contains "First." For the second chart's filter, I filtered for if it contains "Second."
My end result looks like this:

I'm trying to get Last month to date in Power BI

I have a Power Bi dashboard tracking several metrics since the beginning of last month . Some of the comparisons I make are MTD vs Last MTD count of metrics like Total users, No of posts and connections made.
MTD(June) and LMTD(May) were working well last month(June) but when we moved to a new month(july) the numbers are off.
Here's my measure
MTD_Users = CALCULATE(COUNTROWS('reporting profiles'), FILTER('reporting profile', MONTH('reporting profile'[date_created])=MONTH(TODAY())))
LMTD_USERS = CALCULATE(COUNTROWS('reporting profiles'), FILTER('reporting profile', MONTH('reporting profile'[date_created])=MONTH(TODAY())-1))
Since July 2nd these measures are not displaying correct figures for MTD(July 1st) and LMTD(June 1st)
Any advice/assistance will be highly appreciated
You need to ride off contex filter and that mean we must use function ALL or REMOVEFILTERS;
https://dax.guide/removefilters/
https://dax.guide/all/
LMTD_USERS = CALCULATE(COUNTROWS('reporting profiles'), FILTER(ALL('reporting profile'[date_created])), MONTH('reporting profile'[date_created])=MONTH(TODAY())-1))

DAX: Calculate time spent from one stop to other with respect to zipcode

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

Combining formulas

Please help me with combining these two formulas:
REGEXP_EXTRACT([resolution],'(\d{2}:\d{2})')
IF CONTAINS([resolution],"closed") and NOT CONTAINS([resolution],"resolution") THEN 1 ELSE 0 END
Example:
I have lines
1.resolution(01:01) Completed repair equipments
2.resolution(01:02) Preventive maintenance
3.The problem is not current
4.resolution(01:03) Completed repair equipments. The problem is not current
I have 2 calculated field.
REGEXP_EXTRACT([resolution],'(\d{2}:\d{2})') - find numbers 01:01,01:02,01:03
IF CONTAINS([resolution],"closed") and NOT CONTAINS([resolution],"resolution") THEN 1 ELSE 0 END - excludes records:3.The problem is not current
I want to make a formula which combined two formulas and displays numbers:01:01,01:02,01:03
I assume you are trying to use these in a calculated field?
You should be able to pull the initial one into your second calculated field. So if you create your first calculated field(analysis->create calculated field) and lets call it "Calc 1". So this will have your initial formula:
REGEXP_EXTRACT([resolution],'(\d{2}:\d{2})')
Then create a second calculated field and call it "Calc 2". In this formula just update your original to call the first calculated field which contains your formula:
IF CONTAINS([Calc 1],"closed") and NOT CONTAINS([Calc 1],"resolution") THEN 1 ELSE 0 END