Calculating True and False in PowerPivot DAX - dax

I have a custom calculation using the number of days.
<= 5 and my DAX calc is =[Total Days Aged]<=5
=6 and <=10 and my DAX calc is =([Total Days Aged]<=10)&&([Total Days Aged]>=6)
My calculation are above which are returning 'True' or 'False expressions. How can I calculate the total number of 'true' values in the column?
I've tried the following and the IF function, but I can't get it to work.
=calculate(DISTINCTCOUNT(Query[0-5 Days]), Query[0-5 Days] = "True")
=CALCULATE(DISTINCT(Query[0-5 Days]), Query[0-5 Days] = "True")
My error message is: "DAX comparison operations do not support comparing values type of true/false of values with type text."
Thanks in advance,

It looks like you are trying to compare a logical true/false with a string. You should try replacing the string "True" in the query with logical value TRUE(). Try the query below.
=CALCULATE(DISTINCT(Query[0-5 Days]), Query[0-5 Days] = TRUE() )

Related

ruby check a range of dates using date object?

I am new to ruby. I am practising booking a holiday on a fake airbnb but I want to check if the dates I want (start_of_holiday to end_of_holiday) is available in the dates of the room(start_date - end_date) but I can't seem to compare the ranges or convert the dates into ranges. Is there a better way of checking? thank you
require 'date'
start_of_holiday = Date.parse("2022-10-02").strftime('%Y-%m-%d')
end_of_holiday = Date.parse("2022-10-05").strftime('%Y-%m-%d')
start_date = '2022-10-01'
end_date = '2022-10-11'
print true if (start_of_holiday..end_of_holiday).include?(start_date..end_date)
The problem is that you're passing a range to include? which will return false in this case as the whole range (start_date..end_date) is not in (start_of_holiday..end_of_holiday).
From the docs;
include?(obj) → true or false
Returns true if obj is an element of the range, false otherwise.
You could simply adjust it to check for start_of_holiday and end_of_holiday
(start_date..end_date).cover?(start_of_holiday) && (start_date..end_date).cover?(end_of_holiday)
# true

How do I create a DAX expression to display a calculation from a derived table?

I'm using DaxStudio to test some measures, but am having trouble getting them to work. I can run the following expression, but don't know how to run an average of the field Mean to show just the mean of that. I'm basically expecting output to be a single cell with the average.
DAX Query:
EVALUATE
FILTER(
NATURALINNERJOIN(Alldata, NATURALINNERJOIN('Label', NATURALINNERJOIN('LabelBSkill', 'LabelCSkill'))),
'LabelCSkill'[Name] = "Critical"
&& 'Label'[Type]="Red"
)
Mean is in the table Alldata if that matters
Give this a try:
EVALUATE
ROW (
"Mean", CALCULATE (
AVERAGE ( Alldata[Mean] ),
'LabelCSkill'[Name] = "Critical",
'Label'[Type] = "Red"
)
)

if value more than DAX Query

I am trying to make a simple DAX query where if the value is more than 8 it should be consider as a 8
As an example
if value is 24 consider as 8
So whenever the value is 8 or more than 8, it should be 8.
How i can do that in a DAX query or in a POWER Query !
I have search a lot here ---
https://msdn.microsoft.com/en-us/library/ee634907.aspx
but did not find any solution !
Do anyone knows any solution to this problem !
Power Query
// Add new custom field
Max8 =
if [FieldName] > 8
then 8
else [FieldName]
DAX
// Calculated column
Max8 =
IF(
'TableName'[FieldName] > 8
,8
,'TableName'[FieldName]
)
// As a measure to test another measure's return value
Max8:=
IF(
[MeasureName] > 8
,8
,[MeasureName]
)
Use the two parameter version of the MIN function:
MIN('TableName'[FieldName], 8)
This gives you the smaller of 'TableName'[FieldName] and 8.
I solved a similar situation by using AND and Nested IF
RANK = IF(AND(STUDENT[SCORE] >= 50, STUDENT[SCORE] <=100),"One" , "Two")

Linq DateTime comparison not working

I have the following code:
DateTime timeStamp = Convert.ToDateTime(Request.QueryString["TimeStamp"]);
var result = (from rs in db.VRec
where
rs.TimeStamp == timeStamp &&
rs.Fixure == wFixture
select rs).ToList();
The result shows 0 even though the correct timeStamp is passed.
If I remove the part where I do the TimeStamp comparison:
rs.TimeStamp == timeStamp
The code works fine.
Any idea on why the datetime comparison may not be working?
DateTime has a pretty fine resolution - likely you are comparing timestamps that only differ in milliseconds, which will fail. You probably want something like:
DateTime now = DateTime.Now;
DateTime then = now.Add(TimeSpan.FromMilliseconds(1));
const int EPSILON_MS = 10;
if(now.Subtract(then).TotalMilliseconds < EPSILON_MS)
{
Console.WriteLine("More or less equal!");
}
Linq converts DateTime arguments to DateTime2 in the sql query executed.
That is, when you do the comparison the actual sql executed will compare a DateTime to a DateTime2. This comparison will "cast" the DateTime to a DateTime2 and the millisecond part will be expanded to a greater resolution (in an odd way in my opinion, please enlighten me).
Try to execute the following sql:
declare #d1 datetime = '2016-08-24 06:53:01.383'
declare #d2 datetime2 = '2016-08-24 06:53:01.383'
declare #d3 datetime2 = #d1
select #d1 as 'd1', #d2 'd2', #d3 'converted'
select (case when (#d1 = #d2) then 'True' else 'False' end) as 'Equal',
(case when (#d1 > #d2) then 'True' else 'False' end) as 'd1 greatest'
From the question, I do not know if you want to compare the date with time or only the date part. If you only want to compare date then following would work
var result = (from rs in db.VRec
where
rs.TimeStamp.Date == timeStamp.Date &&
rs.Fixure == wFixture
select rs).ToList();
Since you are using some reference to db, it gives me a feeling that you are fetching your records from database (which ORM you are using is not obvious from the question or tags). Assuming that you are using Entity framework the above query will fail with exception that .Date has no direct translation to sql. If so you can rewrite the query as following to make it work.
var result = (from rs in db.VRec
where
rs.TimeStamp.Day == timeStamp.Day &&
rs.TimeStamp.Month == timeStamp.Month &&
rs.TimeStamp.Year == timeStamp.Year &&
rs.Fixure == wFixture
select rs).ToList();
The benefit of this approach is that you can compare properties to arbitrary deep level i.e you can compare Hours, Minutes,Seconds etc. in your query. The second query is tested in Entity framework 5.

LINQ Conditional Sum Calculation - cannot be applied to operands of type 'int' and 'bool'

I am trying to perform to calculation. I have a donations (d) table that contains Quantity Needed (d.QtyNeeded) and I need to determine the numbers of items still needed by pulling quantity filled (qtyfilled) from the donors table. Not every donation has a donor, so I need a conditional statement to handle nulls so the sum will work. When I try to compile, I am getting an error: *Operator '-' cannot be applied to operands of type 'int' and 'bool'. I am not great at Linq yet, what am I missing or is there a better way?
QtyOpen = d.QtyNeeded - (from dv in db.Donors
select dv).All(v => v.QtyFilled == null)
? 0
: (from dv in db.Donations
select dv.QtyFilled).Sum()
The problem is not the LINQ statement, but rather precedence in the subtraction operator. Consider this example:
int result = quantity - true ? 0 : otherValue;
This fails to compile for the exact same reason, Operator '-' cannot be applied to operands of type 'int' and 'bool'. The solution is to simply group the conditional statement in parens:
int result = quantity - (true ? 0 : otherValue);
So, your example should compile by adding parens around your entire conditional operator statement.
Try filtering out the nulls within the initial select by adding a where in the select:
QtyOpen = d.QtyNeeded - (from dv in db.Donors
where dv.QtyFilled != null
select dv.QtyFilled).Sum();

Resources