Issue related to PowerBI DAX measure - powerbi

I am facing 1 issue related to DAX, let me show you the scenario.
DAX for ActualUpdates_60min_Te*
ActualUpdates_60min_Te* =
VAR tbl60 =
CALCULATETABLE(
VALUES(EventDetail[ShipmentKey])
,ALL(EventDetail[TimelinessBreak])
,FILTER(
EventDetail
,EventDetail[TimelinessBreak] = "<= 1 Hr"
)
) // Returns 893 records
VAR tbl30 =
CALCULATETABLE(
VALUES(EventDetail[ShipmentKey])
,ALL(EventDetail[TimelinessBreak])
,FILTER(
EventDetail
,EventDetail[TimelinessBreak] = "<= 30 Mins"
)
) // Returns 301 records
VAR CntRows = COUNTROWS(EXCEPT(tbl60,tbl30)) // Returns 21 records
VAR C = CntRows + [ActualUpdates_30min*] // Returns 914 records
Return C
If you see the Card - It is returning 914, when it is showing in a table it is showing 301.
What I need to achieve is I should have 914 in a table timelinessbreak is <= 1Hr.
Can anyone please help in achieving this.

Related

How get a list of date from an expression in POWER BI DAX

I am trying to find a way how to get the list of date when i do this expression in POWER BI with DAX :
For exemple : 16/11/2025 - 3
The output should be like this ( the last 3 Dates):
16/11/2024
16/11/2023
16/11/2022
EDITS:
I need just some edit about the Output if it possible.
My Inputs:
column1 that contains dates.
Column2 that contains the numbers
1- for exemple when i do this ( column1-Column2) :
10/04/2024 - 1 (Output)=> 10/04/2023
10/04/2023 - 0 (Output)=> 10/04/2023 ( stay the same)
10/04/2026 - 3 (Output)=> 10/04/2025 - 10/04/2024 - 10/04/2023
-And is it possible to store the dates in a list or table because i'm gonna use it later in other formula.
Thanks.
Please Crete a new table (see picture below) and paste this code:
DateTable =
VAR FixedDate =
DATE ( 2025, 11, 16 )
VAR PrevDate = FixedDate - 365
VAR TwoYearBack = PrevDate - 366
VAR ThreeYearBack = TwoYearBack - 365
RETURN
{ PrevDate, TwoYearBack, ThreeYearBack }
VERSION_2
A Better solution from #RonRosenfeld:
DateTable =
VAR FixedDate =
DATE ( 2025, 11, 16 )
VAR PrevDate = DATE(YEAR(FixedDate)-1,MONTH(FixedDate), DAY(FixedDate))
VAR TwoYearBack = DATE(YEAR(PrevDate)-1,MONTH(PrevDate), DAY(PrevDate))
VAR ThreeYearBack = DATE(YEAR(TwoYearBack)-1,MONTH(TwoYearBack), DAY(TwoYearBack))
RETURN
{PrevDate, TwoYearBack, ThreeYearBack}
And Result:

DAX IF Statement Can't Use Table Column

I have the following DAX expression:
Students_Who_Viewed = var max_report_date = TOPN(1,DISTINCT(table_a_Historical[ReportDate]),table_a_Historical[ReportDate])
var last_week_report_date = max_report_date - 2
/////
var this_week_student_viewed = CALCULATE(
SUM(table_a_Historical[CntViewedByStudent_CY]),
KEEPFILTERS(table_a_Historical[ReportDate] = max_report_date)
)
/////
var last_week_student_viewed = CALCULATE(
SUM(table_a_Historical[CntViewedByStudent_CY]),
KEEPFILTERS(table_a_Historical[ReportDate] = last_week_report_date)
)
/////
var students_viewed_wow_change = COALESCE(this_week_student_viewed,0) - COALESCE(last_week_student_viewed,0)
/////
RETURN
IF (
**table_a_Historical[exempt_status_cy] = "exempt" || table_a_Historical[exempt_status_py],**
"N/A",
IF (
students_viewed_wow_change > 0 //This means views this week is more than last week.
,1
,IF (
students_viewed_wow_change < 0 //This means there were less views this week than last week
,-1
,0 //This value will be used if there is no change in views this week relative to last week.
)
)
)
In the part surrounded by **, I keep getting an error message. Those columns are valid in that table. Why is this?
As DAX search for aggregation, can you please try this below where you directly referred to the column name:
min(table_a_Historical[exempt_status_cy])
Hopefully this will work.

PowerBi Circular Dependancy issue

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.

QoQ changes by Customer in DAX

I would like to create a DAX formula to calculate the increases and decreases of customers across periods. Following is a sample of the data that I have
Year-Quarter|Customer|Credit-Limit
2019Q2|A|50
2019Q2|B|100
2019Q2|C|100
2019Q2|D|200
2019Q2|E|1000
2019Q2|F|200
2019Q3|A|50
2019Q3|B|200
2019Q3|C|100
2019Q3|D|50
2019Q3|E|500
2019Q3|F|300
I am looking to create a summary by Year-Quarter showing the number of customers that had an increase/decrease/none of their Credit-Limit.
Note that this is just a sample and the actual data is >10M rows. So even though I can create another table, I think from a computation standpoint, a measure would be more useful
Desired Output:
A commentary like the following: "There are 2 customers that have increased credit limit in 2019Q3"
Done so far:
Prev Quarter Credit Limit =
VAR CurrentYearQuarter = MAX(Sheet1[Year-Quarter])
VAR Quarter_Year =
LEFT (CurrentYearQuarter, 4)
VAR Quarter_period =
RIGHT (CurrentYearQuarter, 1 )
RETURN
IF (
Quarter_period = "1",
CALCULATE (
SUM ( Sheet1[Credit Limit] ),
Sheet1[Year-Quarter]
= ( Quarter_Year - 1 )
& "Q"
& ( Quarter_period + 3 )
),
CALCULATE (
SUM ( Sheet1[Credit Limit] ),
Sheet1[Year-Quarter]
= Quarter_Year
& "Q"
& Quarter_period - 1
)
)
Inc/Dec = IF(SUM(Sheet1[Credit Limit]) - [Prev Quarter Credit Limit] > 0,"Inc",
IF(SUM(Sheet1[Credit Limit]) - [Prev Quarter Credit Limit] < 0,"Dec","None"))
Commentary = "There are " &
CALCULATE(DISTINCTCOUNT(Sheet1[Customer]),
FILTER(Sheet1, [Inc/Dec] = "Inc" && Sheet1[Year-Quarter] = "2019Q3"))
Current output:
Commentary: "There are 4"
I am not sure why I am getting 4 as compared to 2 as the number here. Help or guidance would be really appreciated
I've slightly altered the input data for experimental purpose, I've added
2018Q3 | A | 200
2018Q4 | A | 50
2019Q1 | A | 50
I've added a Quarter-Calendar (which is a calculated table using VALUES(Sheet1[Year-Quarter])
Then by adding more columns to this new table, extracting the current year and quarter using LEFT and RIGHT, then calculating the previous quarter, previous year and combining to a Prevoius-Year-Quarter column:
]
Using this Q-Calendar I create a 1:* relationship to the Sheet1 table between [Year-Quarter] and [Year-Quarter], then I create a second inactive 1:* relationship between [Previous-Year-Quarter] and [Year-Quarter] like this:
I then create two measures, one for sum of previous quarter credit an one for current quarter credit:
Current-Quater CL =
var currentQ = MAX('Q-Calendar'[Year-Quarter])
var tempTable =
FILTER(
ALL('Q-Calendar');
'Q-Calendar'[Year-Quarter] = currentQ
)
return
CALCULATE(
SUM('Sheet1'[Credit-Limit ]);
tempTable
)
In the [Previous-Quarter CL] measure I use USERELATIONSHIP to activate the passive relationship I added from the Q-Calendar.
Prev-Quater CL =
var currentQ = MAX('Q-Calendar'[Year-Quarter])
var tempTable =
FILTER(
ALL('Q-Calendar');
'Q-Calendar'[Year-Quarter] = currentQ
)
return
CALCULATE(
SUM('Sheet1'[Credit-Limit ]);
tempTable;
USERELATIONSHIP('Sheet1'[Year-Quarter]; 'Q-Calendar'[Previous-Year-Quarter])
)
Then create the Inc/Dec measure like this:
Increase/Decrease =
var temp = [Current-Quater CL]-[Prev-Quater CL]
return
SWITCH(
TRUE();
temp > 0; "Increase";
temp < 0; "Decrease";
"No change"
)
And finally created a new (actually 3 new) Commentary measures like this:
Commentary_2 = "There are " &
var customers = VALUES(Sheet1[Customer])
return
SUMX(
customers;
CALCULATE(
IF(
[Current-Quater CL]-[Prev-Quater CL] > 0;
1;
0
)
)
)&" customers how have increased their credit"
Using the Year-Quarter column from the Q-calendar as a slicer I get the current status and I can select a previous quarter to see the status at that time:
N.B: The code in my measures can be optimised, I just kept them as detailed as this to make it more understandable.

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: