Query calculated field with IIF statement - excel

I have an Access table called Snaps which I want to query from Excel in VBA.
The following query works:
SELECT SUM(Cash) FROM Snaps WHERE Balance>50 AND SnapshotDate=#2020-06-30 00:00:00#
If I add another condition on a calculated field created in Access which I call IsEligible, and the value of which is a boolean, then, none of the following works:
SELECT SUM(EligibleNotionalBalance) FROM Snaps WHERE ArrearsStatus=Current AND SnapshotDate=#2020-06-30 00:00:00# AND IsEligible = -1
SELECT SUM(EligibleNotionalBalance) FROM Snaps WHERE ArrearsStatus=Current AND SnapshotDate=#2020-06-30 00:00:00# AND IsEligible = TRUE
SELECT SUM(EligibleNotionalBalance) FROM Snaps WHERE ArrearsStatus=Current AND SnapshotDate=#2020-06-30 00:00:00# And IsEligible = 'true'
I have add similar issues in the past, which I overcame by changing the formula in my calculated field. Essentially this consisted in removing the IIF statetement that I had. However, it only showed values when the result was False, which I don't really like. Does anyone have a guess?
Thanks!

So, not sure about the Group by and Having in this case. However, it now works. It seems I had some connection issues, which I realised after shrinking the query to the most simple possible format.
The " =-1 " actually works!

Related

Excel Power Query truncating text field to 1024 characters

I am accessing a SSAS DMV through Power Query in Excel via:
let
Source = AnalysisServices.Database(TabularServerName, TabularDBName,
[Query="select * from $SYSTEM.TMSCHEMA_EXPRESSIONS"])
in
Source
This works great in Power BI, but in Excel, the Expression column is limited to a max of 1024 characters. How do I get Power Query in Excel to give me the entire value? My largest values are around 15000 characters, so still within the stated limits of Power Query that I can find.
If I set up a table with a connection and query behind it, Excel can pull in the entire Expression column, but the downside is the server and database cannot be parameterized and have to be manually changed in the connection. Also I don't remember how to do this manually, so I always have to access the DMV from DAX Studio and export to Excel to set it up!
Update
I did some heavy transformations of this column. I parsed out a value, I used it to merge the file with itself and add a column that I then did a bunch of transformations on, and then used it to replace text within the original problem column. And something in that pulled in the whole value. I tried just doing small parts of this, like adding a column that referenced the problem column, or doing a replace in the problem column, and none of that worked.
So, no, not easy to duplicate or figure out which step fixed it, but for my purposes, I now have what I need.
I think it is related to the type of the column your are loading in Excel. I had the same issue and read your answer (with Table.ReplaceValue).
Your solution is hiding the initial point : The function used in the expression you shared for Table.ReplaceValue() is Replacer.ReplaceText that as the additional specificity to convert a field of type Any
to type Text.
I tried to juste change the type of my field that was truncated when loaded in Excel, from type Any to type Text. Result : the complete values were then loaded in my worksheet.
I had to change this query today, and after I changed it, the values were truncated again. I added a Replace Value step at the end of the query on the truncated column and that seemed to fix it.
#"Replaced Value" = Table.ReplaceValue(#"Last Step","in ","in ",Replacer.ReplaceText,{"Truncated Column Name"})
in
#"Replaced Value"

Query with switch function requests parameter value [duplicate]

This question already has answers here:
Why does this query require a parameter?
(2 answers)
Closed 3 years ago.
Background: Excel worksheet as front end to an Access database. Excel contains VBA code and uses ADODB to move values into and out of the tables in Access.
One column is a composite value which describes a complex priority relationship. One digit for priority level (1-6), one letter for more granular level (A-C), and a type of project (Safety, Security, Enviro, etc.) The values look like "2B - Maint.", "2A - ProcCntl"
I need to sort the results of a select * query in different ways. Sometimes with the typical Alpha sort which would list 1A values before 1B before 4C. Other times I need to sort all the Safety as a group, Maint. as another, etc. A simple alpha sort on the project type doesn't work because the manager wants a specific order.
I am using the Switch function to assign the order. I have found a couple of ways to make this work in Access but they all ask for a parameter value when I run the query. Entering a blank value gets the desired result. Moving the query into the VBA of Excel returns an error complaining of an empty parameter.
Here is one of the "working" queries:
SELECT *, Mid([Priority],6,2) AS ProjClass,
Switch(ProjClass="Sa",1,ProjClass="En",2,ProjClass="Se",3,ProjClass="6S",4,ProjClass="Qu",5,ProjClass="Pr",6,ProjClass="Ma",7,ProjClass="Bu",8)
AS [FirstSort]
FROM tblProjects
ORDER BY [FirstSort] ASC, [Priority] ASC;
When I run this through the VBA I get
Run-time error "No value given for one or more required parameters."
When I run it in Access a "Enter Parameter Value" dialog opens asking for FirstSort. I click through and see the result is correctly sorted.
The root question: Why is Access seeing FirstSort as a parameter and not a field name?
I think I need to understand that before I can fix the VBA issue.
More research has revealed yet another Access quirk: The problem is that MS Access does not allow AS clauses in the ORDER BY clause.
So a little experimentation lead me to change the ORDER BY clause to ORDER BY 3 ASC, [Priority] ASC;
I chose the three because it is the third field expression in the query. It works but it would be great if someone could clarify the specific rule that makes it work.
Hat tip: Why does this query require a parameter? and onedaywhen's answer.
ProjClass is a field created during execution of the query. Before that when the SQL engine is determining an execution plan for the query it will not recognize that field name so assumes it is parameter. SORT is done at the end of the query and by that time the engine knows which is the third field.
An alternative is to create another table called something like tblPriority with 2 columns ProjectClass and SortOrder
ProjClass SortOrder
Sa 1
En 2
Se 3
6S 4
Qu 5
Pr 6
Ma 7
Bu 8
and use a JOIN
SELECT A.* FROM tblProjects AS A
LEFT JOIN tblPriority as B
ON B.ProjClass = Mid(A.Priority,6,2)
ORDER BY SortOrder

How to present different column headers?

I have a spotfire template feeded by a procedure.
And the name of the columns are something like : STATUT , COLUMN2, COLUMN3 ...
Now I need to create a "calculated column" with:
1. expression = [STATUT]
2. Column name = Statut
But when I click "Ok" (in insert Calculated Column menu), I get the following message:
"You cannot use a column name that already exists."
It seems that it's not case sensitive.
Any idea? I need to show the the calculated column header on a table, but don't want to show the names defined in the procedure.
Thank you very much.
iindeed it is case insensitive. curious what version of Spotfire you are on, if you don't mind sharing?
anyway, to solve this, you can go to Edit ยป Column Properties and update the column names here.
another possible solution that I haven't tried is changing your expression to read [STATUT] AS [Statut]. this may or may not solve the issue, but either way it will be helpful to know that you can write any expression here and append AS [My Column Name] to change the title for that visualization.

Azure Search, Track deletions don't work

As you can see bellow, The "IsActive" column define to detect deletion.
If I go to the DB and change a record "CreationTime" and some data, after running the indexer the changes are applyied in the search service.
Though if I go to the DB and change the IsActive column to 0 (false, since it is a bit column) and the creation time off course, after running the indexer I expect the record to disapear from the search service but it is still there.
When updating IsActive column, you need to also update CreationTime to indicate that the row has changed.
Also, Azure Search sees BIT columns as boolean values instead of 0/1 - so try using "false" as the delete marker value.
Note that SQL integrated change tracking policy would take care of both updates and deletes - consider using it if possible.
The value needs to be set as a string in quotes, so for you, the exact value that you should put in the field "Delete Market Value is "false" including quotes.
IsActive column to 0 (false, since it is a bit column)
Here's the problem. According to the newest documentation:
if you have an integer column where deleted rows are marked with the
value 1, use "1". If you have a BIT column where deleted rows are
marked with the Boolean true value, use the string literal True or
true, the case doesn't matter.
So in my case (BIT column), I've set true (without quotes) as delete marker value and everything works like a charm.

Excel - Nested IF/Nested AND/OR in 'calculated field' option - pivot table

I have the following problem:
A datasheet with a column (HOUR) and another column (AM/PM). Entries in the first column consist of 1,2,3,4,5,6,7,8,9,10,11, or 12, the second column consists of 'AM's or 'PM's. Together they define the time of an incident (regarding the below problem, note that I am not allowed to create a new column in the source datasheet or change existing columns). The below formulas 1.) to 3.) work excellent for getting '1's or '0's for incidents that happened either between 8AM and 4PM, or outside of this time window, as long as I create a new column somewhere.
1.) =IF(AND(A1>=8, A1<=11),IF(B1="AM",1,0),0) + IF(AND(A1>=1, A1<=4),IF(B1="PM",1,0),0) + IF(AND(A1=12),IF(B1="PM",1,0),0)
2.) =--OR(AND(A1>=8, B1="AM", A1<>12), AND(OR(A1<=4, A1=12), B1="PM"))
3.) =--OR(AND(OR(A1={8,9,10,11}),B1="AM"), AND(OR(A1={1,2,3,4,12}), B1="PM"))
However, I want the "1"s to be summarized - without creating an extra column - as calculated field in a pivot table. While excel doesn't accept the 3.) formula at all in the calculated field option, excel accepts 1.) and 2.), but puts out only "0"s in all pivot cells. The below is one of the formulas that puts out only "0"s in the pivot table.
=--OR(AND(HOUR>=8,'AM/PM'="AM",HOUR<>12), AND(OR(HOUR<=4,HOUR=12),'AM/PM'="PM"))
The field value settings don't make a difference, and the fields that are created new with 1.) or 2.) cannot be filtered for "1"s or 0"s, so something must be wrong with the field calculation I guess. Does anybody know what I need to change to make it work? Are there special rules for formulas in pivot tables that apply to formula 1.) and 2.) to make them work?
Thanks for any help on this
I think the limitation is not you, but Excel.
See here for description of what is possible, as well as this question
I tried your code and indeed I see it's not working. Even with a simple if code it doesn't seem to work. I think it's is explicitly called a calculated field because you are only able to calculate the fields in the Sum/Total/Count etc. column.
Have a look at MS, this function is quite limited.
I would try to make another work-around to accomplish your goal.
Microsoft

Resources