PowerBi Circular Dependancy issue - powerbi

I have a case where I'm getting a circular dependency.
I'm trying to calculate what stock will arrive to our warehouse by a certain date, but the eta_date is a calculated date column.
The formula reporting the problem is this :
SIT Arriving Soon =
VAR PastDate = Today() - 40
VAR FutureDate = TODAY() + 14
return
CALCULATE(SUMX(OnOrder,OnOrder[Open ASN qty [units]]]), DATESBETWEEN(OnOrder[ETA Date],PastDate, FutureDate)
)
The issue is that the datesbetween function needs a column to refer to, and my column is the calculated column below, which only refers to other columns within the 'onorder' table:
to understand this formula, here is a small key:
InT is a true/false if a quantity is in transit
Open ASN qty = the qty that is in transit
I then look at different vendors and the shipping mode used to add the number of days transport time.
I then do a calculation to add to the transport time to the 'invoiced by supplier' date, and if the vendor is not one of the defined vendors, then we use the default calculated date "OnOrder[Target VSL3 Date]"
ETA Date =
Var InT = if ( OnOrder[Open ASN qty [units]]] <> 0, 1,0)
Var AAVend = if( OnOrder[Vendor] = "451633" ||
OnOrder[Vendor] = "97051583"||
OnOrder[Vendor] = "452825", "AA", "Non-AA")
Var AATransTime = if( AAVend = "AA", IF(OnOrder[Ship Mode] = "Blitz", 5, IF(OnOrder[Ship Mode] = "Air", 14, 42)), 500)
var TransTime = IF(AATransTime > 0, AATransTime, 99999)
RETURN
if ( InT = 1, DATEADD(OnOrder[Ship Date].[Date], TransTime,DAY), OnOrder[Target VSL3 Date]. [Date])
Can anyone help me to get this issue sorted, or advise a way on how I could calculate this?
Thanks very much!

Ditch the DATESBETWEEN and just filter on the date column:
SIT Arriving Soon =
VAR PastDate = Today() - 40
VAR FutureDate = TODAY() + 14
return
CALCULATE(SUM(OnOrder[Open ASN qty [units]]]), OnOrder[ETA Date] >= PastDate && OnOrder[ETA Date] <=FutureDate)
)

thanks for the reply. I tried the formula, and I still get the circular reference error. It talks about the field 'ADC invoice number'. Here is the code to that :
ADC Invoice No =
CALCULATE ( FIRSTNONBLANK ( 'ADC Movements'[ADC Invoice Number], 1 ), FILTER ( ALL ( 'ADC Movements' ), 'ADC Movements'[PoFull] = OnOrder[PoFull] ) )
I don't see how this conflicts at all.

Related

Trying to create a messure in PowerBI which give me last 6 month accuracy based on single selected value from date slicer

While executing the below DAX expression, I am getting an error "USERELATIONSHIP function can only use the two columns reference participation in relationship".
So could you please help me with that what's wrong with the expression?
Accuracy_Last_6_Month =
VAR ReferenceDate = MAX(Calender[Date])
VAR Last_6Month =
DATESINPERIOD(
Calendar_Last6Month[Date].[Date],
ReferenceDate,
-6,
MONTH
)
VAR Result =
CALCULATE(
[Accuracy],
REMOVEFILTERS(Calender[Date]),
KEEPFILTERS(Last_6Month),
USERELATIONSHIP(Calender[Date],Calendar_Last6Month[Date].[Date])
)
RETURN
Result
Relationship created between tables as inactivated form:
Columns used in both the table:
You should be able to use a single Calendar. Your second calendar is redundant.
I would write something like this:
Accuracy_Last_6_Month =
CALCULATE([Accuracy],
FILTER(ALL(Calender),
Calender[Date] > MAX(Calender[Date])-180 &&
Calender[Date] <= MAX(Calender[Date])))
I'm guessing the error is because you are using Calendar_Last6Month[Date].[Date] inside USERELATIONSHIP, which isn't actually a table column. Try deleting that .[Date] suffix everywhere in your measure:
Accuracy_Last_6_Month =
VAR ReferenceDate = MAX ( Calender[Date] )
VAR Last_6Month =
DATESINPERIOD ( Calendar_Last6Month[Date], ReferenceDate, -6, MONTH )
VAR Result =
CALCULATE (
[Accuracy],
REMOVEFILTERS ( Calender[Date] ),
KEEPFILTERS ( Last_6Month ),
USERELATIONSHIP ( Calender[Date], Calendar_Last6Month[Date] )
)
RETURN
Result
I generally avoid using these Time Intelligence suffixes entirely.

Use RankX function to calculate date rank per Fiscal Year per Entity ID

I would like to use the RankX function (to use the Dense feature for ties) in order to rank the dates in a table per two columns: Fiscal Year and Entity ID.
(I guess it may be easier to concatenate the two columns and just sort by that but I'm having issues with my formula still)
Below is my sample data:
My invalid dax formula which returns all 1's is:
Fiscal Purchase Index =
VAR a = 'Purchases'[Purchase_Date]
VAR b = 'Purchases'[Entity_ID]
var c = 'Purchases'[FISCAL_YEAR]
RETURN
CALCULATE (
RANKX ('Purchases', a,,1,Dense ),
FILTER (all('Purchases'),
'Purchases'[Entity_ID] = b &&
'Purchases'[FISCAL_YEAR] = c
)
)
Any help in fixing the rank formula would be very much appreciated.
Thank you
Use the following dax formula:
Fiscal Purchase Index =
VAR __entity = 'Purchases'[Entity ID]
var __year = 'Purchases'[FISCAL YEAR]
var __table = FILTER ( all('Purchases'), 'Purchases'[Entity ID] = __entity && 'Purchases'[FISCAL YEAR] = __year )
var __result =
RANKX(__table, Purchases[Date] ,, 1, Dense)
RETURN __result
Hope it helps.

How to create a DAX calculated column in PowerBi that finds the MIN value based on another columns FIRSTDATE?

Need some help to calculate the following in two separate columns in a DAX formula
Earliest Reading for each equip
Latest Reading for each equip
Screenshot of sheet
I've been able to get the first date of each equip with this.
CALCULATE(FIRSTDATE(Transactions[Date]),ALLEXCEPT(Transactions,Transactions[Equip No]))
But cannot work out how to get the 'Reading' value that is associated with the first date
I've managed to do this with a measure, but would also like to get this in a calc. column.
Latest Reading =
SUMX (
VALUES( Transactions[Equip No] ),
CALCULATE ( MIN ( Transactions[Reading] ), FIRSTDATE ( Transactions[Date] ) )
)
this should help you.
Earliest Column
Earliest =
VAR __equipNumber = 'Transaction'[Equip No] //Get the Equip No to filter the main table and create an auxiliar table for every different Equip No.
VAR __minDate = CALCULATE( MIN('Transaction'[Date]), FILTER( 'Transaction', 'Transaction'[Equip No] = __equipNumber ) ) //Get the lowest date asociated to every Equip No.
VAR __subTable = FILTER( 'Transaction', 'Transaction'[Date] = __minDate ) //Create a table that contains 1 row asociate to the lowest date.
Return CALCULATE(SUM('Transaction'[Reading]), __subTable) //Operate over the auxiliar table to get the expected value.
Latest Column
Latest =
VAR __equipNumber = 'Transaction'[Equip No]
VAR __maxDate = CALCULATE( MAX('Transaction'[Date]), FILTER( 'Transaction', 'Transaction'[Equip No] = __equipNumber ) )
VAR __subTable = FILTER( 'Transaction', 'Transaction'[Date] = __maxDate )
Return CALCULATE(SUM('Transaction'[Reading]), __subTable)
I obtained the expected result

Power BI: Calculating average of a column's current row divided by the first

Please see the file I have shared in the following link:
https://drive.google.com/file/d/1q7FE85qHoB_OhAm4hlmFidh6WP3m-UnK/view?usp=sharing
I have a dataset with the value of various items on different dates. I first need to calculate ratio by dividing the value of an item on a particular date with the earliest value in the chosen date filters. I then need to show the per day ratio of all items (columns I & J) as the desired output.
I am able to get the "first value" in the filtered table by using the following formula:
first_cr =
VAR item = MAX('Table 1'[item])
VAR min_updated = CALCULATE(
MIN('Table 1'[last_updated]),
'Table 1'[item] = item,
ALLSELECTED('Table 1')
)
RETURN
CALCULATE(
AVERAGE('Table 1'[cr]),
'Table 1'[last_updated] = min_updated,
'Table 1'[item] = item,
ALLSELECTED('Table 1'[fk])
)
However, I am unable to figure out how to calculate the average of all the individual item ratios at just the date level. I guess I need to use AVERAGEX somehow, but not sure how. Perhaps my first_cr formula could use more refinement.
I'd appreciate any help or guidance in the right direction. Thanks for your time.
I was able to get the answer using the following formula. If anyone can refine it better, please do so, else I'll accept my answer after a few days.
ret =
var lastUpdated = MAX(Sheet1[Date])
var tbl = ALLSELECTED(Sheet1)
RETURN
CALCULATE(
AVERAGEX(
Sheet1,
var i = Sheet1[Item]
var minUpdated = CALCULATE(
MIN(Sheet1[Date]),
Sheet1[Item] = i,
tbl
)
var first_cr = CALCULATE(
AVERAGE(Sheet1[Return]),
Sheet1[Date] = minUpdated,
Sheet1[Item] = i,
tbl
)
RETURN Sheet1[Return] / first_cr
),
Sheet1[Date] = lastUpdated,
tbl
)

How to find DateDiff from same column with condition in Power BI

I am having one table in PowerBI which is having 3 columns: 1.EnrollId 2.Status 3.StatusChangeDate. One EnrollId is having 4 statuses and their particular statusChangeDates. I want to find no. of days between two dates with status condition.
EnrollId Status StatusChangeDate
101 AppStart 15/02/2019
101 Application 27/03/2019
101 Enrollment 03/04/2019
101 Complete 28/04/2019
I want to create formula in DAX like
[StatusChangeDate (where Status="Enrollment") - StatusChangeDate(where status="AppStart)]
or
[StatusChangeDate (where Status="Complete")- StatusChangeDate(where status="Enrollment)]
i.e. 03/04/2019 - 15/02/2019 = 44 Days
28/04/2019 - 03/04/2019 = 25 Days
My advice is go for a Calculated Column, you can try and adjust this to a measure but there is more factors to consider regarding the filter context.
For a calculated column:
DaysLastChange =
VAR _currentStatusChangeDate = [StatusChangeDate]
VAR _currentEnrollId = [EnrollId]
RETURN
DATEDIFF(
CALCULATE(MAX('Table1'[StatusChangeDate]);FILTER('Table1';_currentEnrollId = [EnrollId] && _currentStatusChangeDate > [StatusChangeDate] ));
_currentStatusChangeDate;
DAY
)
If you want measures, try something like this. For each period between two Statuses, you need to create another measure. When more than one [EnrollId] is selected, the measure returns a blank.
Days Enrollment - Complete =
VAR scDateEnrollment =
CALCULATE ( MAX ( 'Table'[StatusChangeDate] ), 'Table'[Status] = "Enrollment" )
VAR scDateComplete =
CALCULATE ( MAX ( 'Table'[StatusChangeDate] ), 'Table'[Status] = "Complete" )
RETURN
IF (
HASONEVALUE ( 'Table'[EnrollId] ),
DATEDIFF ( scDateEnrollment, scDateComplete, DAY )
)
Cardvisuals or a table-visual would look like this. As you can see the table-visual is not affected by the slicer.
EDIT
This is the table i used: