DAX - check for the existance of a column in the query - dax

Is it possible, in a DAX measure, to check if the current query contains a particular column?
For example, I have a column named "Time" - is it possible to detect if a user in a self service environment has included this in their report, from the measure?
Edit - adding example of an output
An example output would be below
+---------+---------+------+--------------+
| Col1 | Col2 | Col3 | ContainsCol3 |
+---------+---------+------+--------------+
| Value 1 | Value 2 | 123 | True |
+---------+---------+------+--------------+
+---------+---------+------+--------------+
| Col1 | Col2 | Col4 | ContainsCol3 |
+---------+---------+------+--------------+
| Value 1 | Value 2 | 123 | False |
+---------+---------+------+--------------+
The query containing Col3 returns true, the query that does not include col3 returns false.

not exactly, but you can use functions like ISCROSSFILTERED, ISFILTERED, HASONEFILTER, HASONEVALUE which might be sufficient depending on your end-goal.

Related

How to create a calculated column by comparing string values from another column in Tibco spot fire

can some one please help me to create a calculated
column as explained below?
consider the table
col1 | col2 | col3
1 | x | ER
1 | x | IG
1 | x | C
1 | y | ER
1 | y | ER
2 | y | IG
2 | y | C
2 | y | ER
2 | z | ER
2 | z | IG
I need a calculated column which says 'success' if there exists at least one 'C' on col3 for group of col1 and col2, else 'fail'.
so my new table must look like:
col1 | col2 | col3 | calculated_col
1 | x | ER | success
1 | x | IG | success
1 | x | C | success
1 | y | ER | fail
1 | y | ER | fail
2 | y | IG | success
2 | y | C | success
2 | y | ER | success
2 | z | ER | fail
2 | z | IG | fail
that is:
for the combination of 1 and 'x' from col1 and col2 respectively there is at leat one 'c' at col3, so expression result is 'success'
for the combination of 1 and 'y' from col1 and col2 respectively there is no 'c' at col3, so the expression result is 'fail'
for the combination of 2 and 'y' from col1 and col2 respectively there is at least one 'c' at col3, so expression result is 'success'
for the combination of 2 and 'z' from col1 and col2 respectively there is no 'c' at col3, so the expression result is 'fail'
you can use the Intersect OVER function to gather values in cells according to a hierarchy you define (in this case, col1>col2).
the following expression produces your desired results:
If(
Find("C", UniqueConcatenate([col3]) OVER (Intersect([col1], [col2]))) > 0,
"success","fail"
)
it's not particularly robust, however: if you have any other values in col3 that contain "C", this expression will evaluate to "success"!

Column to comma separated value in Hive

It's been asked and answered for SQL (Convert multiple rows into one with comma as separator), would any of the approaches mentioned work in Hive, e.g. to go from this:
+------+------+
| Col1 | Col2 |
+------+------+
| a | 1 |
| a | 5 |
| a | 6 |
| b | 2 |
| b | 6 |
+------+------+
to this:
+------+-------+
| Col1 | Col2 |
+------+-------+
| a | 1,5,6 |
| b | 2,6 |
+------+-------+
The aggregator function collect_set can achieve what you are trying to get. Here is the documentation. So you can write a query like:
SELECT Col1, collect_set(Col2)
FROM your_table
GROUP BY Col1;
However, there is one striking difference between MySQL's GROUP BY and Hive's collect_set that while GROUP_CONCAT also retains duplicates in the resulting array, collect_set removes the duplicates occuring in the array. In the example shown by you there are no repeating group values for Col2 so you can go ahead and use it.
And there is collect_list that will take full list (with duplicates).
Try this
SELECT Col1, concat_ws(',', collect_set(Col2)) as col2
FROM your_table
GROUP BY Col1;
apache.org documentation

Joining tables with same column names - ORACLE

I am using Oracle.
I am currently working one 2 tables which both have the same column names. Is there any way in which I can combine the 2 tables together as they are?
Simple example to show what I mean:
TABLE 1:
| COLUMN 1 | COLUMN 2 | COLUMN 3 |
----------------------------------------
| a | 1 | w |
| b | 2 | x |
TABLE 2:
| COLUMN 1 | COLUMN 2 | COLUMN 3 |
----------------------------------------
| c | 3 | y |
| d | 4 | z |
RESULT THAT I WANT:
| COLUMN 1 | COLUMN 2 | COLUMN 3 |
----------------------------------------
| a | 1 | w |
| b | 2 | x |
| c | 3 | y |
| d | 4 | z |
Any help would be greatly appreciated. Thank you in advance!
You can use the union set operator to get the result of two queries as a single result set:
select column1, column2, column3
from table1
union all
select column1, column2, column3
from table2
union on its own implicitly removes duplicates; union all preserves them. More info here.
The column names don't need to be the same, you just need the same number of columns with the same datatpes, in the same order.
(This is not what is usually meant by a join, so the title of your question is a bit misleading; I'm basing this on the example data and output you showed.)

How to select id, first_not_null(value1), first_not_null(value2).. on Postgresql

I have a table like this:
+--+---------+---------+
|id|str_value|int_value|
+--+---------+---------+
| 1| 'abc' | |
| 1| | 1 |
| 2| 'abcd' | |
| 2| | 2 |
+--+---------+---------+
I need to get this:
+--+---------+---------+
|id|str_value|int_value|
+--+---------+---------+
| 1| 'abc' | 1 |
| 2| 'abcd' | 2 |
+--+---------+---------+
It seems to me that I need something like:
select id, first_not_null(str_value), first_not_null(int_value)
from table
group by id
Is there any acceptable way to do this? I use Postgresql 9.0.1.
Update: this should work with uuid types as well
You should look at http://www.postgresql.org/docs/8.1/static/functions-aggregate.html for aggregate functions.
I guess max should do the work
EDIT: Working example
select id, max(col1), max(col2) from (
select 1 as id, null as col1, 'test' as col2
union
select 1 as id ,'blah' as col1, null as col2
) x group by id

LISTAGG function with two columns

I have one table like this (report)
--------------------------------------------------
| user_id | Department | Position | Record_id |
--------------------------------------------------
| 1 | Science | Professor | 1001 |
| 1 | Maths | | 1002 |
| 1 | History | Teacher | 1003 |
| 2 | Science | Professor | 1004 |
| 2 | Chemistry | Assistant | 1005 |
--------------------------------------------------
I'd like to have the following result
---------------------------------------------------------
| user_id | Department+Position |
---------------------------------------------------------
| 1 | Science,Professor;Maths, ; History,Teacher |
| 2 | Science, Professor; Chemistry, Assistant |
---------------------------------------------------------
That means I need to preserve the empty space as ' ' as you can see in the result table.
Now I know how to use LISTAGG function but only for one column. However, I can't exactly figure out how can I do for two columns at the sametime. Here is my query:
SELECT user_id, LISTAGG(department, ';') WITHIN GROUP (ORDER BY record_id)
FROM report
Thanks in advance :-)
It just requires judicious use of concatenation within the aggregation:
select user_id
, listagg(department || ',' || coalesce(position, ' '), '; ')
within group ( order by record_id )
from report
group by user_id
i.e. aggregate the concatentation of department with a comma and position and replace position with a space if it is NULL.

Resources