When I try to sort using the "case" by the field of the join table, the following error is returned:
PG::InvalidColumnReference: ERROR: for SELECT DISTINCT, ORDER BY
expressions must appear in select list
<Table1>.includes(:difficulty_level).order(difficulty_level_priority('ASC'))
def difficulty_level_priority(direction)
"
CASE
WHEN difficulty_levels.name = 'first' THEN '1'
WHEN difficulty_levels.name = 'second' THEN '2'
WHEN difficulty_levels.name = 'third' THEN '3'
WHEN difficulty_levels.name = 'last' THEN '4'
END #{direction.upcase}"
end
But a simple sort by name works correctly. Like this:
<Table1>.includes(:difficulty_level).order('difficulty_levels.name ASC')
I tried pasting the select('"difficulty_levels".*') and group(< some columns>) after includes(). It did not help.
It looks like you are trying to sort by the field of the join table, but the field you are trying to sort by is not in the select list. You need to add the field you are trying to sort by to the select list.
lets say i have a column with many lines but only two values, A and B:
i am trying unsuccessfully to count only lines with A - in a summary calculation for a dashboard (without making a new column for this specific calculation)
the expression which gives me syntax error is this:
count([column] = 'A')
any suggestion?
You'll need to use an if then else construct:
count( if([Column]='A') then ([Column]) else (Null))
You can use IF-THEN-ELSE or CASE-WHEN-THEN-ELSE to create your own count:
sum(
if ([Query Item] = 'A')
then (1)
else (0)
)
or
sum(
case
when [Query Item] = 'A'
then 1
else 0
end
)
12 Total :=
SWITCH (
TRUE (),
HASONEVALUE ([level04] ), CALCULATE (
[Local],
DATESINPERIOD (
Calendar[Date],
MAX ( Calendar[Date] ),
-12,
MONTH
)
),
HASONEVALUE ([level03]),If([level 03]= "calculation:" && [level03]= "Cash:" && [level03]= "Costs:"=>0, Blank(), CALCULATE (
[Local],
DATESINPERIOD (
Calendar[Date],
MAX ( Calendar[Date] ),
-12,
MONTH
)
)
),
HASONEVALUE ( [level02] ), BLANK ()
)
I would like to add condition that if lever 03 = cash, calculation, and cost then return blank to remove sub total rows. I tried something like that and its not working properly it gives me error "value for column level03 cannot be determined in the current context". how can i add that condition in level03 column?
Just a side note, "its not working properly" is tremendously unhelpful in determining the source of problems. Specifically in a Power Pivot/Tabular model, this link provides a list of ways to help answerers help you.
Syntactically, there are some errors. Let's address those and see if the behavior is appropriate before diving into alternate measure definitions.
This section is the problem:
...
,HASONEVALUE([level03])
// Note, you are inconsistent in referring to [level03] and
// [level 03]
,If( // Below you are testing column references against scalar
// literals. DAX doesn't understand how to do this, so you
// have to wrap each reference in a VALUES() function, which
// can be implicitly converted to a scalar value
[level 03] = 'calculation:' // DAX needs double quotes: ""
&& [level03]= 'Cash:' // You have multiple predicates
&& [level03]=' Costs:'=>0 // for a single field combined
// with a logical and - these
// can't all simultaneously be true
,Blank()
,CALCULATE(
[Local]
,DATESINPERIOD(
Calendar[Date],
MAX ( Calendar[Date] ),
-12,
MONTH
)
)
)
,....
I am just going to change the combination of predicates to a logical or in my rewrite, so that it's possible for this IF() to return a BLANK. The way it's written above, even fixing the syntax errors will cause it to always evaluate the else condition, because [level 03] cannot simultaneously have the value of "calculation:", "Cash:", and " Costs:=>0".
...
,HASONEVALUE('<table>'[level03])
// As a best practice, you should always use fully qualified
// column references - 'table'[field]
// Also, I've referred only to [level03] below
,IF( // Note we've wrapped [level 03] in VALUES() below - this will
// allow our comparisons to scalar literals
VALUES('<table>'[level03]) = "calculation:" // double quotes
|| VALUES('<table>'[level03]) = "Cash:"
// Note: we've changed the logical and, &&, to a logical
// or, || - meaning if any 1 of these predicates is true,
// we'll return BLANK
|| VALUES('<table>'[level03]) = " Costs:=>0"
,BLANK()
,CALCULATE(
[Local]
,DATESINPERIOD(
Calendar[Date]
,MAX( Calendar[Date] )
,-12
,MONTH
)
)
)
,....
What this will do is check for any cell on the pivot table where [level03] has exactly one distinct value - when this is true, it will evaluate the IF() function.
The IF() function will check the value of [level03]. If that value is any one of the three following values, "calculation:", "Cash:", or " Costs:=>0", it will return BLANK. If the value of [level03] is not any of those three, it will evaluate the CALCULATE(), which returns the measure [Local] for a rolling 12 month period.
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() )
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();