PowerBi Conditional Column Between 2 Dates - powerbi

My First PowerBi Question on here.
Im trying to insert a Conditional Column in the Power Query Editor where if a date falls between Certain dates, a specific Value is added. The dates and values are listed below.
I have attached a screenshot of my attempt but the results stop at 51 and do no increase from there.
can you please confirm where i am going wrong with this? its almost as if it needs a Between arguement but not sure if this is possible in the Power Query Editor?
Dates & Values:
Before 18/07/2022 = 31
On or After 18/07/2022 & Before 08/08/2022 = 51
On or After 08/08/2022 & Before 30/08/2022 = 71
On or After 30/08/2022 = 91

If statement searchs for the first TRUE() value and return it. In your case you use condition "after or equal" so every date with value >=18/07/2022 gets 51, because it is the first True value.
Try to change the order:
is before 18/07/2022 = 31
after or equal to 30/08/2022 = 91
after or equal to 08/08/2022 = 71
after or equal to 18/07/2022 = 51

Related

Date formula Power Query

Oi,
I'm trying to add a new column to my Power Query containing a fixed text & date (MMYYYY) and i keep getting errors while doing so.
Example of what i need is : "TEST 032022" --> Test is Fixed ; 03 is formula ; 2022 is formula
Does anyone know how to fix this?
Thanks!
You need to turn the Numbers into text, using the Date.ToText function or Number.ToText
So for your MM you would need some thing like this, assuming you have a date column
Number.ToText(Date.Month([Date]), "00")
Which gives months 01 to 12
Or
Number.ToText(Date.Month(DateTime.LocalNow()), "00")
Using the current date time "03"
For your example you would need
"TEST " & Number.ToText(Date.Month(DateTime.LocalNow()), "00") & Number.ToText(Date.Year(DateTime.LocalNow()))
Which outputs TEST 032022

Calculate percentage difference between consecutive rows - Power BI

I have a table in the following structure:
Forecast Type Group Forecast
June 00 A 120
July 01 A 125
June 00 B 250
July 01 B 222
June 00 C 308
June 01 C 270
I would like to visualize this in a matrix by grouping by "Forecast Type" and aggregate by summing the "Forecast". Then, I would like to create a calculated column or metric that contains the percentage difference between the Forecast Type. If the user filter by "Group" using the Slicer, this should also be updated accordingly. This is what the matrix should look like when all groups are selected:
Forecast Difference in EUR Percentage Difference
June 00 678 null null
July 01 617 -61 -9%
I was able to create the "Difference in EUR" variable with some research by creating an Index column and using the:
Difference in EUR =
VAR RowAbove=
CALCULATE (
SUM ('Forecast Development'[Forecast]),
FILTER ('Forecast Development', 'Forecast Development'[Index] = EARLIER ('Forecast Development'[Index]) -1))
RETURN
'Forecast Development'[Forecast] - RowAbove
However, I am not able to create the percentage and by using this calculated column formula. Also, in the "Difference in EUR" column, the first row is not null, as it should be since I dont have another value to calculate the difference with.
I also tried to force the first row to being null by creating another calculated column and filtering out the "Forecast Type" June 00 but apparently I can not do this.
I am also not sure how to create the index, for now I just did normally from 1 to 6. But maybe I need to restart the index everytime a group changes? So, 1,2,1,2,1,2?
I am new in Power BI and would appreciate the help. Thanks!
If your [Forecast Type] always contain valid number at the end then we can calculate this like that:
Diff =
var __CurrentType = value( RIGHT(SELECTEDVALUE(Sheet1[Forecast Type]),2))
var _prevForecast = CALCULATE( sum(Sheet1[Forecast]), filter(all(Sheet1[Forecast Type]), value( RIGHT(Sheet1[Forecast Type],2)) = __CurrentType -1 ))
var __currForecast = CALCULATE(sum(Sheet1[Forecast]))
return
if (ISBLANK(_prevForecast), BLANK(),
__currForecast - _prevForecast)
Percentage%% =
var __CurrentType = value( RIGHT(SELECTEDVALUE(Sheet1[Forecast Type]),2))
var _prevForecast = CALCULATE( sum(Sheet1[Forecast]), filter(all(Sheet1[Forecast Type]), value( RIGHT(Sheet1[Forecast Type],2)) = __CurrentType -1 ))
var __currForecast = CALCULATE(sum(Sheet1[Forecast]))
return
DIVIDE(__currForecast, _prevForecast)

Power BI Complex Calculated Column

I'm quite new to Power BI and I try to query Azure DevOps data for a Board with historical data.
I have made an OData feed that queries data and returns the data below (see table) and for each row where IsCurrent = True, I want to calculate the "blocked time" into a new Column BlockedTime for that WorkItemId. So I need to traverse records for that WorkItemId and do date calculation.
I am kind of stuck currently after much delving into CALCULATE, Filters and more.
I have specified three scenarios for the WorkItemId 1, 72 and 149.
I use the column Index as the number to reference lines in my calculations below.
My query returns the following:
WorkItemId Revision Index AnalyticsUpdatedDate IsCurrent TagNames BlockedTime
72 7 0 06/19/2020 11.41.04 True See calculation 1
72 6 1 06/19/2020 11.41.04 False Blocked
72 5 2 06/18/2020 10.41.23 False Blocked
72 4 3 06/17/2020 09.38.54 False
72 3 4 06/16/2020 14.22.21 False Blocked
72 2 5 06/15/2020 15.01.02 False
72 1 6 06/14/2020 07.21.16 False
1 6 7 07/07/2020 09:58:12 True Blocked See calculation 2
1 5 8 07/07/2020 09:58:12 False
1 4 9 07/06/2020 10:22:02 False Blocked
1 3 10 07/05/2020 12:34:31 False
1 2 11 07/04/2020 13:51:30 False Blocked
1 1 12 07/03/2020 08:23:41 False
149 1 13 07/02/2020 10:01:55 False Blocked See calculation 3
RULES
Variable CurrentDate contains current DateTime
When TagNames contains "Blocked" text for a given line it is considered blocked from the date/time in AnalyticsUpdatedDate and the time should be calculated "backwards" by traversing down in the records (Revision number going down) and summarized until a line without "Blocked" in TagNames is encountered
Calculation 1: Calculate BLOCKED TIME and put the result into BlockedTime(0)
(AnalyticsUpdatedDate(1) - AnalyticsUpdatedDate(2)) +
(AnalyticsUpdatedDate(2) - AnalyticsUpdatedDate(3)) +
(AnalyticsUpdatedDate(4) - AnalyticsUpdatedDate(5))
Calculation 2: Calculate BLOCKED TIME and put the result into BlockedTime(7)
(CurrentDate - AnalyticsUpdatedDate(7)) +
(AnalyticsUpdatedDate(9) - AnalyticsUpdatedDate(10)) +
(AnalyticsUpdatedDate(11) - AnalyticsUpdatedDate(12))
Calculation 3: Calculate BLOCKED TIME and put the result into BlockedTime(13)
(CurrentDate - AnalyticsUpdatedDate(13))
Anyone who has any ideas of how this could be solved best?
So here we go. I made it in 3 steps with 3 columns so it is a bit easier to follow, you can make it in one script.
First we need to find the dates of all teh releases (when the block is not there anymore)
This we do with the following column:
Released =
var workItemId = Track[WorkItemId]
var Revision = Track[Revision]
var ReleaseTime = CALCULATE(MIN(Track[AnalyticsUpdatedDate]), FILTER(Track, Track[WorkItemId] = workItemId && Revision < Track[Revision]))
var ReleaseFinal = if (ISBLANK(ReleaseTime), NOW(), ReleaseTime)
return if(Track[TagNames] = "Blocked", ReleaseFinal)
In the calculate, I am getting the first row (MIN) where the item ensuring I have the same workItemId with a release higher.
If I do not find a releaseTime, it means the item is still blocked so I populate the ReleaseFinal with NOW().
As I only want output when the item is blocked, I have an if on the last return
Next I create a new column to calculate the difference in seconds:
SecDiff = DATEDIFF(Track[AnalyticsUpdatedDate],Track[Released], SECOND)
The last column I created to sum the seconds together and poplulate the column with the IsCurrent = True:
TotalBlockedTime =
var WorkItemId = Track[WorkItemId]
return if (Track[IsCurrent], CALCULATE(SUM(Track[SecDiff]), FILTER(Track, Track[WorkItemId] = WorkItemId)))
End result, see below. I think your data is not 100% because your 3rd example Iscurrent = false (what should be true).
Enjoy!

DAX Query to Return Text Value as of 1 Month Ago

I am looking for some assistance, i am trying to create a DAX formula that will return a text value as of 1 month ago / the last entry date in a range of dates.
From the example table you see a list of codes in [JCS Grade] what i am trying to do is insert a new calculated column that will insert the previous months [JCS Grade] value, for example Employee Number 111 on Item Date 01 August 2019 has a [JCS Grade] = M2 and on 01 July they have a JCS Grade Value of M1 and i would like to return that previous months value in a calculated column [JCS1MonthAgo] but currently this is not working for me and i have tried the formula:
JCS1MonthAgo = CALCULATE(FIRSTNONBLANK(Sheet1[JCS Grade],DATEADD(Sheet1[Item Date],-1,MONTH)))
But this formula does not work as it only returns the current row value and not the prior months!
if anyone could spare a moment to assist me it would be greatly appreciated.
Many Thanks
Chris
You could use variables to do this calculation:
JCS1MonthAgo =
VAR EmpNo = Sheet1[Employee number]
VAR ItemDate_Previous = EDATE(Sheet1[Item Date],-1)
RETURN CALCULATE(MIN(Sheet1[JCS Grade]),
FILTER(Sheet1,Sheet1[Employee number]=EmpNo),
FILTER(Sheet1,Sheet1[Item Date]=ItemDate_Previous))
The idea is to apply a filter to get the correct employee number and the date of the previous month. Hope this meets your requirements.

Weeknum function is not working correctly

I got an issue using DAX function. The thing is that I'm using a dataset and I need to caclulate the date using weeks but unfortunately I can't find it directly.
So, I have created a new measure and I tried to apply the function below :
Week Number = WEEKNUM(Table1[Date],2)
And this give me the following error :
"A single value for column 'Date' in table 'Table1' cannot be determined. This can happen when a measure formula refers to a column that contains many values without specifying an aggregation such as min, max, count, or sum to get a single result."
The structure of my file is like this:
DATE N° PAGE
26/06/2018 E2 17
27/06/2018 E6 59
28/06/2018 E7 179
29/06/2018 E8 223
30/06/2018 E11 205
01/07/2018 F13 202
What am I missing ?
Thanks in advance.
I don't think this is a problem having to do with the WEEKNUM function, but has to do with the difference between a measure and a calculated column.
If you are creating a calculated column then your formula should work because DAX uses the row context to know which single date to use in the calculation.
If you are creating a measure then it is not associated with any single row, so you need to specify an aggregate so it has a single date value to work with.
Reference: Calculated columns and measures in DAX