SSRS sorting on a date field containing alphanumerics - sorting

I am using SSRS 3.0, and SharePoint 2013. I am sorting an SSRS report on a date field. I've used expressions to plug some of the dates as alphanumeric (ie “N/A”, “Ongoing”) The customer wants the rows sorted by date with the oldest dates first and the “N/A” and the “Ongoing” dates last. The Sort function is sorting the alphas first, then the other rows that contain dates. How do I get the Sort expression to sort the dates oldest to newest and then sort the “N/A” and “Ongoing” dates last? Thank you!

You can create a computed sort expression either in the SQL or in the report. I'll give an example of doing it in the report since that's what you asked.
First, go into your group properties and then the Sorting tab. We'll add two sorting conditions. Click the "Add" button and then click the expression button.
For the first condition, we'll separate the dates and the text. So the expression would be something like this:
=IIf(IsDate(Fields!MyColumn.Value), 1, 2)
Add a second sort condition and set it to sort your column in descending order. This will put the dates first and the text after it.

Separate the result set into two parts, one for the pure date result, and the rest for 'ongoing' & 'N/A'. Combine them using UNION.
Which will be the query like this:
Select col1, col2
From table
where ... --condition to filter out those rows with real date
order by date
UNION
Select col1, case when col2 ... then 'Ongoing' when col2 ... then 'N/A'
From table
where ... --condition to filter out those rows need to be replace with characters
--not necessary using `order by for this part`

You can simply use the ORDER BY clause as follows:
SELECT MyDate FROM MyDateTable
ORDER BY
CASE WHEN MyDate IN ('n/a','Ongoing') THEN 1 ELSE 0 END,
MyDate

Related

Google Sheets Query - use a reference cell to select which columns to select in a query

I would like for a Google Sheets Query to choose which columns to select based on a cell reference. "select X" for example where the column header equals a cell that I reference. This is different from selecting which rows of a column to display. I want the Colx to be based on a cell reference.
Specifically, the data has different column headers with dates. I want the query to select the column that matches a particular date or any of the headers I may have in a drop down menu.
I used this format but it did not work
=query('Schedules'!A:N,"select A,B,C,D,E,K,H,J,L,M, '"&A2&"' WHERE N=1 and K !='HR(PD)' ORDER BY B,K",1)
This is the cell reference to the column I am looking for: '"&A2&"'
in A2 I have the letter of the column I want to use
Your current formula maybe a bit spotty with the quotes placement. You can see a working sample here:
=query(A:D,"Select A,"&F1&" Where A is not null")
based on header match
=query({A:D},"Select Col1, Col"&xmatch(F1,A1:D1)&" Where Col1 is not null")

Sort by Month in dropdown in power bi

I drag slicer and add Month_paid field in that and convert that one to dropdown
now month looks like
Feb , aPRIL , June , Jan , Dec
i want to sort these months in dropdown how i should do this
this is the link of sample file
https://www.dropbox.com/s/sp9zfyq2sccgmmi/Claims_test_model_2.pbix?dl=0
will you please check ?
You can create a "sorting" calculated column by using the following DAX (or something similar):
Month Sort =
MONTH(Claims_excel[CLM_LOSS_DT])
Once this has been created, right click on the Claims_excel[CLM_LOSS_MONTH] and click the 'Sort by' option and choose the new column.
This will produce the following results:
If you have actual dates in your field then create a calculated column using the following logic:
YearMonthSort = YEAR(Claims_excel[CLM_LOSS_DT])*100 + MONTH(Claims_excel[CLM_LOSS_DT])
You will need another integer column in that table with numbers 1 to 12 representing the order of the Months. You can create that column in either DAX or M. When created, just select the Month column and click on 'Sort By Column' option in the Modeling tab and select the integer column which was created.This will sort the order of the months in the slicer.
If the month data in the column is already in an order starting from January to December then a simple Index column (from 'Add Column' tab in Power Query editor) is enough, else a calculated column which will check the month column value for each row and put the corresponding order number in the newly calculated integer column will be required. Refer this https://radacad.com/sort-by-column-in-power-bi

How can I use a variable to pick which column is selected?

How can we use variable as column name ?
In my table days (MONDAY,TUESDAY..) are column names.
I want to get the DAY dynamically and use AS COLUMN NAME in my query.
My query :
SELECT EMP FROM SCHEDULE WHERE "DAY"(Dynamically I want) =1;
You simply can't use variables to change the actual text of the queries. variables can be used just in place of literal values (dates, strings, times, numbers) but they can't change the text of the actual command.
The technical reason is that (oversimplyfying the things) oracle FIRST parses the text, establishes an execution plan and only after this considers the values of the variables. more or less you can think (this is just an analogy, of course, it is not really the same thing!) that oracle "compiles" the queries like an C++ compiler compiles the source code of a function: it is not possible to pass a c++ procedure a variable that modifies the text of the procedure itself.
what you have to do is to rethink your approach taking in consideration what I just said:
SELECT EMP FROM SCHEDULE
WHERE
(case :DAY_I_WANT
WHEN 'MONDAY' then -- 'MONDAY' is the string value of the variable :DAY_I_WANT
monday -- monday, here is the column whose value I want CASE to return
WHEN 'TUESDAY' then tuesday
...
...
WHEN 'SUNDAY' then sunday
end) = 1
keep in mind that this solution will not take advantage on any index on the MONDAY..SUNDAY columns. the best approach would be to create a different data structure where you have a separate row for each day and a proper dayofweek column. If you do this, you will be able able to write:
select EMP from schedule
where schedule.DAY = :DAY_I_WANT
and it will allow you to create an index on the DAY column, speeding up searches.
Having a separate column for each day equals to be looking for troubles.

How MAX of a concatenated column in oracle works?

In Oracle, while trying to concatenate two columns of both Number type and then trying to take MAX of it, I am having a question.
i.e column A column B of Number data type,
Select MAX(A||B) from table
Table data
A B
20150501 95906
20150501 161938
when I’m running the query Select MAX(A||B) from table
O/P - 2015050195906
Ideally 20150501161938 should be the output????
I am trying to format column B like TO_CHAR(B,'FM000000') and execute i'm getting the expected output.
Select MAX(A || TO_CHAR(B,'FM000000')) FROM table
O/P - 2015011161938
Why is 2015050195906 is considered as MAX in first case.
Presumably, column A is a date and column B is a time.
If that's true, treat them as such:
select max(to_date(to_char(a)||to_char(b,'FM000000'),'YYYYMMDDHH24MISS')) from your_table;
That will add a leading space for the time component (if necessary) then concatenate the columns into a string, which is then passed to the to_date function, and then the max function will treat as a DATE datatype, which is presumably what you want.
PS: The real solution here, is to fix your data model. Don't store dates and times as numbers. In addition to sorting issues like this, the optimizer can get confused. (If you store a date as a number, how can the optimizer know that '20141231' will immediately be followed by '20150101'?)
You should convert to number;
select MAX(TO_NUMBER(A||B)) from table
Concatenation will result in a character/text output. As such, it sorts alphabetically, so 9 appears after 16.
In the second case, you are specifiying a format to pad the number to six digits. That works well, because 095906 will now appear before 161938.

Oracle random row from table

I found this solution for selecting a random row from a table in Oracle. Actually sorting rows in a random manner, but you can fetch only the first row for a random result.
SELECT *
FROM table
ORDER BY dbms_random.value;
I just don't understand how it works. After ORDER BY it should be a column used for sorting. I see that "dbms_random.value" returns a value lower than zero. This behavior can be explained or is just like that?
Thanks
you could also think of it like this:
SELECT col1, col2, dbms_random.value
FROM table
ORDER BY 3
In this example the number 3 = the third column
When you order by dbms_random.value, Oracle orders by the expression, not for a column.For every record Oracle calculate a random number, and then order by this number.
In a similar way, is like this:
select * from emp order by upper(ename);
You have an order by based on a function.

Resources