Conditional Column POLL: SQL vs Power Query vs DAX - sql

Need to add a multi conditional (approx 12) column to an Excel file and was wondering what was more efficient: SQL (CASE), M/PowerQuery (if-then-else), or DAX (SWITCH).
What are you experiences?
TIA,
Danny

I would use the Conditional Column feature in the Query Editor ("M/PowerQuery") for this requirement. It's the only option where you won't need to write any code at all, even for requirements like "does not contain" that are quite complex to code in your alternatives.
I tell my data analyst clients not to bother learning SQL any more - the Power BI Query Editor / Power Query for Excel is so much more productive.

Related

Is there a way to convert this MDX syntax to DAX?

I'm changing a legacy multidimensional analysis services to tabular and "Converting" the MDX measures to DAX. (I don't have much knowledge using MDX)
I found the script below and i'm not sure what it is doing.
([Origin].[Origin].&[BUDGET FILE],[Measures].[Value])
I think it's is using the Column Value inside the Budget file located in the Origin to fill or create something in the measures.
So, first can anyone help me understand what this code is doing?
And second, would DAX support such thing?
Thanks!
Here is the equivalent assuming the Value measure is a Sum.
CALCULATE(SUM(YourTableName[Value]), 'Origin'[Origin]="BUDGET FILE")

Access dynamic query - Better to build one conditional SQL query or multiple queries with VBA?

I have a Microsoft Access 2010 form with dropboxes and a checkbox which represent certain parameters. I need to run a query with conditions based on these parameters. It should also be a possibility for no criteria from the dropdown boxes and checkbox in order to pull all data.
I have two working ways of implementing this:
I build a query with IIf statements in the WHERE clause, nesting statements until I have accounted for every combination of criteria. I reference the criteria in the SQL logic by using Forms!frmMyFrm!checkbox1 for example or by using a function FormFieldValue(formName,fieldName) which returns the value of a control with the input of the form and control name (This is because of previous issues). I set this query to run with the press of the form's button.
I set a vba sub to run with the press of the button. I check the conditions and set the query SQL to a predetermined SQL string based on the control criteria (referenced in the same way as the previous method). This also involves many If...Else statements, but is a little easier to read than a giant query.
What is the preferred method? Which is more efficient?
I don't believe you would find one way is more efficient over the other, at least not noticeably. For the most part it is simply personal preference.
I generally use VBA and check the value of each dropdown/checkbox and build pieces of the SQL query then put together at the end. The issue that you may run into with this method though is that if you have a large number of dropdowns and checkboxes the code is easy to get "lost" in.
If time to run is very key though you could always use some of the tips How do you test running time of VBA code? to see which way is faster.
After a lot of experimentation, and a bit of new information indicating having a pre-built query is faster than having SQL compiled in VBA, the most efficient and clear solution in the context of Microsoft Access is to build and save a number of dependent queries beforehand.
Essentially, build a string of queries each with an IIf dependent on a different criterium. Then you only need to run the final query. The only case where you would have to incorporate a VBA If...Else is if you need to query something more complicated than SELECT...WHERE(IIf(...)).
This has a few advantages:
The SQL is already compiled in the saved query, speeding things up.
No more getting lost in code:
There is no giant, nearly-impossible-to-edit query with way too many IIfs.
The minimal VBA code is even easier to follow.
At least for me, who's not an expert in SQL, it's convenient that I can often use the MS Access visual query builder for each part.

Performance comparison for SWITCH vs IIF?

Background: migrating legendary Excel Reporting projects into MS Access for the very need of database structure.
Limitation: Development environment is within Excel and Access. Due to large chunks of data processing and keeping of moving n-Months data, it can't afford to loop via recordsets.
Issue: In current Excel reporting platform, there are multi number of sub tools are used separately to process data. As a result ithas the priviledge to process small chunks of data by going through each row. Conditional checks are performed using IF-ELSE.
In proposed MS Access structure, these IF-ELSE are converted into IIF. Given theis situation, would like to know opinions on performance comparison of IIF vs SWITCH. Any better solutions are most welcome.
PS: after importing source, the db is auto closed to compact and repair as it "bloats". eventually db should be compatible for both ms 2003 and 2010 packages
I think that may be here is what you want:
Microsoft
The Iif function returns one of two values depending on whether the expression is true or not. The following expression uses the Iif
function to return a Boolean value of True if the value of LineTotal
exceeds 100. Otherwise it returns False.
The Switch function is useful when you have three or more conditions
to test. The Switch function returns the value associated with the
first expression in a series that evaluates to true.
I think that Switch is a VBA function (like Nz) while Iif is an SQL one. That has a lot of implications that you can explore here, on Allen Browne's site.
Those VBA function should specially be avoided in queries when working in a client-server architecture (SQL Server/Oracle or similar backend).

Converting Cognos reports to SSRS , what does XSUM command does?

We are in process of converting cognos reports to ssrs reports . i have a query which i got it from the cognos report and it is written in pervasive sql i am not sure about the language. I am able to interpret most of the query but i am stuck with one command XSUM. It has a syntax as
XSUM(col21 at col3 col7 col5 col5 for col3 col7) as col22 where col21,col3,col5, and col7 are columns from the table.
Can any one help me in understanding what the command XSUM does and what is the equivalent of it in T-sql please.
Thanks.
The X stands for extended.
Your report is probably doing some arithmetic on the application outside of the database that is generating these xsums and xmins.
The XSUM is just as Stephen has highlighted. It means calculations are being performed within the report itself. Is your report doing any roll ups or aggs across queries or subtotals in the crosstab itself or are you joining subqueries within a report. Once you have subqueries and tie them together, the arithmetic has to be performed on the application servers. Think of it as a two tier approach -> gather the data from the database that meets the criteria of the filters -> then aggregate the data on the application server before firing off to the UI.
You may want to looking at the native sql and cognos sql probably one of them may not have the Xsum.
Apart from this, will there be a semantic layer in SSRS where you have to create something similar to a package. If thats the case , would be better to recreate the report in SSRS.
Thanks
PR

MDX query issue

In analysis services, I have cube that is based on hospitalization data. For each hospitalization there are potentially 9 icd codes and these are each stored in their own field in the view on which the cube is based. These are stored in a child table in the relational database on which the SSAS database is based.
I would like to query the cube to return all rows that have a certain ICD code in any one or more of the 9 icd code fields. It seems as if it should be simple to have this sort of "OR" in the WHERE or the Filter clause, but I'm not finding the correct method.
Thanks in advance,
Jeremy Schrader
As far as i understand, you are an SQL guy and new to MDX, so that's why you have difficulties for the query.
it would be better if you tell us what are the measures you want to select with ICD codes but i am going to try to show you an mdx query sample as simple as possible. Your query should like below;
select {Measure1,Measure2,...} on columns
ICDCodeDimension.Children on rows
//{ICDCodeDimension.ICDCode1,ICDCodeDimension.ICDCode5,...} on rows
from Cube
MDX is highly advanced query language and there are many more concept you should know/learn to use it effectively.
Hope this help.
I am guessing that you'd have a dimension called [ICD Codes] with a single level called [Codes] and 9 members called [Code A] and [Code B] or whatever. Perhaps even a member for [No code] too?
In that case your query would be able to tell you the total number of hospitalisation cases for each code, for a certain time period, across all hospitals:
SELECT {[ICD Codes].[Codes].members} ON ROWS,
{[Measures].[Number of Cases]} ON COLUMNS
FROM [CubeName]
WHERE ([Time].[2010].[Quarter 1])
Thanks for both of your feedback. After I researched further (particularly an article here: http://sqlblog.com/blogs/mosha/default.aspx that uses the SUBCUBE method to give some "OR" functionality but with very poor performance) I realized that the OR construct that I was looking for requires record-level information and so doesn't work after the aggregation that SSAS performs. Thus, I need to create a field on the fact table that has the result of the SQL "OR" statement that I need.
In this case I will just create a flag for any record that has a certain range of ICD codes in any of the 9 ICD code fields. Then, I'll create a measure that gives a count of these. Luckily, the requirements of my app are that only a limited number of diagnoses need to be looked at in this way(i.e. any hospitalization that is diabetes-related,tobacco-related,etc.). I'm still curious how one would approach this if you needed to allow the user to choose any ICD code. My understanding at this point is that you would then need to revert back to plain SQL.
Jeremy