SQL server 2005 can't reference column that has been "casted" - casting

this query works:
select ist_id, ist_opis , CAST(ist_sifra as float ) as sifra
from IZOBRAZBENA_STOPNJA
where ist_aktivno<>0
order by sifra
but if I want to see only only when sifra = 5 I get "Invalid column name 'sifra'."
select ist_id, ist_opis , CAST(ist_sifra as float ) as sifra
from IZOBRAZBENA_STOPNJA
where ist_aktivno<>0 and sifra = 5
order by sifra
I tryed wraping working sql statement in another select and then using "where sifra = 5" it also fails..

yes, it is a fact. you sould do something like
where ist_aktivno<>0 and ist_sifra = 5
or
where ist_aktivno<>0 and CAST(ist_sifra as float ) =5

Because there is no column named sifra. You either cast in the where clause as well
select ist_id, ist_opis , CAST(ist_sifra as float ) as sifra
from IZOBRAZBENA_STOPNJA
where ist_aktivno<>0 and CAST(ist_sifra as float ) = 5
order by sifra
or refer the uncasted column
select ist_id, ist_opis , CAST(ist_sifra as float ) as sifra
from IZOBRAZBENA_STOPNJA
where ist_aktivno<>0 and ist_sifra = 5
order by sifra

Related

RANKX in measures

I have a table with 2 columns: client and product_name.
I need to number the product_name for each client
client
product_name
rank
1
aaa
1
1
baa
2
1
cwe
3
2
te
1
3
aaa
1
3
cwq
2
I created a column
RANKX_column =
RANKX(
FILTER(Query1,Query1[client_id] = EARLIER(Query1[client_id])),
Query1[product_id],,ASC,Dense
)
but if I apply a filter, the rank is not recalculated.
I tried to rewrite this formula for measure, but it returns an error about the function EARLIER.
Try something like:
=
VAR ThisClientID =
MIN( Query1[client_id] )
RETURN
RANKX(
FILTER( ALL( Query1 ), Query1[client_id] = ThisClientID ),
CALCULATE( MIN( Query1[product_id] ) ),
,
ASC,
DENSE
)
It is better to do it using Power Query:
You need first to group by columns [Client] -- See picture:
Resulting Table:
Then create a custom column, Here is the M Code:
Table.AddIndexColumn([AllData],"Rank",1,1)
Resulting Table:
Then Combine (Union) the all tables using The M Code:
= Table.Combine(#"Added Custom"[Rank])
Then close & apply. Return to your Main menu:
If you check your data view , The table there seems:

SQL for nested WITH CLAUSE - RESULTS OFFSET in Oracle 19c

Please suggest a way to implement nesting of (temp - results - select) as shown below?
I see that oracle 19c does not allow nesting of WITH clause.
with temp2 as
(
with temp1 as
(
__
__
),
results(..fields..) as
(
select ..<calc part>.. from temp1, results where __
)
select ..<calc part>.. from temp1 join results where __
),
results(..fields..) as
(
select ..<calc part>.. from temp2, results where __
)
select ..<calc part>.. from temp2 join results where __
For instance:
DB Fiddle
I need to calculate CALC3 in similar recursive way as of CALC
CREATE TABLE TEST ( DT DATE, NAME VARCHAR2(10), VALUE NUMBER(10,3));
insert into TEST values ( to_date( '01-jan-2021'), 'apple', 198.95 );
insert into TEST values ( to_date( '02-jan-2021'), 'apple', 6.15 );
insert into TEST values ( to_date( '03-jan-2021'), 'apple', 4.65 );
insert into TEST values ( to_date( '06-jan-2021'), 'apple', 20.85 );
insert into TEST values ( to_date( '01-jan-2021'), 'banana', 80.5 );
insert into TEST values ( to_date( '02-jan-2021'), 'banana', 9.5 );
insert into TEST values ( to_date( '03-jan-2021'), 'banana', 31.65 );
--Existing working code -
with t as
( select
test.*,
row_number() over ( partition by name order by dt ) as seq
from test
),
results(name, dt, value, calc ,seq) as
(
select name, dt, value, value/5 calc, seq
from t
where seq = 1
union all
select t.name, t.dt, t.value, ( 4 * results.calc + t.value ) / 5, t.seq
from t, results
where t.seq - 1 = results.seq
and t.name = results.name
)
select results.*, calc*3 as calc2 -- Some xyz complex logic as calc2
from results
order by name, seq;
Desired output:
CALC3 - grouped by name and dt -
((CALC3 of prev day record * 4) + CALC2 of current record )/ 5
i.e for APPLE
for 1-jan-21, CALC = ((0*4)+119.37)/5 = 23.87 -------> since it is 1st record, have taken 0 as CALC3 of prev day record
for 2-jan-21, CALC = ((23.87*4)+99.19)/5= 115.33 -----> prev CALC3 is considered from 1-jan-21 - 23.87 and 99.19 from current row
for 3-jan-21, CALC = ((115.33*4)+82.14)/5= 477.76 and so on
For BANANA
1-jan-21, CALC = ((0*4)+48.30)/5=9.66
1-jan-21, CALC = ((9.66*4)+44.34)/5=47.51
etc
You do not need to, you can just do it all in one level:
with temp1(...fields...) as
(
__
__
__
),
results1(...fields...) as
(
select ...<calc part>... from temp1 where __
),
temp2( ...fields...) as
(
select ...<calc part>... from temp1 join results1 where __
),
results2(...fields...) as
(
select ...<calc part>... from temp2 where __
)
select ...<calc part>... from temp2 join results2 where __
For your actual problem, you can use a MODEL clause:
SELECT dt,
name,
amount,
calc,
seq,
calc2,
calc3
FROM (
SELECT t.*,
ROW_NUMBER() OVER (PARTITION BY name ORDER BY dt) AS seq
FROM test t
)
MODEL
PARTITION BY (name)
DIMENSION BY (seq)
MEASURES ( dt, amount, 0 AS calc, 0 AS calc2, 0 as calc3)
RULES (
calc[1] = amount[1]/5,
calc[seq>1] = (amount[cv(seq)] + 4*calc[cv(seq)-1])/5,
calc2[seq] = 3*calc[cv(seq)],
calc3[1] = calc2[1]/5,
calc3[seq>1] = (calc2[cv(seq)] + 4*calc3[cv(seq)-1])/5
)
Which outputs:
DT
NAME
AMOUNT
CALC
SEQ
CALC2
CALC3
01-JAN-21
banana
80.5
16.1
1
48.3
9.66
02-JAN-21
banana
9.5
14.78
2
44.34
16.596
03-JAN-21
banana
31.65
18.154
3
54.462
24.1692
01-JAN-21
apple
198.95
39.79
1
119.37
23.874
02-JAN-21
apple
6.15
33.062
2
99.186
38.9364
03-JAN-21
apple
4.65
27.3796
3
82.1388
47.57688
06-JAN-21
apple
20.85
26.07368
4
78.22104
53.705712
db<>fiddle here

ALLEXCEPT not working when filtering blanks

I have a simple problem. My DAX measure does not seem to be working correctly when I filter for non-existing values. Here are some details:
Table:
Column1: A,A,A,A,A,B,B,B,B
Column2: 1,2,3,4,5,1,2,3,5
Measure = calculate(countrows(table), allexcept(column1))
Card Visual returns correct row count when I filter by column1 (any value in filtering pane)
However it returns wrong row count when I filter by column2 = "4" and Column1 = "B" (in filtering pane). It seems that it should ingore filtering by column2 and it does except when I specifically filer for value = "4". It gives "blank" result value in a card visual then.
Any ideas why?
Here's the screen. I would like to populate that blank cell with "4" (in a singe-table data model.enter image description here
In your case you dont need to add allexcept in your measure. Below code would be fine.
TestMeasure = countrows(Test_Data)
PFB screenshot
I am hoping that you have a data model as following
table name _dim1
colA
A
B
C
table name _dim2
colB
1
2
3
4
5
table name _fact
colA
colB
A
1
A
2
A
3
A
4
A
5
B
1
B
2
B
3
B
5
C
2
C
3
If you have this you can reach where you need by using following measures
Measure3 =
CALCULATE ( COUNTROWS ( _fact ), ALL ( _dim2[colB] ), VALUES ( _fact[colA] ) )
Measure9 =
VAR _1 =
MAX ( _dim2[colB] )
VAR _2 =
CALCULATE (
MAXX (
FILTER ( _dim2, _dim2[colB] <= _1 ),
LASTNONBLANKVALUE ( _dim2[colB], [Measure3] )
),
ALL ( _dim2[colB] )
)
RETURN
_2
Measure10 =
VAR _1 =
MAX ( _dim2[colB] )
VAR _2 =
CALCULATE (
MAXX (
FILTER ( _dim2, _dim2[colB] > _1 ),
FIRSTNONBLANKVALUE ( _dim2[colB], [Measure3] )
),
ALL ( _dim2[colB] )
)
RETURN
IF ( ISBLANK ( [Measure9] ) = TRUE (), _2, [Measure9] )
I don't think you can reach here from a single table like following
colA
colB
A
1
A
2
A
3
A
4
A
5
B
1
B
2
B
3
B
5
C
2
C
3

Power BI if condition if true then column with date value else NULL

The below table has 2 columns
Where Column A is a Date column and Column B is a Text column where some values are equal to "x" and some are blank.
I need to create an output column which based on the below formula
IF (
AND ( ColumnA < EOMONTH ( ColumnA, 3 ), ( ColumnB = "x" ) ),
EOMONTH ( ColumnA, 3 ),
"-"
)
I have written the following DAX formula for it:
Output =
IF (
AND (
ColumnA
< EOMONTH ( DATE ( YEAR ( ColumnA ), MONTH ( ColumnA ), DAY ( ColumnA ) ), 3 ),
( ColumnB = "x" )
),
EOMONTH ( ColumnA, 3 ),
"-"
)
I'm getting an error with this formula that NULL is not allowed in this context
Note: We can leave Blank in place of "x".
How do I write the correct DAX formula to achieve the above?
The problem with your calculation is that you are mixing different data types in the same column.
The Output column is handling a date data types with a text data types, that's why you are getting an error. The columns could only handle date or text but not both at the same time.
To fix your calculation your need to change your ELSE statement from "-" to BLANK()

Add Column Power BI from two column of different tables

I wanna add a column calculated from two column from different tables :
table 1 :
Date ; target;
19/10/2018; 52
table 2 :
Product; Duration;
P1; 1;
P2; 3;
P3; 4;
And i wanna have something like that
Product; Duration; New Column
P1; 1; (52/(1+3+4)*1)
P2; 3; (52/(1+3+4)*3)
P3; 4; (52/(1+3+4)*4)
With DAX try this as a new column for table2:
New Column = VALUES('table1'[target])/SUM(table2[ Duration])*'table2'[ Duration]
The VALUES function will work here because there is only one value in 'table1'[target]
When you expand table1 with more dates and targets like this:
You can use the LOOKUPVALUE function to retrieve the target for a specific date:
New Column =
LOOKUPVALUE ( Table1[target], Table1[Date], DATE ( 2018, 10, 19 ) )
/ SUM ( table2[ Duration] )
* 'table2'[ Duration]
Or the target form the latest date:
New Column =
LOOKUPVALUE ( Table1[target], Table1[Date], MAX ( 'Table1'[Date] ) )
/ SUM ( table2[ Duration] )
* 'table2'[ Duration]