Why does operator <= (less than or equal) not return expected result when date is variable? - powerbi

Problem: DAX is not returning expected result in if statement with operator when date is a variable.
Background: I've checked to make sure that there is no date or time difference.
Dates are ALL in format (1stday of month, 12:00:00AM). I have 2 years (2018 & 2019: 24 distinct dates).
I've tried to use the <= operator in a calculated column to determine if a date is "Before/Same" to a variable date or "After". I've tried to combine < filter and = filter with || (or), same result.
TestColumn =
var CurrentDate = [Current_Period]
return
IF(
ValuesTable[Month-Year] <= CurrentDate,
"Before/Same", "After"
)
FYI: The [Current_Period] Measure, this works fine, returns one date as expected
Current_Period =
VAR ThisActMonth =
CALCULATE (
DISTINCT ( Fact_FX[Month-Year] ),
Fact_FX[bool_Latest_FX_Act] = TRUE ()
)
RETURN
ThisActMonth
I would expect that for every row where the Month-Year (Date) <= the result would be "Before/Same";
however, The result I currently get is:
"Before/Same" only where ValuesTable[Month-Year] = CurrentDate
CurrentDate = April 2019
"After" all other Months (23)
Please Help!

What's happening is that you are evaluating your Current_Period measure within the row context of the calculated column, so the measure doesn't see the row with Fact_FX[bool_Latest_FX_Act] = TRUE () except in the April row.
The solution is to calculate the Current_Period outside of that evaluation context and there are multiple possible approaches. A quick solution would be to remove row context using the ALL function inside of your CALCULATE:
Current_Period =
VAR ThisActMonth =
CALCULATE (
DISTINCT ( Fact_FX[Month-Year] ),
ALL( Fact_FX ),
Fact_FX[bool_Latest_FX_Act] = TRUE ()
)
RETURN
ThisActMonth

Related

AND function in Power BI

I'm trying to create a new mesure in order to create a gantt chart, but the dax field keeps spitting that there's an error. Where in fact i literally copied the same code in the tutorial i'm following.
And the new mesure is as follows:
CF Gantt =
var startdate =
CALCULATE(
min(Contracts_range[Start Date ],
REMOVEFILTERS(TabelaCalculada)
), var EndDate =
CALCULATE(
min(Contracts_range[End Date]),
REMOVEFILTERS(TabelaCalculada)
)
var ProjectPeriod =
min(TabelaCalculada[Date]) >= startdate
&& min(TabelaCalculada[Date]) <= startdate
var result =
if(
TabelaCalculada,
1
)
return
You see PBI is saying the following: "the syntax for '&&' is incorrect", i even searched the documentation for this particular AND method but i don't see how it's wrong.
This DAX is invalid in multiple ways which I point out on the assumption that you are trying to learn what valid DAX is.
Comma after the definition of var startdate.
Project Period is a bunch of filters with no actual function.
Project Period is trying to filter on an aggregate, which is not allowed.
return statement is missing the return argument
var result is incomprehensible.
This will return a 1 for each row where the date falls between minimum Start Date and maximum End Date, per your latest comment.
CF Gantt =
var startdate =
CALCULATE(
min(Contracts_range[Start Date]),
REMOVEFILTERS(TabelaCalculada)
)
var EndDate =
CALCULATE(
max(Contracts_range[End Date]),
REMOVEFILTERS(TabelaCalculada)
)
var result = if(
selectedvalue(TabelaCalculada[Date] >= startdate
&& TabelaCalculada[Date] <= EndDate,1)
)
return result
I've found a third syntax alternative that worked. So my goal as i explained was to create a Gantt Chart using the matrix innate visual and for that one way to do it is to create a measure that returns a 1 if i have a date that's either on or after the starting date and on or before the ending date.
BetweenDates =
VAR beginning = min ( Contracts_range[Start Date])
VAR finish = max ( Contracts_range[End Date] )
VAR duration = max ( Contracts_range[Duration] )
VAR colorVar = 0
RETURN
SWITCH (
TRUE(),
AND ( MAX ( 'Calendar'[Date] ) >= beginning , MIN ( 'Calendar'[Date] ) <= finish ) && duration >0,1
)

Dax - How to get the max value from a column compare with Selected date and row by row

My Dax dont work..
ERROR:
A single value for column 'Datum' in table 'tabell' 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.
Q:
How to i get the desired values from my dax to get the latest date?
I want it to return the newst date but if its older then the selected values i want it to return that value. I have done a dax but you see the error above.
I want every row to return the Datum or the selected values depedning on the logic.
Selected values '2022-09-01'
desired value = MAX('tabell'[Datum],selectedvalue(vwdatum[datum].[date]))
Datum desired value
2020-09-25 2020-09-25
2020-09-22 2020-09-25
2020-01-02 2020-09-01
alternativ
IF('tabell'[Datum]>selectedvalue(vwdatum[datum].[date]),'tabell'[Datum],selectedvalue(vwdatum[datum].[date])
Maybe It is much better to create a calculated column, and use this DAX code to evaluate it on a row_by_row basis:
Just write your selected date value into the variable:
Desired Value =
VAR SelectedValue =
DATE ( 2020, 09, 01 )
RETURN
IF ( Tabell[Datum] > SelectedValue, MAX ( Tabell[Datum] ), SelectedValue )
If we test it, It gives us:
SelectedEnd Code For #Jonas
SelectedEnd =
VAR aret =
SELECTEDVALUE ( 'vwDimDatum2 Slut'[År],2020 )
VAR manad =
SELECTEDVALUE ( 'vwDimDatum2 Slut'[Månad nr], 01 )
VAR datumet =
DATE ( aret, manad, 01 )
VAR sista =
EOMONTH ( datumet, 0 )
RETURN
sista

How to get the value at the latest date, where values are not null?

I have a table like this. Notice the blank values towards the bottom.
I would like to get the value of the latest date, but the value can't be null.
My DAX formula is bringing back the value of the latest date (19/12/2021) which is nullhowever I want to bring back the latest non-null value, which is for the date 21/11/2021.
Here is what I have tried so far:
Latest Value =
CALCULATE(
// get sum of value column
SUM('Table1'[Value]),
// where value is not blank, and date is max date
'Table1'[Value] <> BLANK() && Table1[Date] = MAX(Table1[Date])
)
I thought this should bring back the figure 305? Because my conditions are:
where value is not null AND where date = max date
Shouldn't the max date now be 21/11/21 because the nulls have been removed?
Another piece of DAX I've tried, using the fiter function.
Latest Value = CALCULATE(
SUM('Table1'[Value]),
FILTER(ALL('Table1'),
'Table1'[Value] <> BLANK()
&&
'Table1'[Date] = MAX('Table1'[Date]))
Where am I going wrong? I think it's something to do with my max date section.
Unfortunately all file hosters are blocked in work, so I can't share this dummy file.
The idea is to filter the table first and get the max value from the date column. In my case, I saved that date in a variable last_date. Then we just select a value from the Value column using filter by last_date.
LatestValue =
VAR last_date =
CALCULATE ( MAX ( 'Table1'[Date] ), 'Table1'[Value] <> BLANK () )
RETURN
CALCULATE ( SELECTEDVALUE ( 'Table1'[Value] ), 'Table1'[Date] = last_date )
or the same expression with SUM:
LatestSumOfValues =
VAR last_date = CALCULATE(MAX('Table1'[Date]),'Table1'[Value] <> BLANK())
RETURN
CALCULATE(SUM('Table1'[Value]),'Table1'[Date] = last_date)

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.

PowerBI: Total Count Distinct

I need some help with my measure counting correct in total.
This is the MainTable:
With my Measure "CountPass" I count every ID distinct with the action "pass":
CountPass = CALCULATE(DISTINCTCOUNT(Workflow[ID]),Workflow[action]="pass")
Furthermore with my Measure CountPassPreweek I do same with reference on previous Week by using the a date table:
CountPassPreweek =
var currentweek = SELECTEDVALUE(DateTable[WeekNum])
var currentweekday = SELECTEDVALUE(DateTable[WeekNo])
var currentyear = SELECTEDVALUE(DateTable[Year])
var maxweeknum = CALCULATE(MAX(DateTable[WeekNum]),all(DateTable))
Return
SUMX(
if(currentweek = 1,
DateTable[WeekNo] = currentweekday && DateTable[WeekNum] = maxweeknum && DateTable[Year] = currentyear - 1,
DateTable[WeekNo] = currentweekday && DateTable[WeekNum] = currentweek -1 && DateTable[Year] = currentyear)),
[CountPass]
)
This is working so far but not showing the totals, so I have a second measure for doing that:
CountPreweekTotal =
var _table = SUMMARIZE(DateTable,DateTable[Date],"_value",[CountPassPreweek])
return
SUMX(_table,[_value])
And here you see my problem: The measure doesn't count distinct like the "original" counting Measure as you see here
Hope somebody can help me with that.
Thanks a lot!
It's counting 3 since abc is getting counted twice the way your measure is written (since dates are separated in your SUMMARIZE).
Since you appear to have a proper date table, you should be able to use time intelligence functions to write this much more simply as
CountPassPreviousWeek =
CALCULATE ( [CountPass], DATEADD ( DateTable[Date], -7, DAY ) )
This should work for the total too.